refactor config.parse_config

* add debug-output
* return config-values as expected by __main__.main
* remove test_config_invalid
* fix tests
This commit is contained in:
Thor77 2016-05-21 22:59:32 +02:00
parent ec62d4f439
commit c094edb6e9
2 changed files with 24 additions and 23 deletions

View File

@ -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)
)

View File

@ -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