python3-carom-client/carom/config.py
2018-11-22 20:47:36 +01:00

44 lines
1.1 KiB
Python

import configparser
import logging
from voluptuous import Schema, Error, Range, All, Coerce
from voluptuous.humanize import validate_with_humanized_errors
from carom.const import CONFIG_FILES, DEFAULT_CONFIG
logger = logging.getLogger(__name__)
_parser = configparser.ConfigParser()
_parser.read_string(DEFAULT_CONFIG)
_parser.read(CONFIG_FILES)
# Convert the ConfigParser object to a real dict now:
_parser_dict = {s: dict(_parser.items(s)) for s in _parser.sections()}
_schema = Schema({
'connection': {
'endpoint': str,
'token': str,
},
'mapping': {
All(Coerce(int), Range(0, 28)): All(Coerce(int), Range(1, 8)),
},
'logging': {
'file': str
}
})
try:
config = validate_with_humanized_errors(_parser_dict, _schema)
except Error as e:
logger.error('Failed to validate the configuration:\n{}'.format(e))
config = None
def log_config():
for s in _parser.sections():
logger.warning("[{}]".format(s))
for i in _parser.items(s):
logger.warning("{} = {}".format(*i))
__all__ = ('config', 'log_config')