From d91512d07e03dbc947df277ec5aa5f1e7c95fccf Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Thu, 11 Dec 2014 19:52:51 +0100 Subject: [PATCH] Even more fixes to config parsing --- example.cfg | 4 ++-- tests/utils/test_main.py | 14 ++++++++++++++ vdirsyncer/utils/__init__.py | 21 +++++++++++++-------- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/example.cfg b/example.cfg index 2991aaf..97cc02f 100644 --- a/example.cfg +++ b/example.cfg @@ -43,8 +43,8 @@ collections = ["default", "work"] type = filesystem path = ~/.contacts/ fileext = .vcf -# Create the directory if it doesn't exist: `True` or `False` -#create = True +# Create the directory if it doesn't exist: `true` or `false` +#create = true #encoding = utf-8 [storage bob_contacts_remote] diff --git a/tests/utils/test_main.py b/tests/utils/test_main.py index f42aa61..5ef47dc 100644 --- a/tests/utils/test_main.py +++ b/tests/utils/test_main.py @@ -8,6 +8,7 @@ ''' import click +import pytest from click.testing import CliRunner import os @@ -69,6 +70,19 @@ def test_parse_options(): assert type(a[key]) is type(expected[key]) # flake8: noqa +def test_parse_config_value(): + with pytest.raises(ValueError): + utils.parse_config_value('123 # comment!') + + assert utils.parse_config_value('"123 # comment!"') == '123 # comment!' + assert utils.parse_config_value('True') is True + assert utils.parse_config_value('False') is False + assert utils.parse_config_value('Yes') is True + assert utils.parse_config_value('3.14') == 3.14 + assert utils.parse_config_value('') == '' + assert utils.parse_config_velue('""') == '' + + def test_get_password_from_netrc(monkeypatch): username = 'foouser' password = 'foopass' diff --git a/vdirsyncer/utils/__init__.py b/vdirsyncer/utils/__init__.py index 977d197..7849b86 100644 --- a/vdirsyncer/utils/__init__.py +++ b/vdirsyncer/utils/__init__.py @@ -66,27 +66,32 @@ def uniq(s): def parse_config_value(value): - if value in ('on', 'yes'): + try: + return json.loads(value) + except ValueError: + rv = value + + if value.lower() in ('on', 'true', 'yes'): logger.warning('{} is deprecated for the config, please use true.\n' 'The old form will be removed in 0.4.0.' .format(value)) return True - if value in ('off', 'no'): + if value.lower() in ('off', 'false', 'no'): logger.warning('{} is deprecated for the config, please use false.\n' 'The old form will be removed in 0.4.0.' .format(value)) return False - if value == 'None': + if value.lower() == 'none': logger.warning('None is deprecated for the config, please use null.\n' 'The old form will be removed in 0.4.0.') return None - try: - rv = json.loads(value) - except ValueError: - rv = value + if '#' in value: + raise ValueError('Invalid value:{}\n' + 'Use double quotes (") if you want to use hashes in ' + 'your value.') - if isinstance(rv, (bytes, text_type)) and len(value.splitlines()) > 1: + if len(value.splitlines()) > 1: # ConfigParser's barrier for mistaking an arbitrary line for the # continuation of a value is awfully low. The following example will # also contain the second line in the value: