mirror of
https://github.com/samsonjs/vdirsyncer.git
synced 2026-03-29 09:35:50 +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
|
||||
:license: MIT, see LICENSE for more details.
|
||||
'''
|
||||
import io
|
||||
from textwrap import dedent
|
||||
|
||||
from click.testing import CliRunner
|
||||
|
|
@ -13,13 +14,10 @@ from click.testing import CliRunner
|
|||
import vdirsyncer.cli as cli
|
||||
|
||||
|
||||
def test_load_config(tmpdir, monkeypatch):
|
||||
f = tmpdir.join('test.cfg')
|
||||
status_path = '{}/status/'.format(str(tmpdir))
|
||||
contacts_path = '{}/contacts/'.format(str(tmpdir))
|
||||
f.write(dedent('''
|
||||
def test_load_config(monkeypatch):
|
||||
f = io.StringIO(dedent(u'''
|
||||
[general]
|
||||
status_path = {status}
|
||||
status_path = /tmp/status/
|
||||
|
||||
[pair bob]
|
||||
a = bob_a
|
||||
|
|
@ -29,7 +27,7 @@ def test_load_config(tmpdir, monkeypatch):
|
|||
|
||||
[storage bob_a]
|
||||
type = filesystem
|
||||
path = {contacts}
|
||||
path = /tmp/contacts/
|
||||
fileext = .vcf
|
||||
yesno = off
|
||||
number = 42
|
||||
|
|
@ -39,16 +37,15 @@ def test_load_config(tmpdir, monkeypatch):
|
|||
|
||||
[bogus]
|
||||
lol = true
|
||||
''').strip().format(status=status_path, contacts=contacts_path))
|
||||
'''))
|
||||
|
||||
fname = str(tmpdir) + '/test.cfg'
|
||||
errors = []
|
||||
monkeypatch.setattr('vdirsyncer.cli.cli_logger.error', errors.append)
|
||||
general, pairs, storages = cli.load_config(fname, pair_options=('bam',))
|
||||
assert general == {'status_path': status_path}
|
||||
general, pairs, storages = cli.load_config(f, pair_options=('bam',))
|
||||
assert general == {'status_path': '/tmp/status/'}
|
||||
assert pairs == {'bob': ('bob_a', 'bob_b', {'bam': True}, {'foo': 'bar'})}
|
||||
assert storages == {
|
||||
'bob_a': {'type': 'filesystem', 'path': contacts_path, 'fileext':
|
||||
'bob_a': {'type': 'filesystem', 'path': '/tmp/contacts/', 'fileext':
|
||||
'.vcf', 'yesno': False, 'number': 42,
|
||||
'instance_name': 'bob_a'},
|
||||
'bob_b': {'type': 'carddav', 'instance_name': 'bob_b'}
|
||||
|
|
|
|||
|
|
@ -75,10 +75,9 @@ def validate_general_section(general_config):
|
|||
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()
|
||||
with open(fname) as f:
|
||||
c.readfp(f)
|
||||
c.readfp(f)
|
||||
|
||||
get_options = lambda s: dict(parse_options(c.items(s), section=s))
|
||||
|
||||
|
|
@ -283,7 +282,8 @@ def _create_app():
|
|||
expand_path('~/.config/'))
|
||||
fname = os.path.join(xdg_config_dir, 'vdirsyncer/config')
|
||||
try:
|
||||
ctx.obj['config'] = load_config(fname)
|
||||
with open(fname) as f:
|
||||
ctx.obj['config'] = load_config(f)
|
||||
except Exception as e:
|
||||
raise CliError('Error during reading config {}: {}'
|
||||
.format(fname, e))
|
||||
|
|
|
|||
Loading…
Reference in a new issue