64 lines
1.6 KiB
Python
64 lines
1.6 KiB
Python
#! /usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
#
|
|
# Copyright (C) 2011 Sebastian Pipping <sebastian@pipping.org>
|
|
# Licensed under GPL v3 or later
|
|
|
|
from __future__ import print_function
|
|
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
|
|
('SERVER_URL', str, None),
|
|
('SERVER_USER', str, None),
|
|
('SERVER_PASSWORD', str, None),
|
|
)
|
|
|
|
_DEFAULTS = dict([(key, str(default))
|
|
for (key, _type, default)
|
|
in _KEYS
|
|
if default is not None])
|
|
|
|
|
|
class Settings:
|
|
def __init__(self):
|
|
pass
|
|
|
|
def load(self, filename, _additional_keys=None):
|
|
additional_keys = () if _additional_keys is None else _additional_keys
|
|
|
|
cp = ConfigParser.ConfigParser(_DEFAULTS)
|
|
if not cp.read(filename):
|
|
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(_SECTION, key)
|
|
else:
|
|
value = cp.get(_SECTION, key)
|
|
except ValueError:
|
|
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.%s".' \
|
|
% (filename, _SECTION, key), file=sys.stderr)
|
|
valid = False
|
|
else:
|
|
setattr(self, key, value)
|
|
if self.CLIENT_DEBUG:
|
|
print('DEBUG: CONFIG: %s.%s=%s("%s")' % (_SECTION, key, type(value).__name__, value))
|
|
|
|
return valid
|
|
|
|
|
|
settings = Settings()
|