Solve the boolean problem in config files

once and for all
This commit is contained in:
Markus Unterwaditzer 2014-02-28 21:50:58 +01:00
parent 1e3b8055d8
commit f0d7e211d9
2 changed files with 32 additions and 12 deletions

View file

@ -28,24 +28,39 @@ storage_names = {
} }
def get_config_parser(env): def parse_options(items):
fname = env.get('VDIRSYNCER_CONFIG', expand_path('~/.vdirsyncer/config')) for key, value in items:
if value.lower() in ('yes', 'true', 'on'):
value = True
elif value.lower() in ('no', 'false', 'off'):
value = False
try:
value = int(value)
except ValueError:
pass
yield key, value
def load_config(fname):
c = ConfigParser.RawConfigParser() c = ConfigParser.RawConfigParser()
c.read(fname) c.read(fname)
get_options = lambda s: dict(parse_options(c.items(s)))
pairs = {} pairs = {}
storages = {} storages = {}
for section in c.sections(): for section in c.sections():
if section.startswith('storage '): if section.startswith('storage '):
name = section[len('storage '):] name = section[len('storage '):]
storages.setdefault(name, {}).update(c.items(section)) storages.setdefault(name, {}).update(get_options(section))
elif section.startswith('pair '): elif section.startswith('pair '):
name = section[len('pair '):] name = section[len('pair '):]
options = dict(c.items(section)) options = get_options(section)
pairs[name] = a, b = (options.pop('a'), options.pop('b')) pairs[name] = a, b = options.pop('a'), options.pop('b')
storages.setdefault(a, {}).update(options) storages.setdefault(a, {}).update(options)
storages.setdefault(b, {}).update(options) storages.setdefault(b, {}).update(options)
elif section == 'general': elif section == 'general':
general = dict(c.items(section)) general = get_options(section)
else: else:
cli_logger.error( cli_logger.error(
'Unknown section in {}: {}'.format(fname, section)) 'Unknown section in {}: {}'.format(fname, section))
@ -95,7 +110,9 @@ def storage_instance_from_config(config):
def main(): def main():
env = os.environ env = os.environ
cfg = get_config_parser(env)
fname = env.get('VDIRSYNCER_CONFIG', expand_path('~/.vdirsyncer/config'))
cfg = load_config(fname)
_main(env, cfg) _main(env, cfg)
@ -150,7 +167,7 @@ def _main(env, file_cfg):
app.register_command('sync', sync_command) app.register_command('sync', sync_command)
if general.get('verbose', 'False').lower() == 'true': if general.get('verbose', False):
verbose_option() verbose_option()
else: else:
quiet_option() quiet_option()

View file

@ -36,8 +36,8 @@ class CaldavStorage(Storage):
:param url: Direct URL for the CalDAV collection. No autodiscovery. :param url: Direct URL for the CalDAV collection. No autodiscovery.
:param username: Username for authentication. :param username: Username for authentication.
:param password: Password for authentication. :param password: Password for authentication.
:param start_date: Start date of timerange to show, default now. :param start_date: Start date of timerange to show, default -inf.
:param end_date: End date of timerange to show, default now + one year. :param end_date: End date of timerange to show, default +inf.
:param verify: Verify SSL certificate, default True. :param verify: Verify SSL certificate, default True.
:param auth: Authentication method, from {'basic', 'digest'}, default 'basic'. :param auth: Authentication method, from {'basic', 'digest'}, default 'basic'.
:param useragent: Default 'vdirsyncer'. :param useragent: Default 'vdirsyncer'.
@ -66,8 +66,11 @@ class CaldavStorage(Storage):
elif start_date is not None and end_date is not None: elif start_date is not None and end_date is not None:
namespace = dict(datetime.__dict__) namespace = dict(datetime.__dict__)
namespace['start_date'] = self.start_date = \ namespace['start_date'] = self.start_date = \
eval(start_date, namespace) (eval(start_date, namespace) if isinstance(start_date, str)
self.end_date = eval(end_date, namespace) else start_date)
self.end_date = \
(eval(end_date, namespace) if isinstance(end_date, str)
else end_date)
headers = self._default_headers() headers = self._default_headers()
headers['Depth'] = 1 headers['Depth'] = 1