From c094edb6e95f9f79c7821726e891a9e7b77f6880 Mon Sep 17 00:00:00 2001 From: Thor77 Date: Sat, 21 May 2016 22:59:32 +0200 Subject: [PATCH] refactor config.parse_config * add debug-output * return config-values as expected by __main__.main * remove test_config_invalid * fix tests --- tsstats/config.py | 27 +++++++++++++++++---------- tsstats/tests/test_config.py | 20 +++++++------------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/tsstats/config.py b/tsstats/config.py index 8b432cc..3acda48 100644 --- a/tsstats/config.py +++ b/tsstats/config.py @@ -1,19 +1,26 @@ -from os.path import abspath - -from tsstats.exceptions import InvalidConfig - try: from configparser import ConfigParser except ImportError: from ConfigParser import ConfigParser +import logging + +logger = logging.getLogger('tsstats') + def parse_config(config_path): + logger.debug('reading config') config = ConfigParser() config.read(config_path) - if not config.has_section('General') or not \ - (config.has_option('General', 'log') and - config.has_option('General', 'output')): - raise InvalidConfig - return (abspath(config.get('General', 'log')), - abspath(config.get('General', 'output'))) + # 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') + 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) + ) diff --git a/tsstats/tests/test_config.py b/tsstats/tests/test_config.py index c9becbc..5a84bbb 100644 --- a/tsstats/tests/test_config.py +++ b/tsstats/tests/test_config.py @@ -8,7 +8,6 @@ from os.path import abspath, exists import pytest -from tsstats import exceptions from tsstats.config import parse_config configpath = abspath('tsstats/tests/res/test.cfg') @@ -31,20 +30,15 @@ def config(request): request.addfinalizer(clean) -def test_invalid_config(config): - create_config({ - 'loggfile': 'tsstats/tests/res/test.log', - 'outputfile': '' - }) - with pytest.raises(exceptions.InvalidConfig): - _, _, _, _ = parse_config(configpath) - - def test_config(config): create_config({ + 'idmap': 'tsstats/tests/res/id_map.json', 'log': 'tsstats/tests/res/test.log', 'output': 'output.html', + 'debug': 'true' }) - log, output = parse_config(configpath) - assert log == abspath('tsstats/tests/res/test.log') - assert output == abspath('output.html') + idmap, log, output, debug = parse_config(configpath) + assert idmap == 'tsstats/tests/res/id_map.json' + assert log == 'tsstats/tests/res/test.log' + assert output == 'output.html' + assert debug is True