Browse Source

add uncomment function

Sean Dague 2 years ago
parent
commit
79f8f551c3
2 changed files with 121 additions and 2 deletions
  1. 8
    2
      devstack/dsconf.py
  2. 113
    0
      devstack/tests/test_ini_uncomment.py

+ 8
- 2
devstack/dsconf.py View File

@@ -64,7 +64,7 @@ class IniFile(object):
64 64
                     writer.write("[%s]\n" % section)
65 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 68
         temp = tempfile.NamedTemporaryFile(mode='r')
69 69
         shutil.copyfile(self.fname, temp.name)
70 70
         current_section = ""
@@ -75,7 +75,7 @@ class IniFile(object):
75 75
                     if m:
76 76
                         current_section = m.group(1)
77 77
                     if current_section == section:
78
-                        if re.match("%s\s*\=" % name, line):
78
+                        if re.match(match % name, line):
79 79
                             # run function with writer and found line
80 80
                             func(writer, line)
81 81
                         else:
@@ -98,6 +98,12 @@ class IniFile(object):
98 98
 
99 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 107
     def set(self, section, name, value):
102 108
         def _do_set(writer, line):
103 109
             writer.write("%s = %s\n" % (name, value))

+ 113
- 0
devstack/tests/test_ini_uncomment.py View File

@@ -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
+
19
+import fixtures
20
+import testtools
21
+
22
+from devstack import dsconf
23
+
24
+
25
+BASIC = """[default]
26
+a = b
27
+c = d
28
+# f = 1
29
+[second]
30
+e = f
31
+g = h
32
+# h = 2
33
+[new]
34
+s = t
35
+# x = 3
36
+"""
37
+
38
+RESULT1 = """[default]
39
+a = b
40
+c = d
41
+f = 1
42
+[second]
43
+e = f
44
+g = h
45
+# h = 2
46
+[new]
47
+s = t
48
+# x = 3
49
+"""
50
+
51
+RESULT2 = """[default]
52
+a = b
53
+c = d
54
+# f = 1
55
+[second]
56
+e = f
57
+g = h
58
+h = 2
59
+[new]
60
+s = t
61
+# x = 3
62
+"""
63
+
64
+RESULT3 = """[default]
65
+a = b
66
+c = d
67
+# f = 1
68
+[second]
69
+e = f
70
+g = h
71
+# h = 2
72
+[new]
73
+s = t
74
+x = 3
75
+"""
76
+
77
+
78
+class 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)

Loading…
Cancel
Save