diff --git a/rattail/config.py b/rattail/config.py new file mode 100644 index 0000000000000000000000000000000000000000..10c54c127c2a30ec6391601f896127167ebd5d20 --- /dev/null +++ b/rattail/config.py @@ -0,0 +1,50 @@ +# -*- 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 . +# +################################################################################ + +""" +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 diff --git a/rattail/filemon/config.py b/rattail/filemon/config.py index 6f56ae9af128179e84912d0a345db8d1c8da6a78..e3c6f27240f94b95734bb2cd994dc2408baa658a 100644 --- a/rattail/filemon/config.py +++ b/rattail/filemon/config.py @@ -27,11 +27,10 @@ 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 @@ -361,24 +360,3 @@ def load_legacy_profiles(config): 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 diff --git a/tests/filemon/test_config.py b/tests/filemon/test_config.py index 1434fe034314d8e8b6a7fcd3ec536377884f6a27..da3f68e18cbbffae288fafd103fcea965a4421e0 100644 --- a/tests/filemon/test_config.py +++ b/tests/filemon/test_config.py @@ -249,57 +249,3 @@ class TestLoadLegacyProfiles(TestCase): 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') diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000000000000000000000000000000000000..b5fd5a5c342216a84a7a2c4ff116f60292788b0f --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,59 @@ +# -*- 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')