mirror of
https://github.com/samsonjs/vdirsyncer.git
synced 2026-04-27 14:57:41 +00:00
Refactor parse_config to use fileobject
This commit is contained in:
parent
cb44046a8a
commit
a1f2d14c05
2 changed files with 13 additions and 16 deletions
|
|
@ -6,6 +6,7 @@
|
||||||
:copyright: (c) 2014 Markus Unterwaditzer & contributors
|
:copyright: (c) 2014 Markus Unterwaditzer & contributors
|
||||||
:license: MIT, see LICENSE for more details.
|
:license: MIT, see LICENSE for more details.
|
||||||
'''
|
'''
|
||||||
|
import io
|
||||||
from textwrap import dedent
|
from textwrap import dedent
|
||||||
|
|
||||||
from click.testing import CliRunner
|
from click.testing import CliRunner
|
||||||
|
|
@ -13,13 +14,10 @@ from click.testing import CliRunner
|
||||||
import vdirsyncer.cli as cli
|
import vdirsyncer.cli as cli
|
||||||
|
|
||||||
|
|
||||||
def test_load_config(tmpdir, monkeypatch):
|
def test_load_config(monkeypatch):
|
||||||
f = tmpdir.join('test.cfg')
|
f = io.StringIO(dedent(u'''
|
||||||
status_path = '{}/status/'.format(str(tmpdir))
|
|
||||||
contacts_path = '{}/contacts/'.format(str(tmpdir))
|
|
||||||
f.write(dedent('''
|
|
||||||
[general]
|
[general]
|
||||||
status_path = {status}
|
status_path = /tmp/status/
|
||||||
|
|
||||||
[pair bob]
|
[pair bob]
|
||||||
a = bob_a
|
a = bob_a
|
||||||
|
|
@ -29,7 +27,7 @@ def test_load_config(tmpdir, monkeypatch):
|
||||||
|
|
||||||
[storage bob_a]
|
[storage bob_a]
|
||||||
type = filesystem
|
type = filesystem
|
||||||
path = {contacts}
|
path = /tmp/contacts/
|
||||||
fileext = .vcf
|
fileext = .vcf
|
||||||
yesno = off
|
yesno = off
|
||||||
number = 42
|
number = 42
|
||||||
|
|
@ -39,16 +37,15 @@ def test_load_config(tmpdir, monkeypatch):
|
||||||
|
|
||||||
[bogus]
|
[bogus]
|
||||||
lol = true
|
lol = true
|
||||||
''').strip().format(status=status_path, contacts=contacts_path))
|
'''))
|
||||||
|
|
||||||
fname = str(tmpdir) + '/test.cfg'
|
|
||||||
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(fname, pair_options=('bam',))
|
general, pairs, storages = cli.load_config(f, pair_options=('bam',))
|
||||||
assert general == {'status_path': status_path}
|
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 == {
|
||||||
'bob_a': {'type': 'filesystem', 'path': contacts_path, 'fileext':
|
'bob_a': {'type': 'filesystem', 'path': '/tmp/contacts/', 'fileext':
|
||||||
'.vcf', 'yesno': False, 'number': 42,
|
'.vcf', 'yesno': False, 'number': 42,
|
||||||
'instance_name': 'bob_a'},
|
'instance_name': 'bob_a'},
|
||||||
'bob_b': {'type': 'carddav', 'instance_name': 'bob_b'}
|
'bob_b': {'type': 'carddav', 'instance_name': 'bob_b'}
|
||||||
|
|
|
||||||
|
|
@ -75,9 +75,8 @@ def validate_general_section(general_config):
|
||||||
raise CliError('Invalid general section.')
|
raise CliError('Invalid general section.')
|
||||||
|
|
||||||
|
|
||||||
def load_config(fname, pair_options=('collections', 'conflict_resolution')):
|
def load_config(f, pair_options=('collections', 'conflict_resolution')):
|
||||||
c = RawConfigParser()
|
c = RawConfigParser()
|
||||||
with open(fname) as f:
|
|
||||||
c.readfp(f)
|
c.readfp(f)
|
||||||
|
|
||||||
get_options = lambda s: dict(parse_options(c.items(s), section=s))
|
get_options = lambda s: dict(parse_options(c.items(s), section=s))
|
||||||
|
|
@ -283,7 +282,8 @@ def _create_app():
|
||||||
expand_path('~/.config/'))
|
expand_path('~/.config/'))
|
||||||
fname = os.path.join(xdg_config_dir, 'vdirsyncer/config')
|
fname = os.path.join(xdg_config_dir, 'vdirsyncer/config')
|
||||||
try:
|
try:
|
||||||
ctx.obj['config'] = load_config(fname)
|
with open(fname) as f:
|
||||||
|
ctx.obj['config'] = load_config(f)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise CliError('Error during reading config {}: {}'
|
raise CliError('Error during reading config {}: {}'
|
||||||
.format(fname, e))
|
.format(fname, e))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue