refactor __main__.cli and __main__.main
* __main__.cli is now just used to setup argparse and pass arguments to __main__.main * __main__.main is now used to validate the arguments and pass them to the right functions
This commit is contained in:
parent
23145b476b
commit
ec62d4f439
|
@ -4,7 +4,7 @@ import logging
|
||||||
from os.path import abspath, exists
|
from os.path import abspath, exists
|
||||||
|
|
||||||
from tsstats.config import parse_config
|
from tsstats.config import parse_config
|
||||||
from tsstats.exceptions import ConfigNotFound
|
from tsstats.exceptions import InvalidConfig
|
||||||
from tsstats.log import parse_logs
|
from tsstats.log import parse_logs
|
||||||
from tsstats.template import render_template
|
from tsstats.template import render_template
|
||||||
|
|
||||||
|
@ -35,28 +35,33 @@ def cli():
|
||||||
help='debug mode', action='store_true'
|
help='debug mode', action='store_true'
|
||||||
)
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
if args.debug:
|
main(**vars(args))
|
||||||
|
|
||||||
|
|
||||||
|
def main(config=None, idmap=None, log=None, output=None, debug=False):
|
||||||
|
if config:
|
||||||
|
config = abspath(config)
|
||||||
|
if not exists(config):
|
||||||
|
logger.fatal('config not found (%s)', config)
|
||||||
|
idmap, log, output, debug = parse_config(config)
|
||||||
|
|
||||||
|
if debug:
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
main(args.config, args.idmap)
|
|
||||||
|
|
||||||
|
if idmap:
|
||||||
def main(config_path='config.ini', id_map_path='id_map.json'):
|
idmap = abspath(idmap)
|
||||||
# check cmdline-args
|
if not exists(idmap):
|
||||||
config_path = abspath(config_path)
|
logger.fatal('identmap not found (%s)', idmap)
|
||||||
id_map_path = abspath(id_map_path)
|
|
||||||
|
|
||||||
if not exists(config_path):
|
|
||||||
raise ConfigNotFound(config_path)
|
|
||||||
|
|
||||||
if exists(id_map_path):
|
|
||||||
# read id_map
|
# read id_map
|
||||||
id_map = json.load(open(id_map_path))
|
identmap = json.load(open(idmap))
|
||||||
else:
|
else:
|
||||||
id_map = {}
|
identmap = None
|
||||||
|
|
||||||
log, output = parse_config(config_path)
|
if not log or not output:
|
||||||
clients = parse_logs(log, ident_map=id_map)
|
raise InvalidConfig('log or output missing')
|
||||||
render_template(clients, output=output)
|
|
||||||
|
clients = parse_logs(log, ident_map=identmap)
|
||||||
|
render_template(clients, output=abspath(output))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in New Issue