From f54acc9bbff671f2f3e472f085b1e6291295d0ba Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Sun, 6 Sep 2015 15:33:56 -0700 Subject: [PATCH] #11: Fixed parsing of punctuation in configuration file. --- NEWS | 4 +++ atticmatic/config.py | 10 +++---- atticmatic/tests/integration/test_config.py | 29 +++++++++++++++++++++ atticmatic/tests/unit/test_config.py | 2 +- setup.py | 2 +- 5 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 atticmatic/tests/integration/test_config.py diff --git a/NEWS b/NEWS index d5185023..2a7c7aee 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ +0.1.7-dev + + * #11: Fixed parsing of punctuation in configuration file. + 0.1.6 * #9: New configuration option for the encryption passphrase. diff --git a/atticmatic/config.py b/atticmatic/config.py index bc1a32d1..4f45b89b 100644 --- a/atticmatic/config.py +++ b/atticmatic/config.py @@ -2,10 +2,10 @@ from collections import OrderedDict, namedtuple try: # Python 2 - from ConfigParser import ConfigParser + from ConfigParser import RawConfigParser except ImportError: # Python 3 - from configparser import ConfigParser + from configparser import RawConfigParser Section_format = namedtuple('Section_format', ('name', 'options')) @@ -22,7 +22,7 @@ def option(name, value_type=str, required=True): def validate_configuration_format(parser, config_format): ''' - Given an open ConfigParser and an expected config file format, validate that the parsed + Given an open RawConfigParser and an expected config file format, validate that the parsed configuration file has the expected sections, that any required options are present in those sections, and that there aren't any unexpected options. @@ -83,7 +83,7 @@ def validate_configuration_format(parser, config_format): def parse_section_options(parser, section_format): ''' - Given an open ConfigParser and an expected section format, return the option values from that + Given an open RawConfigParser and an expected section format, return the option values from that section as a dict mapping from option name to value. Omit those options that are not present in the parsed options. @@ -108,7 +108,7 @@ def parse_configuration(config_filename, config_format): Raise IOError if the file cannot be read, or ValueError if the format is not as expected. ''' - parser = ConfigParser() + parser = RawConfigParser() parser.read(config_filename) validate_configuration_format(parser, config_format) diff --git a/atticmatic/tests/integration/test_config.py b/atticmatic/tests/integration/test_config.py new file mode 100644 index 00000000..31fcd9e3 --- /dev/null +++ b/atticmatic/tests/integration/test_config.py @@ -0,0 +1,29 @@ +try: + # Python 2 + from cStringIO import StringIO +except ImportError: + # Python 3 + from io import StringIO + +from collections import OrderedDict +import string + +from atticmatic import config as module + + +def test_parse_section_options_with_punctuation_should_return_section_options(): + parser = module.RawConfigParser() + parser.readfp(StringIO('[section]\nfoo: {}\n'.format(string.punctuation))) + + section_format = module.Section_format( + 'section', + (module.Config_option('foo', str, required=True),), + ) + + config = module.parse_section_options(parser, section_format) + + assert config == OrderedDict( + ( + ('foo', string.punctuation), + ) + ) diff --git a/atticmatic/tests/unit/test_config.py b/atticmatic/tests/unit/test_config.py index 4c889b09..7a1866e8 100644 --- a/atticmatic/tests/unit/test_config.py +++ b/atticmatic/tests/unit/test_config.py @@ -198,7 +198,7 @@ def test_parse_section_options_for_missing_section_should_return_empty_dict(): def insert_mock_parser(): parser = flexmock() parser.should_receive('read') - module.ConfigParser = lambda: parser + module.RawConfigParser = lambda: parser return parser diff --git a/setup.py b/setup.py index 73a77961..9dc31c7f 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup, find_packages -VERSION = '0.1.6' +VERSION = '0.1.7-dev' setup(