replace tsstats.config.parse_config with tsstats.config.load, which just returns a configparser.ConfigParser-instance for easier extension
tsstats.__main__.main: now accepts only a configparser.ConfigParser-instance and extracts values from it tsstats.__main__.cli: defaults from argparser are now suppressed and given cli-args override values from config * bump version to 0.10.0
This commit is contained in:
parent
2a1ab472bb
commit
708f071033
2
setup.py
2
setup.py
|
@ -2,7 +2,7 @@ from setuptools import setup
|
|||
|
||||
setup(
|
||||
name='tsstats',
|
||||
version='0.9.1',
|
||||
version='0.10.0',
|
||||
author='Thor77',
|
||||
author_email='thor77@thor77.org',
|
||||
description='A simple Teamspeak stats-generator',
|
||||
|
|
|
@ -5,7 +5,7 @@ import json
|
|||
import logging
|
||||
from os.path import abspath, exists
|
||||
|
||||
from tsstats.config import parse_config
|
||||
from tsstats import config
|
||||
from tsstats.exceptions import InvalidConfiguration
|
||||
from tsstats.log import parse_logs
|
||||
from tsstats.template import render_template
|
||||
|
@ -15,7 +15,8 @@ logger = logging.getLogger('tsstats')
|
|||
|
||||
def cli():
|
||||
parser = argparse.ArgumentParser(
|
||||
description='A simple Teamspeak stats-generator - based on server-logs'
|
||||
description='A simple Teamspeak stats-generator, based on server-logs',
|
||||
argument_default=argparse.SUPPRESS
|
||||
)
|
||||
parser.add_argument(
|
||||
'-c', '--config',
|
||||
|
@ -41,22 +42,21 @@ def cli():
|
|||
help='don\'t add connect until now to onlinetime',
|
||||
action='store_false', dest='onlinedc'
|
||||
)
|
||||
main(**vars(parser.parse_args()))
|
||||
options = parser.parse_args()
|
||||
if 'config' in options:
|
||||
configuration = config.load(options.config)
|
||||
else:
|
||||
configuration = config.load()
|
||||
for option, value in vars(options).items():
|
||||
configuration.set('General', option, value)
|
||||
main(configuration)
|
||||
|
||||
|
||||
def main(config=None, idmap=None, log=None,
|
||||
output=None, debug=False, onlinedc=True):
|
||||
if debug:
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
if config:
|
||||
config = abspath(config)
|
||||
if not exists(config):
|
||||
logger.fatal('config not found (%s)', config)
|
||||
idmap, log, output, debug, onlinedc = parse_config(config)
|
||||
if debug:
|
||||
def main(configuration):
|
||||
if configuration.getboolean('General', 'debug'):
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
idmap = configuration.get('General', 'idmap')
|
||||
if idmap:
|
||||
idmap = abspath(idmap)
|
||||
if not exists(idmap):
|
||||
|
@ -66,16 +66,23 @@ def main(config=None, idmap=None, log=None,
|
|||
else:
|
||||
identmap = None
|
||||
|
||||
if not log or not output:
|
||||
log = configuration.get('General', 'log')
|
||||
if not log:
|
||||
raise InvalidConfiguration('log or output missing')
|
||||
|
||||
sid_clients = parse_logs(log, ident_map=identmap, online_dc=onlinedc)
|
||||
sid_clients = parse_logs(
|
||||
log, ident_map=identmap,
|
||||
online_dc=configuration.getboolean('General', 'onlinedc')
|
||||
)
|
||||
for sid, clients in sid_clients.items():
|
||||
if sid:
|
||||
ext = '.{}'.format(sid)
|
||||
else:
|
||||
ext = ''
|
||||
render_template(clients, output=abspath(output + ext))
|
||||
render_template(
|
||||
clients,
|
||||
output=abspath(configuration.get('General', 'output') + ext)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -10,7 +10,18 @@ import logging
|
|||
logger = logging.getLogger('tsstats')
|
||||
|
||||
|
||||
def parse_config(config_path):
|
||||
DEFAULT_CONFIG = {
|
||||
'General': {
|
||||
'debug': False,
|
||||
'log': '',
|
||||
'output': 'output.html',
|
||||
'idmap': '',
|
||||
'onlinedc': True
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def load(path=None):
|
||||
'''
|
||||
parse config at `config_path`
|
||||
|
||||
|
@ -22,19 +33,13 @@ def parse_config(config_path):
|
|||
'''
|
||||
logger.debug('reading config')
|
||||
config = ConfigParser()
|
||||
config.read(config_path)
|
||||
# use dict(ConfigParser.items) to get an easy-to-use interface
|
||||
# compatible with py2 and py3
|
||||
config_items = dict(config.items('General'))
|
||||
if 'debug' in config_items:
|
||||
config_items['debug'] = config.getboolean('General', 'debug')
|
||||
if 'onlinedc' in config_items:
|
||||
config_items['onlinedc'] = config.getboolean('General', 'onlinedc')
|
||||
logger.debug('raw config: %s', config_items)
|
||||
return (
|
||||
config_items.get('idmap'),
|
||||
config_items.get('log'),
|
||||
config_items.get('output'),
|
||||
config_items.get('debug', False),
|
||||
config_items.get('onlinedc', True)
|
||||
)
|
||||
# use this way to set defaults, because ConfigParser.read_dict
|
||||
# is not available < 3.2
|
||||
for section, items in DEFAULT_CONFIG.items():
|
||||
if section not in config:
|
||||
config.add_section(section)
|
||||
for key, value in items.items():
|
||||
config.set(section, key, str(value))
|
||||
if path:
|
||||
config.read(path)
|
||||
return config
|
||||
|
|
Loading…
Reference in New Issue