Changeset - b3b544769d0b
[Not reviewed]
0 2 2
Lance Edgar (lance) - 10 years ago 2014-07-12 17:34:55
lance@edbob.org
Add `rattail.config` module, move `parse_list` function there.

This function is already proving useful in other contexts besides just the
file monitor.
4 files changed with 110 insertions and 77 deletions:
0 comments (0 inline, 0 general)
rattail/config.py
Show inline comments
 
new file 100644
 
# -*- coding: utf-8 -*-
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2014 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
#  Rattail is free software: you can redistribute it and/or modify it under the
 
#  terms of the GNU Affero General Public License as published by the Free
 
#  Software Foundation, either version 3 of the License, or (at your option)
 
#  any later version.
 
#
 
#  Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
 
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
#  FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for
 
#  more details.
 
#
 
#  You should have received a copy of the GNU Affero General Public License
 
#  along with Rattail.  If not, see <http://www.gnu.org/licenses/>.
 
#
 
################################################################################
 

	
 
"""
 
Configuration Utilities
 
"""
 

	
 
import sys
 
import shlex
 

	
 

	
 
def parse_list(value):
 
    """
 
    Parse a configuration value, splitting by whitespace and/or commas and
 
    taking quoting into account, etc., yielding a list of strings.
 
    """
 
    if value is None:
 
        return []
 
    # Per the shlex docs (https://docs.python.org/2/library/shlex.html):
 
    # "Prior to Python 2.7.3, this module did not support Unicode input."
 
    if sys.version_info < (2, 7, 3) and isinstance(value, unicode): # pragma: no cover
 
        value = value.encode(u'utf-8')
 
    parser = shlex.shlex(value)
 
    parser.whitespace += u','
 
    parser.whitespace_split = True
 
    values = list(parser)
 
    for i, value in enumerate(values):
 
        if value.startswith(u'"') and value.endswith(u'"'):
 
            values[i] = value[1:-1]
 
    return values
rattail/filemon/config.py
Show inline comments
 
@@ -24,17 +24,16 @@
 
"""
 
File Monitor Configuration
 
"""
 

	
 
import os
 
import re
 
import sys
 
import shlex
 
import warnings
 
import logging
 

	
 
from rattail.config import parse_list
 
from rattail.util import load_object
 
from rattail.exceptions import ConfigurationError
 

	
 

	
 
log = logging.getLogger(__name__)
 

	
 
@@ -358,27 +357,6 @@ def load_legacy_profiles(config):
 
        elif not profile.actions:
 
            log.warning(u"profile has no actions to perform, "
 
                        u"and will be pruned: {0}".format(key))
 
            del monitored[key]
 

	
 
    return monitored
 

	
 

	
 
def parse_list(value):
 
    """
 
    Parse a configuration value, splitting by whitespace and/or commas and
 
    taking quoting into account, etc., yielding a list of strings.
 
    """
 
    if value is None:
 
        return []
 
    # Per the shlex docs (https://docs.python.org/2/library/shlex.html):
 
    # "Prior to Python 2.7.3, this module did not support Unicode input."
 
    if sys.version_info < (2, 7, 3) and isinstance(value, unicode):
 
        value = value.encode(u'utf-8')
 
    parser = shlex.shlex(value)
 
    parser.whitespace += u','
 
    parser.whitespace_split = True
 
    values = list(parser)
 
    for i, value in enumerate(values):
 
        if value.startswith(u'"') and value.endswith(u'"'):
 
            values[i] = value[1:-1]
 
    return values
tests/filemon/test_config.py
Show inline comments
 
@@ -246,60 +246,6 @@ class TestLoadLegacyProfiles(TestCase):
 
        monitored = config.load_legacy_profiles(self.config)
 
        self.assertEqual(len(monitored), 2)
 
        # remove foo's actions
 
        self.config.remove_option(u'rattail.filemon', u'foo.actions')
 
        monitored = config.load_legacy_profiles(self.config)
 
        self.assertEqual(len(monitored), 1)
 

	
 

	
 
