Change in vdsm[master]: ConfigFile.py: include default section wrapping comments.

mtayer at redhat.com mtayer at redhat.com
Sun May 18 15:59:21 UTC 2014


mooli tayer has uploaded a new change for review.

Change subject: ConfigFile.py: include default section wrapping comments.
......................................................................

ConfigFile.py: include default section wrapping comments.

Since ConfigFile.py represents common operations on config files
it should also include the default comments vdsm inserts.
Only version has no default to allow different callares
have different configuration version.

Change-Id: I54b6b10d64d4a2d299e8fc783880c92c07dedd7b
Signed-off-by: Mooli Tayer <mtayer at redhat.com>
---
M lib/vdsm/tool/configfile.py
M tests/toolTests.py
2 files changed, 41 insertions(+), 29 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/20/27820/1

diff --git a/lib/vdsm/tool/configfile.py b/lib/vdsm/tool/configfile.py
index 293b43a..a3870d4 100644
--- a/lib/vdsm/tool/configfile.py
+++ b/lib/vdsm/tool/configfile.py
@@ -71,8 +71,14 @@
     sections are removed.
     """
 
-    def __init__(self, filename, sectionStart, sectionEnd, version):
-
+    def __init__(
+                self,
+                filename,
+                version,
+                sectionStart='## beginning of configuration section by vdsm',
+                sectionEnd='## end of configuration section by vdsm',
+                prefix='# VDSM backup ',
+        ):
         if not os.path.exists(filename):
             raise OSError(
                 'No such file or directory: %s' % (filename, )
@@ -82,6 +88,7 @@
         self.context = False
         self.sectionStart = sectionStart
         self.sectionEnd = sectionEnd
+        self.prefix = prefix
         self.version = version
 
     def __enter__(self):
@@ -116,10 +123,10 @@
                     if m:
                         oldentries.add(m.group('key'))
                     if self.prefixRemove:
-                        if line.startswith(self.prefixRemove):
-                            line = line[len(self.prefixRemove):]
+                        if line.startswith(self.prefix):
+                            line = line[len(self.prefix):]
                     if self.prefixAdd:
-                        line = self.prefixAdd + line
+                        line = self.prefix + line
                     oldlines.append(line)
             return oldlines, oldentries
 
@@ -186,20 +193,20 @@
         self.section = section
 
     @context
-    def prefixLines(self, prefix):
+    def prefixLines(self):
         """
-        Add 'prefix' to the beginning of each line.
+        Add self.prefix to the beginning of each line.
         No editing is done on new content added by this config file.
         """
-        self.prefixAdd = prefix
+        self.prefixAdd = True
 
     @context
-    def unprefixLines(self, prefix):
+    def unprefixLines(self):
         """
-        Remove 'prefix' from each line starting with it.
+        Remove self.prefix from each line starting with it.
         No editing is done on new content added by this config file.
         """
-        self.prefixRemove = prefix
+        self.prefixRemove = True
 
     @context
     def removeConf(self):
diff --git a/tests/toolTests.py b/tests/toolTests.py
index d0b9596..d9c6703 100644
--- a/tests/toolTests.py
+++ b/tests/toolTests.py
@@ -195,13 +195,13 @@
                        "    key2    =val2\n"
                        "#key3=val4")
         with ConfigFile(self.tname,
+                        version='3.4.4',
                         sectionStart="# start conf",
-                        sectionEnd="# end conf",
-                        version='3.4.4') as conf:
+                        sectionEnd="# end conf") as conf:
             conf.addEntry("key3", "val3")
             conf.addEntry("key2", "val3")
-        with open(self.tname, 'r') as f:
 
+        with open(self.tname, 'r') as f:
             self.assertEqual(f.read(), "key1=val1\n"
                                        "    key2    =val2\n"
                                        "#key3=val4"
@@ -214,10 +214,11 @@
                        "        weekly\n"
                        "}\n")
         with ConfigFile(self.tname,
+                        version='3.4.4',
                         sectionStart="# start conf",
                         sectionEnd="# end conf",
-                        version='3.4.4') as conf:
-                    conf.prefixLines("# comment ")
+                        prefix="# comment ") as conf:
+                    conf.prefixLines()
                     conf.prependSection("Some text to\n"
                                         "add at the top\n")
         with open(self.tname, 'r') as f:
@@ -238,30 +239,33 @@
         )
         self.writeConf(original)
         with ConfigFile(self.tname,
+                        version='3.4.4',
                         sectionStart="# start conf",
                         sectionEnd="# end conf",
-                        version='3.4.4') as conf:
-                    conf.prefixLines("# comment ")
+                        prefix="# comment ") as conf:
+                    conf.prefixLines()
         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,
+                        version='3.4.4',
                         sectionStart="# start conf",
                         sectionEnd="# end conf",
-                        version='3.4.4') as conff:
-            conff.unprefixLines("# comment ")
+                        prefix="# comment ") as conff:
+            conff.unprefixLines()
         with open(self.tname, 'r') as f:
             self.assertEqual(f.read(), original)
 
     def testRemoveEntireLinePrefix(self):
         self.writeConf("# comment\n")
         with ConfigFile(self.tname,
+                        version='3.4.4',
                         sectionStart="# start conf",
                         sectionEnd="# end conf",
-                        version='3.4.4') as conf:
-            conf.unprefixLines("# comment")
+                        prefix="# comment") as conf:
+            conf.unprefixLines()
         with open(self.tname, 'r') as f:
             self.assertEqual(f.read(), "\n")
 
@@ -273,9 +277,10 @@
                        "# end conf-text-here don't matter\n"
                        "# comment line\n")
         with ConfigFile(self.tname,
+                        version='3.4.4',
                         sectionStart="# start conf",
                         sectionEnd="# end conf",
-                        version='3.4.4') as conf:
+                        prefix="# comment") as conf:
                             conf.removeConf()
         with open(self.tname, 'r') as f:
                     self.assertEqual(f.read(), "key=val\n"
@@ -284,11 +289,11 @@
 
     def testOutOfContext(self):
         conff = ConfigFile(self.tname,
+                           version='3.4.4',
                            sectionStart="# start conf",
-                           sectionEnd="# end conf",
-                           version='3.4.4')
-        self.assertRaises(RuntimeError, conff.prefixLines, 'a')
-        self.assertRaises(RuntimeError, conff.removeConf, 'a')
+                           sectionEnd="# end conf")
+        self.assertRaises(RuntimeError, conff.prefixLines)
+        self.assertRaises(RuntimeError, conff.removeConf)
 
     def testHasConf(self):
         self.writeConf("key=val\n"
@@ -297,9 +302,9 @@
                        "all you sections are belong to us\n"
                        "# end conf-3.4.4\n")
         self.assertTrue(ConfigFile(self.tname,
+                                   version='3.4.4',
                                    sectionStart="# start conf",
-                                   sectionEnd="# end conf",
-                                   version='3.4.4').hasConf())
+                                   sectionEnd="# end conf").hasConf())
 
     def testConfRead(self):
         self.writeConf("key=val\n"


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I54b6b10d64d4a2d299e8fc783880c92c07dedd7b
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: mooli tayer <mtayer at redhat.com>


More information about the vdsm-patches mailing list