[master/f21] Fix # handling in SimpleConfigFile (#1045687)
Brian C. Lane
bcl at redhat.com
Mon Oct 13 21:16:53 UTC 2014
It was treating anything after a # as a comment, even if it appeared
inside a quoted string. This fixes it to find the last # outside a
(optional) quoted string and adds several tests.
This also adds back the non-IfcfgFile tests for SimpleConfigFile from
old_tests
---
pyanaconda/simpleconfig.py | 30 ++++++-
tests/pyanaconda_tests/simpleconfig_test.py | 128 ++++++++++++++++++++++++++++
2 files changed, 154 insertions(+), 4 deletions(-)
create mode 100644 tests/pyanaconda_tests/simpleconfig_test.py
diff --git a/pyanaconda/simpleconfig.py b/pyanaconda/simpleconfig.py
index a148384..41b7978 100644
--- a/pyanaconda/simpleconfig.py
+++ b/pyanaconda/simpleconfig.py
@@ -43,6 +43,20 @@ def quote(s, always=False):
return s
return '"'+s.replace('"', '\\"')+'"'
+def find_comment(s):
+ """ Look for a # comment outside of a quoted string.
+ If there are no quotes, find the last # in the string.
+
+ :param str s: string to check for comment and quotes
+ :returns: index of last quote, index of last hash
+ :rtype: tuple
+
+ Returns a tuple of indexes, which may be -1 if it wasn't found.
+ """
+ last_quote = max(i for i in map(s.rfind, "'\""))
+ last_hash = s.rfind("#")
+ return (last_quote, last_hash)
+
class SimpleConfigFile(object):
""" Edit values in a configuration file without changing comments.
Supports KEY=VALUE lines and ignores everything else.
@@ -122,10 +136,17 @@ class SimpleConfigFile(object):
key is always UPPERCASE
"""
s = line.strip()
- if '#' in s:
- s = s[:s.find('#')] # remove from comment to EOL
+ if s.startswith("#"):
+ return (None, None)
+
+ # Look for a # outside any quotes
+ last_quote, last_hash = find_comment(s)
+ if last_hash > last_quote:
+ s = s[:last_hash] # remove from comment to EOL
s = s.strip() # and any unnecessary whitespace
+
key, eq, val = s.partition('=')
+ key = key.strip()
if self.read_unquote:
val = unquote(val)
if key != '' and eq == '=':
@@ -153,8 +174,9 @@ class SimpleConfigFile(object):
if key not in self.info:
continue
oldkeys.append(key)
- if "#" in line:
- comment = " " + line[line.find("#"):]
+ last_quote, last_hash = find_comment(line)
+ if last_hash > last_quote:
+ comment = " " + line.strip()[last_hash:]
else:
comment = ""
s += self._kvpair(key, comment)
diff --git a/tests/pyanaconda_tests/simpleconfig_test.py b/tests/pyanaconda_tests/simpleconfig_test.py
new file mode 100644
index 0000000..fbf43a1
--- /dev/null
+++ b/tests/pyanaconda_tests/simpleconfig_test.py
@@ -0,0 +1,128 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2014 Red Hat, Inc.
+#
+# This copyrighted material is made available to anyone wishing to use,
+# modify, copy, or redistribute it subject to the terms and conditions of
+# the GNU General Public License v.2, or (at your option) any later version.
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY expressed or implied, including the implied warranties of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+# Public License for more details. You should have received a copy of the
+# GNU General Public License along with this program; if not, write to the
+# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
+# source code or documentation are not subject to the GNU General Public
+# License and may only be used or replicated with the express permission of
+# Red Hat, Inc.
+#
+# Red Hat Author(s): Brian C. Lane <bcl at redhat.com>
+
+from pyanaconda.simpleconfig import SimpleConfigFile
+from pyanaconda import simpleconfig
+import unittest
+import tempfile
+
+class SimpleConfigTests(unittest.TestCase):
+ TEST_CONFIG = """ESSID="Example Network #1"
+ESSID2="Network #2" # With a comment
+COMMENT="Save this string" # Strip this comment
+#SKIP=Skip this commented line
+BOOT=always
+"""
+
+ def comment_test(self):
+ with tempfile.NamedTemporaryFile() as testconfig:
+ testconfig.write(self.TEST_CONFIG)
+ testconfig.flush()
+
+ config = SimpleConfigFile(testconfig.name)
+ config.read()
+ self.assertEqual(config.get("ESSID"), "Example Network #1")
+ self.assertEqual(config.get("ESSID2"), "Network #2")
+ self.assertEqual(config.get("COMMENT"), "Save this string")
+ self.assertEqual(str(config), self.TEST_CONFIG)
+
+ def unquote_test(self):
+ self.assertEqual(simpleconfig.unquote("plain string"), "plain string")
+ self.assertEqual(simpleconfig.unquote('"double quote"'), "double quote")
+ self.assertEqual(simpleconfig.unquote("'single quote'"), "single quote")
+
+ def quote_test(self):
+ self.assertEqual(simpleconfig.quote("nospaces"), "nospaces")
+ self.assertEqual(simpleconfig.quote("plain string"), '"plain string"')
+ self.assertEqual(simpleconfig.quote("alwaysquote", always=True), '"alwaysquote"')
+
+ def set_and_get_test(self):
+ """Setting and getting values"""
+ scf = SimpleConfigFile()
+ scf.set(('key1', 'value1'))
+ self.assertEqual(scf.get('key1'), 'value1')
+ scf.set(('KEY2', 'value2'))
+ self.assertEqual(scf.get('key2'), 'value2')
+ scf.set(('KEY3', 'value3'))
+ self.assertEqual(scf.get('KEY3'), 'value3')
+ scf.set(('key4', 'value4'))
+ self.assertEqual(scf.get('KEY4'), 'value4')
+
+ def unset_test(self):
+ scf = SimpleConfigFile()
+ scf.set(('key1', 'value1'))
+ scf.unset(('key1'))
+ self.assertEqual(scf.get('key1'), '')
+
+ def write_test(self):
+ with tempfile.NamedTemporaryFile() as testconfig:
+ scf = SimpleConfigFile()
+ scf.set(('key1', 'value1'))
+ scf.write(testconfig.name)
+ testconfig.flush()
+ self.assertEqual(open(testconfig.name).read(), 'KEY1=value1\n')
+
+ def read_test(self):
+ with tempfile.NamedTemporaryFile() as testconfig:
+ scf = SimpleConfigFile()
+ open(testconfig.name, 'w').write('KEY1="value1"\n')
+ testconfig.flush()
+ scf.read(testconfig.name)
+ self.assertEqual(scf.get('key1'), 'value1')
+
+ def read_write_test(self):
+ with tempfile.NamedTemporaryFile() as testconfig:
+ testconfig.write(self.TEST_CONFIG)
+ testconfig.flush()
+
+ scf = SimpleConfigFile()
+ scf.read(testconfig.name)
+ scf.write(testconfig.name)
+ testconfig.flush()
+ self.assertEqual(open(testconfig.name).read(), self.TEST_CONFIG)
+
+ def write_new_keys_test(self):
+ with tempfile.NamedTemporaryFile() as testconfig:
+ testconfig.write(self.TEST_CONFIG)
+ testconfig.flush()
+
+ scf = SimpleConfigFile()
+ scf.read(testconfig.name)
+ scf.set(("key1", "value1"))
+ scf.write(testconfig.name)
+ testconfig.flush()
+
+ self.assertEqual(open(testconfig.name).read(),
+ self.TEST_CONFIG+"KEY1=value1\n")
+
+ def remove_key_test(self):
+ with tempfile.NamedTemporaryFile() as testconfig:
+ testconfig.write(self.TEST_CONFIG)
+ testconfig.flush()
+
+ scf = SimpleConfigFile()
+ scf.read(testconfig.name)
+ self.assertEqual(scf.get("BOOT"), "always")
+ scf.unset("BOOT")
+ scf.write(testconfig.name)
+ testconfig.flush()
+ scf.reset()
+ scf.read(testconfig.name)
+ self.assertEqual(scf.get("BOOT"), "")
--
1.9.3
More information about the anaconda-patches
mailing list