Make status_path relative to config file

This commit is contained in:
Markus Unterwaditzer 2015-01-12 20:16:10 +01:00
parent 5fa05bce13
commit ce2cea130c
5 changed files with 28 additions and 20 deletions

View file

@ -16,6 +16,8 @@ Version 0.4.2
- Vdirsyncer now respects redirects when uploading and updating items. This - Vdirsyncer now respects redirects when uploading and updating items. This
might fix issues with Zimbra. might fix issues with Zimbra.
- Relative ``status_path`` values are now interpreted as relative to the
configuration file's directory.
Version 0.4.1 Version 0.4.1
============= =============

View file

@ -32,7 +32,8 @@ General Section
- ``status_path``: A directory where vdirsyncer will store metadata for the - ``status_path``: A directory where vdirsyncer will store metadata for the
next sync. The data is needed to determine whether a new item means it has next sync. The data is needed to determine whether a new item means it has
been added on one side or deleted on the other. been added on one side or deleted on the other. Relative paths will be
interpreted as relative to the configuration file's directory.
- ``password_command`` specifies a command to query for server passwords. The - ``password_command`` specifies a command to query for server passwords. The
command will be called with the username as the first argument, and the command will be called with the username as the first argument, and the

View file

@ -40,7 +40,7 @@ def runner(tmpdir, monkeypatch):
return _CustomRunner(tmpdir) return _CustomRunner(tmpdir)
def test_load_config(monkeypatch): def test_read_config(monkeypatch):
f = io.StringIO(dedent(u''' f = io.StringIO(dedent(u'''
[general] [general]
status_path = /tmp/status/ status_path = /tmp/status/
@ -67,7 +67,7 @@ def test_load_config(monkeypatch):
errors = [] errors = []
monkeypatch.setattr('vdirsyncer.cli.cli_logger.error', errors.append) monkeypatch.setattr('vdirsyncer.cli.cli_logger.error', errors.append)
general, pairs, storages = cli.load_config(f) general, pairs, storages = cli.utils.read_config(f)
assert general == {'status_path': '/tmp/status/'} assert general == {'status_path': '/tmp/status/'}
assert pairs == {'bob': ('bob_a', 'bob_b', {'bam': True, 'foo': 'bar'})} assert pairs == {'bob': ('bob_a', 'bob_b', {'bam': True, 'foo': 'bar'})}
assert storages == { assert storages == {
@ -234,7 +234,7 @@ def test_invalid_storage_name():
''')) '''))
with pytest.raises(cli.CliError) as excinfo: with pytest.raises(cli.CliError) as excinfo:
cli.load_config(f) cli.utils.read_config(f)
assert 'invalid characters' in str(excinfo.value).lower() assert 'invalid characters' in str(excinfo.value).lower()

View file

@ -8,7 +8,6 @@
''' '''
import functools import functools
import os
import sys import sys
from .tasks import discover_collections, sync_pair from .tasks import discover_collections, sync_pair
@ -16,7 +15,6 @@ from .utils import CliError, WorkerQueue, cli_logger, handle_cli_error, \
load_config, parse_pairs_args load_config, parse_pairs_args
from .. import __version__, log from .. import __version__, log
from ..doubleclick import click from ..doubleclick import click
from ..utils import expand_path
def catch_errors(f): def catch_errors(f):
@ -58,18 +56,7 @@ def app(ctx, verbosity):
ctx.obj = {} ctx.obj = {}
if 'config' not in ctx.obj: if 'config' not in ctx.obj:
fname = expand_path(os.environ.get('VDIRSYNCER_CONFIG', ctx.obj['config'] = load_config()
'~/.vdirsyncer/config'))
if not os.path.exists(fname):
xdg_config_dir = os.environ.get('XDG_CONFIG_HOME',
expand_path('~/.config/'))
fname = os.path.join(xdg_config_dir, 'vdirsyncer/config')
try:
with open(fname) as f:
ctx.obj['config'] = load_config(f)
except Exception as e:
raise CliError('Error during reading config {}: {}'
.format(fname, e))
main = app main = app

View file

@ -250,7 +250,26 @@ def _validate_pair_section(pair_config):
raise e raise e
def load_config(f): def load_config():
fname = expand_path(os.environ.get('VDIRSYNCER_CONFIG',
'~/.vdirsyncer/config'))
if not os.path.exists(fname):
xdg_config_dir = os.environ.get('XDG_CONFIG_HOME',
expand_path('~/.config/'))
fname = os.path.join(xdg_config_dir, 'vdirsyncer/config')
try:
with open(fname) as f:
general, pairs, storages = read_config(f)
_validate_general_section(general)
general['status_path'] = os.path.join(fname, general['status_path'])
except Exception as e:
raise CliError('Error during reading config {}: {}'
.format(fname, e))
return general, pairs, storages
def read_config(f):
c = RawConfigParser() c = RawConfigParser()
c.readfp(f) c.readfp(f)
@ -293,7 +312,6 @@ def load_config(f):
except ValueError as e: except ValueError as e:
raise CliError('Section `{}`: {}'.format(section, str(e))) raise CliError('Section `{}`: {}'.format(section, str(e)))
_validate_general_section(general)
return general, pairs, storages return general, pairs, storages