mooli tayer has uploaded a new change for review.

Change subject: vdsm-tool: add unprefixLines() to configfile.py.
......................................................................

vdsm-tool: add unprefixLines() to configfile.py.

new method removes 'prefix' from each line starting with it.
No editing is done on new content added by this config file.

changed doc string of prefixLines to reflect the link
between the two methods.

tests included (see testPrefixIdempotencey in toolTests.py).

unprefixLines is the reverse function of prefixLines so that
unprefixLines(prefixLines(file)) = file.

Change-Id: Idd869dced51f1e67c9fb1bb264424b3758cc54f0
Signed-off-by: Mooli Tayer <[email protected]>
---
M lib/vdsm/tool/configfile.py
M tests/toolTests.py
2 files changed, 53 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/37/27737/1

diff --git a/lib/vdsm/tool/configfile.py b/lib/vdsm/tool/configfile.py
index 4a16a95..f65f2d8 100644
--- a/lib/vdsm/tool/configfile.py
+++ b/lib/vdsm/tool/configfile.py
@@ -73,7 +73,8 @@
             raise RuntimeError("can only enter once")
         self.entries = {}
         self.context = True
-        self.prefix = None
+        self.prefixRemove = None
+        self.prefixAdd = None
         self.section = None
         self.oldmod = os.stat(self.filename).st_mode
         self.remove = None
@@ -98,8 +99,11 @@
                     m = confpat.match(line.rstrip())
                     if m:
                         oldentries.add(m.group('key'))
-                    if self.prefix:
-                        line = self.prefix + line
+                    if self.prefixRemove:
+                        if line.startswith(self.prefixRemove):
+                            line = line[len(self.prefixRemove):]
+                    if self.prefixAdd:
+                        line = self.prefixAdd + line
                     oldlines.append(line)
             return oldlines, oldentries
 
@@ -166,10 +170,18 @@
     @context
     def prefixLines(self, prefix):
         """
-        prefix each line originaly included in the file (not including
-         'prependedSection' lines) with 'prefix'.
+        Add 'prefix' to the beginning of each line.
+        No editing is done on new content added by this config file.
         """
-        self.prefix = prefix
+        self.prefixAdd = prefix
+
+    @context
+    def unprefixLines(self, prefix):
+        """
+        Remove 'prefix' from each line starting with it.
+        No editing is done on new content added by this config file.
+        """
+        self.prefixRemove = prefix
 
     @context
     def removeConf(self):
diff --git a/tests/toolTests.py b/tests/toolTests.py
index fdd2e45..d0b9596 100644
--- a/tests/toolTests.py
+++ b/tests/toolTests.py
@@ -230,6 +230,41 @@
                              "# comment         weekly\n"
                              "# comment }\n")
 
+    def testPrefixIdempotencey(self):
+        original = (
+            "/var/log/libvirt/libvirtd.log {\n"
+            "        weekly\n"
+            "}\n"
+        )
+        self.writeConf(original)
+        with ConfigFile(self.tname,
+                        sectionStart="# start conf",
+                        sectionEnd="# end conf",
+                        version='3.4.4') as conf:
+                    conf.prefixLines("# comment ")
+        with open(self.tname, 'r') as f:
+            self.assertEqual(f.read(),
+                             "# comment /var/log/libvirt/libvirtd.log {\n"
+                             "# comment         weekly\n"
+                             "# comment }\n")
+        with ConfigFile(self.tname,
+                        sectionStart="# start conf",
+                        sectionEnd="# end conf",
+                        version='3.4.4') as conff:
+            conff.unprefixLines("# comment ")
+        with open(self.tname, 'r') as f:
+            self.assertEqual(f.read(), original)
+
+    def testRemoveEntireLinePrefix(self):
+        self.writeConf("# comment\n")
+        with ConfigFile(self.tname,
+                        sectionStart="# start conf",
+                        sectionEnd="# end conf",
+                        version='3.4.4') as conf:
+            conf.unprefixLines("# comment")
+        with open(self.tname, 'r') as f:
+            self.assertEqual(f.read(), "\n")
+
     def testRemoveConfSection(self):
         self.writeConf("key=val\n"
                        "key=val\n"


-- 
To view, visit http://gerrit.ovirt.org/27737
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idd869dced51f1e67c9fb1bb264424b3758cc54f0
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: mooli tayer <[email protected]>
_______________________________________________
vdsm-patches mailing list
[email protected]
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to