gp_ini: Allow better overriding of behaviour in inherited classes
authorGarming Sam <garming@catalyst.net.nz>
Thu, 31 May 2018 02:36:00 +0000 (14:36 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 16 Aug 2018 21:42:21 +0000 (23:42 +0200)
We will need this to parse the parameters or section names as SIDs for fdeploy1.ini

Signed-off-by: Garming Sam <garming@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/gp_parse/gp_ini.py

index 6c9b616df25c0b529fd1eff9eae526f83f98b80d..cfbf02298701e973d24c67ff3088265edf18c917 100644 (file)
@@ -38,19 +38,46 @@ class GPIniParser(GPParser):
 
         self.ini_conf.readfp(StringIO(contents.decode(self.encoding)))
 
+    def build_xml_parameter(self, section_xml, section, key_ini, val_ini):
+        child = SubElement(section_xml, 'Parameter')
+        key = SubElement(child, 'Key')
+        value = SubElement(child, 'Value')
+        key.text = key_ini
+        value.text = val_ini
+
+        return child
+
+    def load_xml_parameter(self, param_xml, section):
+        key = param_xml.find('Key').text
+        value = param_xml.find('Value').text
+        if value is None:
+            value = ''
+        self.ini_conf.set(section, key, value)
+
+        return (key, value)
+
+    def build_xml_section(self, root_xml, sec_ini):
+        section = SubElement(root_xml, 'Section')
+        section.attrib['name'] = sec_ini
+
+        return section
+
+    def load_xml_section(self, section_xml):
+        section_name = section_xml.attrib['name']
+        self.ini_conf.add_section(section_name)
+
+        return section_name
+
     def write_xml(self, filename):
         with file(filename, 'w') as f:
             root = Element('IniFile')
 
             for sec_ini in self.ini_conf.sections():
-                section = SubElement(root, 'Section')
-                section.attrib['name'] = sec_ini
+                section = self.build_xml_section(root, sec_ini)
+
                 for key_ini, val_ini in self.ini_conf.items(sec_ini, raw=True):
-                    child = SubElement(section, 'Parameter')
-                    key = SubElement(child, 'Key')
-                    value = SubElement(child, 'Value')
-                    key.text = key_ini
-                    value.text = val_ini
+                    self.build_xml_parameter(section, sec_ini, key_ini,
+                                             val_ini)
 
             self.write_pretty_xml(root, f)
 
@@ -64,15 +91,10 @@ class GPIniParser(GPParser):
         self.ini_conf.optionxform = str
 
         for s in root.findall('Section'):
-            section_name = s.attrib['name']
-            self.ini_conf.add_section(section_name)
+            section_name = self.load_xml_section(s)
 
             for param in s.findall('Parameter'):
-                key = param.find('Key').text
-                value = param.find('Value').text
-                if value is None:
-                    value = ''
-                self.ini_conf.set(section_name, key, value)
+                self.load_xml_parameter(param, section_name)
 
     def write_binary(self, filename):
         with codecs.open(filename, 'wb+', self.encoding) as f: