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:
Thor77 2016-08-06 21:36:17 +02:00
parent 2a1ab472bb
commit 708f071033
3 changed files with 47 additions and 35 deletions

View File

@ -2,7 +2,7 @@ from setuptools import setup
setup( setup(
name='tsstats', name='tsstats',
version='0.9.1', version='0.10.0',
author='Thor77', author='Thor77',
author_email='thor77@thor77.org', author_email='thor77@thor77.org',
description='A simple Teamspeak stats-generator', description='A simple Teamspeak stats-generator',

View File

@ -5,7 +5,7 @@ import json
import logging import logging
from os.path import abspath, exists from os.path import abspath, exists
from tsstats.config import parse_config from tsstats import config
from tsstats.exceptions import InvalidConfiguration from tsstats.exceptions import InvalidConfiguration
from tsstats.log import parse_logs from tsstats.log import parse_logs
from tsstats.template import render_template from tsstats.template import render_template
@ -15,7 +15,8 @@ logger = logging.getLogger('tsstats')
def cli(): def cli():
parser = argparse.ArgumentParser( 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( parser.add_argument(
'-c', '--config', '-c', '--config',
@ -41,22 +42,21 @@ def cli():
help='don\'t add connect until now to onlinetime', help='don\'t add connect until now to onlinetime',
action='store_false', dest='onlinedc' 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, def main(configuration):
output=None, debug=False, onlinedc=True): if configuration.getboolean('General', 'debug'):
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:
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
idmap = configuration.get('General', 'idmap')
if idmap: if idmap:
idmap = abspath(idmap) idmap = abspath(idmap)
if not exists(idmap): if not exists(idmap):
@ -66,16 +66,23 @@ def main(config=None, idmap=None, log=None,
else: else:
identmap = None identmap = None
if not log or not output: log = configuration.get('General', 'log')
if not log:
raise InvalidConfiguration('log or output missing') 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(): for sid, clients in sid_clients.items():
if sid: if sid:
ext = '.{}'.format(sid) ext = '.{}'.format(sid)
else: else:
ext = '' ext = ''
render_template(clients, output=abspath(output + ext)) render_template(
clients,
output=abspath(configuration.get('General', 'output') + ext)
)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -10,7 +10,18 @@ import logging
logger = logging.getLogger('tsstats') 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` parse config at `config_path`
@ -22,19 +33,13 @@ def parse_config(config_path):
''' '''
logger.debug('reading config') logger.debug('reading config')
config = ConfigParser() config = ConfigParser()
config.read(config_path) # use this way to set defaults, because ConfigParser.read_dict
# use dict(ConfigParser.items) to get an easy-to-use interface # is not available < 3.2
# compatible with py2 and py3 for section, items in DEFAULT_CONFIG.items():
config_items = dict(config.items('General')) if section not in config:
if 'debug' in config_items: config.add_section(section)
config_items['debug'] = config.getboolean('General', 'debug') for key, value in items.items():
if 'onlinedc' in config_items: config.set(section, key, str(value))
config_items['onlinedc'] = config.getboolean('General', 'onlinedc') if path:
logger.debug('raw config: %s', config_items) config.read(path)
return ( return config
config_items.get('idmap'),
config_items.get('log'),
config_items.get('output'),
config_items.get('debug', False),
config_items.get('onlinedc', True)
)