Even more fixes to config parsing

This commit is contained in:
Markus Unterwaditzer 2014-12-11 19:52:51 +01:00
parent 43ff90da9a
commit d91512d07e
3 changed files with 29 additions and 10 deletions

View file

@ -43,8 +43,8 @@ collections = ["default", "work"]
type = filesystem type = filesystem
path = ~/.contacts/ path = ~/.contacts/
fileext = .vcf fileext = .vcf
# Create the directory if it doesn't exist: `True` or `False` # Create the directory if it doesn't exist: `true` or `false`
#create = True #create = true
#encoding = utf-8 #encoding = utf-8
[storage bob_contacts_remote] [storage bob_contacts_remote]

View file

@ -8,6 +8,7 @@
''' '''
import click import click
import pytest
from click.testing import CliRunner from click.testing import CliRunner
import os import os
@ -69,6 +70,19 @@ def test_parse_options():
assert type(a[key]) is type(expected[key]) # flake8: noqa 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): def test_get_password_from_netrc(monkeypatch):
username = 'foouser' username = 'foouser'
password = 'foopass' password = 'foopass'

View file

@ -66,27 +66,32 @@ def uniq(s):
def parse_config_value(value): 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' logger.warning('{} is deprecated for the config, please use true.\n'
'The old form will be removed in 0.4.0.' 'The old form will be removed in 0.4.0.'
.format(value)) .format(value))
return True 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' logger.warning('{} is deprecated for the config, please use false.\n'
'The old form will be removed in 0.4.0.' 'The old form will be removed in 0.4.0.'
.format(value)) .format(value))
return False return False
if value == 'None': if value.lower() == 'none':
logger.warning('None is deprecated for the config, please use null.\n' logger.warning('None is deprecated for the config, please use null.\n'
'The old form will be removed in 0.4.0.') 'The old form will be removed in 0.4.0.')
return None return None
try: if '#' in value:
rv = json.loads(value) raise ValueError('Invalid value:{}\n'
except ValueError: 'Use double quotes (") if you want to use hashes in '
rv = value '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 # ConfigParser's barrier for mistaking an arbitrary line for the
# continuation of a value is awfully low. The following example will # continuation of a value is awfully low. The following example will
# also contain the second line in the value: # also contain the second line in the value: