summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean Dague <sean@dague.net>2017-01-16 10:18:53 -0500
committerSean Dague <sean@dague.net>2017-01-16 10:18:53 -0500
commit79f8f551c3925933203b3079d85b6aca658c51be (patch)
tree4af5d946a41426d2e6a93df1a529dd13daaa6b02
parent6971e39e94c3a16d62d55eddf764cd13d41d6682 (diff)
add uncomment function
-rw-r--r--devstack/dsconf.py10
-rw-r--r--devstack/tests/test_ini_uncomment.py113
2 files changed, 121 insertions, 2 deletions
diff --git a/devstack/dsconf.py b/devstack/dsconf.py
index f9ba066..597fe9b 100644
--- a/devstack/dsconf.py
+++ b/devstack/dsconf.py
@@ -64,7 +64,7 @@ class IniFile(object):
64 writer.write("[%s]\n" % section) 64 writer.write("[%s]\n" % section)
65 writer.write("%s = %s\n" % (name, value)) 65 writer.write("%s = %s\n" % (name, value))
66 66
67 def _at_existing_key(self, section, name, func): 67 def _at_existing_key(self, section, name, func, match="%s\s*\="):
68 temp = tempfile.NamedTemporaryFile(mode='r') 68 temp = tempfile.NamedTemporaryFile(mode='r')
69 shutil.copyfile(self.fname, temp.name) 69 shutil.copyfile(self.fname, temp.name)
70 current_section = "" 70 current_section = ""
@@ -75,7 +75,7 @@ class IniFile(object):
75 if m: 75 if m:
76 current_section = m.group(1) 76 current_section = m.group(1)
77 if current_section == section: 77 if current_section == section:
78 if re.match("%s\s*\=" % name, line): 78 if re.match(match % name, line):
79 # run function with writer and found line 79 # run function with writer and found line
80 func(writer, line) 80 func(writer, line)
81 else: 81 else:
@@ -98,6 +98,12 @@ class IniFile(object):
98 98
99 self._at_existing_key(section, name, _do_comment) 99 self._at_existing_key(section, name, _do_comment)
100 100
101 def uncomment(self, section, name):
102 def _do_uncomment(writer, line):
103 writer.write(re.sub("^#\s*", "", line))
104
105 self._at_existing_key(section, name, _do_uncomment, match="#\s*%s\s*\=")
106
101 def set(self, section, name, value): 107 def set(self, section, name, value):
102 def _do_set(writer, line): 108 def _do_set(writer, line):
103 writer.write("%s = %s\n" % (name, value)) 109 writer.write("%s = %s\n" % (name, value))
diff --git a/devstack/tests/test_ini_uncomment.py b/devstack/tests/test_ini_uncomment.py
new file mode 100644
index 0000000..9064762
--- /dev/null
+++ b/devstack/tests/test_ini_uncomment.py
@@ -0,0 +1,113 @@
1# Copyright 2017 IBM
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15# Implementation of ini add / remove for devstack. We don't use the
16# python ConfigFile parser because that ends up rewriting the entire
17# file and doesn't ensure comments remain.
18
19import fixtures
20import testtools
21
22from devstack import dsconf
23
24
25BASIC = """[default]
26a = b
27c = d
28# f = 1
29[second]
30e = f
31g = h
32# h = 2
33[new]
34s = t
35# x = 3
36"""
37
38RESULT1 = """[default]
39a = b
40c = d
41f = 1
42[second]
43e = f
44g = h
45# h = 2
46[new]
47s = t
48# x = 3
49"""
50
51RESULT2 = """[default]
52a = b
53c = d
54# f = 1
55[second]
56e = f
57g = h
58h = 2
59[new]
60s = t
61# x = 3
62"""
63
64RESULT3 = """[default]
65a = b
66c = d
67# f = 1
68[second]
69e = f
70g = h
71# h = 2
72[new]
73s = t
74x = 3
75"""
76
77
78class TestIniUncomment(testtools.TestCase):
79
80 def setUp(self):
81 super(TestIniUncomment, self).setUp()
82 self._path = self.useFixture(fixtures.TempDir()).path
83 self._path += "/test.ini"
84 with open(self._path, "w") as f:
85 f.write(BASIC)
86
87 def test_uncomment_ini_default(self):
88 conf = dsconf.IniFile(self._path)
89 conf.uncomment("default", "f")
90 with open(self._path) as f:
91 content = f.read()
92 self.assertEqual(content, RESULT1)
93
94 def test_uncomment_ini_second(self):
95 conf = dsconf.IniFile(self._path)
96 conf.uncomment("second", "h")
97 with open(self._path) as f:
98 content = f.read()
99 self.assertEqual(content, RESULT2)
100
101 def test_uncomment_ini_new(self):
102 conf = dsconf.IniFile(self._path)
103 conf.uncomment("new", "x")
104 with open(self._path) as f:
105 content = f.read()
106 self.assertEqual(content, RESULT3)
107
108 def test_uncomment_ini_none(self):
109 conf = dsconf.IniFile(self._path)
110 conf.uncomment("default", "s")
111 with open(self._path) as f:
112 content = f.read()
113 self.assertEqual(content, BASIC)