Extract constant for config file section
.. and improve config error reporting
This commit is contained in:
parent
62f0f02fa7
commit
692284f41d
|
@ -9,6 +9,8 @@ import sys
|
|||
import ConfigParser
|
||||
|
||||
|
||||
_SECTION = 'client-barcode'
|
||||
|
||||
_KEYS = (
|
||||
# Key, type, default
|
||||
('CLIENT_DEBUG', bool, False), # Should go first to enable debugging as early as possible
|
||||
|
@ -32,28 +34,28 @@ class Settings:
|
|||
|
||||
cp = ConfigParser.ConfigParser(_DEFAULTS)
|
||||
if not cp.read(filename):
|
||||
print('FATAL: Config file %s could not be parsed.' % (filename), file=sys.stderr)
|
||||
print('FATAL: Config file "%s" could not be parsed.' % (filename), file=sys.stderr)
|
||||
return False
|
||||
|
||||
valid = True
|
||||
for key, _type, dummy in _KEYS + additional_keys:
|
||||
try:
|
||||
if _type is bool:
|
||||
value = cp.getboolean('client-barcode', key)
|
||||
value = cp.getboolean(_SECTION, key)
|
||||
else:
|
||||
value = cp.get('client-barcode', key)
|
||||
value = cp.get(_SECTION, key)
|
||||
except ValueError:
|
||||
print('ERROR: Key %s in config file %s must be of type %s.' \
|
||||
print('ERROR: Key "%s" in config file "%s" must be of type "%s".' \
|
||||
% (key, filename, _type.__name__), file=sys.stderr)
|
||||
valid = False
|
||||
except ConfigParser.NoOptionError:
|
||||
print('ERROR: Config file %s misses required key %s.' \
|
||||
% (filename, key), file=sys.stderr)
|
||||
print('ERROR: Config file "%s" misses required key "%s.%s".' \
|
||||
% (filename, _SECTION, key), file=sys.stderr)
|
||||
valid = False
|
||||
else:
|
||||
setattr(self, key, value)
|
||||
if self.CLIENT_DEBUG:
|
||||
print('DEBUG: CONFIG: %s=%s("%s")' % (key, type(value).__name__, value))
|
||||
print('DEBUG: CONFIG: %s.%s=%s("%s")' % (_SECTION, key, type(value).__name__, value))
|
||||
|
||||
return valid
|
||||
|
||||
|
|
Loading…
Reference in New Issue