Exemplo n.º 1
0
 def testPrefixIdempotencey(self):
     original = (
         "/var/log/libvirt/libvirtd.log {\n"
         "        weekly\n"
         "}\n"
     )
     self._writeConf(original)
     with ConfigFile(self.tname,
                     version='3.4.4',
                     sectionStart="# start conf",
                     sectionEnd="# end conf",
                     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",
                     prefix="# comment ") as conff:
         conff.unprefixLines()
     with open(self.tname, 'r') as f:
         self.assertEqual(f.read(), original)
Exemplo n.º 2
0
 def testOutOfContext(self):
     conff = ConfigFile(self.tname,
                        version='3.4.4',
                        sectionStart="# start conf",
                        sectionEnd="# end conf")
     self.assertRaises(RuntimeError, conff.prefixLines)
     self.assertRaises(RuntimeError, conff.removeConf)
Exemplo n.º 3
0
 def testRemoveEntireLinePrefix(self):
     self._writeConf("# comment\n")
     with ConfigFile(self.tname,
                     version='3.4.4',
                     sectionStart="# start conf",
                     sectionEnd="# end conf",
                     prefix="# comment") as conf:
         conf.unprefixLines()
     with open(self.tname, 'r') as f:
         self.assertEqual(f.read(), "\n")
Exemplo n.º 4
0
 def testHasConf(self):
     self._writeConf("key=val\n"
                     "kay=val\n"
                     "# start conf-3.4.4\n"
                     "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").hasConf())
Exemplo n.º 5
0
    def testEncoding(self):
        self._writeConf("")
        with ConfigFile(self.tname,
                        version='3.4.4',
                        sectionStart="# start conf",
                        sectionEnd="# end conf") as conf:
            conf.addEntry("key1", "\xd7\x99\xd7\xa0\xd7\x99\xd7\x91")

        with io.open(self.tname, 'r', encoding='utf8') as f:
            self.assertEqual(
                f.read(), "# start conf-3.4.4\n"
                "key1=\xd7\x99\xd7\xa0\xd7\x99\xd7\x91\n"
                "# end conf-3.4.4\n")
Exemplo n.º 6
0
    def testAddCommentedoutConf(self):
        self._writeConf("key1=val1\n" "#key3=val4")
        with ConfigFile(self.tname,
                        version='3.4.4',
                        sectionStart="# start conf",
                        sectionEnd="# end conf") as conf:
            conf.addEntry("key3", "val3")
            conf.addEntry("key2", "val3")

        with open(self.tname, 'r') as f:
            self.assertEqual(
                f.read(), "key1=val1\n"
                "#key3=val4"
                "# start conf-3.4.4\n"
                "key2=val3\n"
                "key3=val3\n"
                "# end conf-3.4.4\n")
Exemplo n.º 7
0
 def testRemoveConfSection(self):
     self._writeConf("key=val\n"
                     "remove me!(see 'Backward compatibility')# by vdsm\n"
                     "key=val\n"
                     "# start conf-text-here don't matter\n"
                     "all you sections are belong to us\n"
                     "# 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",
                     prefix="# comment") as conf:
         conf.removeConf()
     with open(self.tname, 'r') as f:
         self.assertEqual(f.read(), "key=val\n"
                          "key=val\n"
                          "# comment line\n")
Exemplo n.º 8
0
    def testSort(self):
        self._writeConf("")
        with ConfigFile(self.tname,
                        version='3.4.4',
                        sectionStart="# start conf",
                        sectionEnd="# end conf") as conf:
            conf.addEntry("key3", "val")
            conf.addEntry("key2", "val")
            conf.addEntry("key1", "val")
            conf.addEntry("key4", "val")

        with open(self.tname, 'r') as f:
            self.assertEqual(f.read(), "# start conf-3.4.4\n"
                                       "key1=val\n"
                                       "key2=val\n"
                                       "key3=val\n"
                                       "key4=val\n"
                                       "# end conf-3.4.4\n")
Exemplo n.º 9
0
 def testPrefixAndPrepend(self):
     self._writeConf("/var/log/libvirt/libvirtd.log {\n"
                     "        weekly\n"
                     "}\n")
     with ConfigFile(self.tname,
                     version='3.4.4',
                     sectionStart="# start conf",
                     sectionEnd="# end conf",
                     prefix="# comment ") as conf:
         conf.prefixLines()
         conf.prependSection(u"Some text to\n" "add at the top\n")
     with open(self.tname, 'r') as f:
         self.assertEqual(
             f.read(), "# start conf-3.4.4\n"
             "Some text to\n"
             "add at the top\n"
             "# end conf-3.4.4\n"
             "# comment /var/log/libvirt/libvirtd.log {\n"
             "# comment         weekly\n"
             "# comment }\n")
Exemplo n.º 10
0
def open_config(path, conf_version):
    return ConfigFile(path, conf_version)
Exemplo n.º 11
0
def _openConfig(path):
    return ConfigFile(path, CONF_VERSION)