From 708f071033f50a31d31c35ded4e31186b23f80bf Mon Sep 17 00:00:00 2001
From: Thor77 <thor77@thor77.org>
Date: Sat, 6 Aug 2016 21:36:17 +0200
Subject: [PATCH] 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
---
 setup.py            |  2 +-
 tsstats/__main__.py | 41 ++++++++++++++++++++++++-----------------
 tsstats/config.py   | 39 ++++++++++++++++++++++-----------------
 3 files changed, 47 insertions(+), 35 deletions(-)

diff --git a/setup.py b/setup.py
index 1be7488..4614435 100644
--- a/setup.py
+++ b/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',
diff --git a/tsstats/__main__.py b/tsstats/__main__.py
index d31ceea..a8b3a23 100644
--- a/tsstats/__main__.py
+++ b/tsstats/__main__.py
@@ -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:
+def main(configuration):
+    if configuration.getboolean('General', '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)
-
+    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__':
diff --git a/tsstats/config.py b/tsstats/config.py
index 498bd54..9df3558 100644
--- a/tsstats/config.py
+++ b/tsstats/config.py
@@ -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