#11: Fixed parsing of punctuation in configuration file.

This commit is contained in:
Dan Helfman 2015-09-06 15:33:56 -07:00
parent d86faa30d3
commit f54acc9bbf
5 changed files with 40 additions and 7 deletions

4
NEWS
View File

@ -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.

View File

@ -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)

View File

@ -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),
)
)

View File

@ -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

View File

@ -1,7 +1,7 @@
from setuptools import setup, find_packages
VERSION = '0.1.6'
VERSION = '0.1.7-dev'
setup(