class TestParseList(TestCase):
 

	
 
    def test_none(self):
 
        value = config.parse_list(None)
 
        self.assertEqual(len(value), 0)
 

	
 
    def test_single_value(self):
 
        value = config.parse_list(u'foo')
 
        self.assertEqual(len(value), 1)
 
        self.assertEqual(value[0], u'foo')
 

	
 
    def test_single_value_padded_by_spaces(self):
 
        value = config.parse_list(u'   foo   ')
 
        self.assertEqual(len(value), 1)
 
        self.assertEqual(value[0], u'foo')
 

	
 
    def test_slash_is_not_a_separator(self):
 
        value = config.parse_list(u'/dev/null')
 
        self.assertEqual(len(value), 1)
 
        self.assertEqual(value[0], u'/dev/null')
 

	
 
    def test_multiple_values_separated_by_whitespace(self):
 
        value = config.parse_list(u'foo bar baz')
 
        self.assertEqual(len(value), 3)
 
        self.assertEqual(value[0], u'foo')
 
        self.assertEqual(value[1], u'bar')
 
        self.assertEqual(value[2], u'baz')
 

	
 
    def test_multiple_values_separated_by_commas(self):
 
        value = config.parse_list(u'foo,bar,baz')
 
        self.assertEqual(len(value), 3)
 
        self.assertEqual(value[0], u'foo')
 
        self.assertEqual(value[1], u'bar')
 
        self.assertEqual(value[2], u'baz')
 

	
 
    def test_multiple_values_separated_by_whitespace_and_commas(self):
 
        value = config.parse_list(u'  foo,   bar   baz')
 
        self.assertEqual(len(value), 3)
 
        self.assertEqual(value[0], u'foo')
 
        self.assertEqual(value[1], u'bar')
 
        self.assertEqual(value[2], u'baz')
 

	
 
    def test_multiple_values_separated_by_whitespace_and_commas_with_some_quoting(self):
 
        value = config.parse_list(u"""
 
        foo
 
        "C:\\some path\\with spaces\\and, a comma",
 
        baz
 
""")
 
        self.assertEqual(len(value), 3)
 
        self.assertEqual(value[0], u'foo')
 
        self.assertEqual(value[1], u'C:\\some path\\with spaces\\and, a comma')
 
        self.assertEqual(value[2], u'baz')
tests/test_config.py
Show inline comments
 
new file 100644
 
# -*- coding: utf-8 -*-
 

	
 
from unittest import TestCase
 

	
 
from rattail import config
 

	
 

	
 
class TestParseList(TestCase):
 

	
 
    def test_none(self):
 
        value = config.parse_list(None)
 
        self.assertEqual(len(value), 0)
 

	
 
    def test_single_value(self):
 
        value = config.parse_list(u'foo')
 
        self.assertEqual(len(value), 1)
 
        self.assertEqual(value[0], u'foo')
 

	
 
    def test_single_value_padded_by_spaces(self):
 
        value = config.parse_list(u'   foo   ')
 
        self.assertEqual(len(value), 1)
 
        self.assertEqual(value[0], u'foo')
 

	
 
    def test_slash_is_not_a_separator(self):
 
        value = config.parse_list(u'/dev/null')
 
        self.assertEqual(len(value), 1)
 
        self.assertEqual(value[0], u'/dev/null')
 

	
 
    def test_multiple_values_separated_by_whitespace(self):
 
        value = config.parse_list(u'foo bar baz')
 
        self.assertEqual(len(value), 3)
 
        self.assertEqual(value[0], u'foo')
 
        self.assertEqual(value[1], u'bar')
 
        self.assertEqual(value[2], u'baz')
 

	
 
    def test_multiple_values_separated_by_commas(self):
 
        value = config.parse_list(u'foo,bar,baz')
 
        self.assertEqual(len(value), 3)
 
        self.assertEqual(value[0], u'foo')
 
        self.assertEqual(value[1], u'bar')
 
        self.assertEqual(value[2], u'baz')
 

	
 
    def test_multiple_values_separated_by_whitespace_and_commas(self):
 
        value = config.parse_list(u'  foo,   bar   baz')
 
        self.assertEqual(len(value), 3)
 
        self.assertEqual(value[0], u'foo')
 
        self.assertEqual(value[1], u'bar')
 
        self.assertEqual(value[2], u'baz')
 

	
 
    def test_multiple_values_separated_by_whitespace_and_commas_with_some_quoting(self):
 
        value = config.parse_list(u"""
 
        foo
 
        "C:\\some path\\with spaces\\and, a comma",
 
        baz
 
""")
 
        self.assertEqual(len(value), 3)
 
        self.assertEqual(value[0], u'foo')
 
        self.assertEqual(value[1], u'C:\\some path\\with spaces\\and, a comma')
 
        self.assertEqual(value[2], u'baz')
0 comments (0 inline, 0 general)