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: try:
from configparser import ConfigParser from configparser import ConfigParser
except ImportError: except ImportError:
from ConfigParser import ConfigParser from ConfigParser import ConfigParser
import logging
logger = logging.getLogger('tsstats')
def parse_config(config_path): def parse_config(config_path):
logger.debug('reading config')
config = ConfigParser() config = ConfigParser()
config.read(config_path) config.read(config_path)
if not config.has_section('General') or not \ # use dict(ConfigParser.items) to get an easy-to-use interface
(config.has_option('General', 'log') and # compatible with py2 and py3
config.has_option('General', 'output')): config_items = dict(config.items('General'))
raise InvalidConfig if 'debug' in config_items:
return (abspath(config.get('General', 'log')), config_items['debug'] = config.getboolean('General', 'debug')
abspath(config.get('General', 'output'))) 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 import pytest
from tsstats import exceptions
from tsstats.config import parse_config from tsstats.config import parse_config
configpath = abspath('tsstats/tests/res/test.cfg') configpath = abspath('tsstats/tests/res/test.cfg')
@ -31,20 +30,15 @@ def config(request):
request.addfinalizer(clean) 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): def test_config(config):
create_config({ create_config({
'idmap': 'tsstats/tests/res/id_map.json',
'log': 'tsstats/tests/res/test.log', 'log': 'tsstats/tests/res/test.log',
'output': 'output.html', 'output': 'output.html',
'debug': 'true'
}) })
log, output = parse_config(configpath) idmap, log, output, debug = parse_config(configpath)
assert log == abspath('tsstats/tests/res/test.log') assert idmap == 'tsstats/tests/res/id_map.json'
assert output == abspath('output.html') assert log == 'tsstats/tests/res/test.log'
assert output == 'output.html'
assert debug is True