Add other output formats
This commit is contained in:
parent
168c2199ca
commit
20fa690c90
2 changed files with 41 additions and 4 deletions
|
@ -13,6 +13,7 @@ import logging
|
||||||
from logging.handlers import SysLogHandler
|
from logging.handlers import SysLogHandler
|
||||||
import click
|
import click
|
||||||
import click_config_file
|
import click_config_file
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
|
||||||
HOME_FOLDER = os.environ.get('HOME', os.environ.get('USERPROFILE', '/'))
|
HOME_FOLDER = os.environ.get('HOME', os.environ.get('USERPROFILE', '/'))
|
||||||
|
@ -71,11 +72,36 @@ class __project_codename_camel__:
|
||||||
data['last_update'] = time.time()
|
data['last_update'] = time.time()
|
||||||
with open(self.config['cache_file'], 'w', encoding='utf-8') as cache_file:
|
with open(self.config['cache_file'], 'w', encoding='utf-8') as cache_file:
|
||||||
json.dump(data, cache_file, indent=2)
|
json.dump(data, cache_file, indent=2)
|
||||||
self._log.debug(
|
self._debug(
|
||||||
"Saved cached data in '%s'",
|
f"Saved cached data in '{self.config['cache_file']}'",
|
||||||
self.config['cache_file']
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _output(self, message):
|
||||||
|
if self.config['output_format'] == 'JSON':
|
||||||
|
return json.dumps(message, indent=2)
|
||||||
|
elif self.config['output_format'] == 'YAML':
|
||||||
|
return yaml.dump(message, Dumper=yaml.Dumper)
|
||||||
|
elif self.config['output_format'] == 'PLAIN':
|
||||||
|
return message
|
||||||
|
else:
|
||||||
|
self._log.warning(
|
||||||
|
"Output format '%s' not supported",
|
||||||
|
self.config['output_format']
|
||||||
|
)
|
||||||
|
return message
|
||||||
|
|
||||||
|
def _info(self, message):
|
||||||
|
return self._log.info(self._output(message))
|
||||||
|
|
||||||
|
def _warning(self, message):
|
||||||
|
return self._log.warning(self._output(message))
|
||||||
|
|
||||||
|
def _error(self, message):
|
||||||
|
return self._log.error(self._output(message))
|
||||||
|
|
||||||
|
def _debug(self, message):
|
||||||
|
return self._log.debug(self._output(message))
|
||||||
|
|
||||||
def _init_log(self):
|
def _init_log(self):
|
||||||
''' Initialize log object '''
|
''' Initialize log object '''
|
||||||
self._log = logging.getLogger("__project_codename__")
|
self._log = logging.getLogger("__project_codename__")
|
||||||
|
@ -127,6 +153,16 @@ class __project_codename_camel__:
|
||||||
),
|
),
|
||||||
help='Set the debug level for the standard output.'
|
help='Set the debug level for the standard output.'
|
||||||
)
|
)
|
||||||
|
@click.option(
|
||||||
|
"--output-format",
|
||||||
|
"-o",
|
||||||
|
default="JSON",
|
||||||
|
type=click.Choice(
|
||||||
|
["JSON", "YAML", "CSV", "PLAIN"],
|
||||||
|
case_sensitive=False,
|
||||||
|
),
|
||||||
|
help='Set the output format.'
|
||||||
|
)
|
||||||
@click.option(
|
@click.option(
|
||||||
'--log-file',
|
'--log-file',
|
||||||
'-l',
|
'-l',
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
click
|
click
|
||||||
click_config_file
|
click_config_file
|
||||||
|
PyYAML
|
||||||
|
|
Loading…
Reference in a new issue