gp_parse: Introduce new module for parsing GPO files
authorGarming Sam <garming@catalyst.net.nz>
Wed, 23 May 2018 00:39:02 +0000 (12:39 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 16 Aug 2018 21:42:20 +0000 (23:42 +0200)
This is the default parser which will cause the file to be restored
as-is -- leaving only an effectively blank XML file as a placeholder.

Signed-off-by: Garming Sam <garming@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/gp_parse/__init__.py [new file with mode: 0644]

diff --git a/python/samba/gp_parse/__init__.py b/python/samba/gp_parse/__init__.py
new file mode 100644 (file)
index 0000000..a8beb87
--- /dev/null
@@ -0,0 +1,57 @@
+# GPO Parser for generic extensions
+#
+# Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
+# Written by Garming Sam <garming@catalyst.net.nz>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+from xml.dom import minidom
+from io import BytesIO
+from xml.etree.ElementTree import ElementTree
+
+class GPNoParserException(Exception):
+    pass
+
+# [MS-GPIPSEC] (LDAP)
+# [MS-GPDPC] Deployed Printer Connections (LDAP)
+# [MS-GPPREF] Preferences Extension (XML)
+# [MS-GPWL] Wireless/Wired Protocol Extension (LDAP)
+class GPParser(object):
+    encoding = 'utf-16'
+    output_encoding = 'utf-8'
+
+    def parse(self, contents):
+        pass
+
+    def write_xml(self, filename):
+        with file(filename, 'w') as f:
+            f.write('<?xml version="1.0" encoding="utf-8"?><UnknownFile/>')
+
+    def load_xml(self, filename):
+        pass
+
+    def write_binary(self, filename):
+        raise GPNoParserException("This file has no parser available.")
+
+    def write_pretty_xml(self, xml_element, handle):
+        # Add the xml header as well as format it nicely.
+        # ElementTree doesn't have a pretty-print, so use minidom.
+
+        et = ElementTree(xml_element)
+        temporary_bytes = BytesIO()
+        et.write(temporary_bytes, encoding=self.output_encoding,
+                 xml_declaration=True)
+        minidom_parsed = minidom.parseString(temporary_bytes.getvalue())
+        handle.write(minidom_parsed.toprettyxml(encoding=self.output_encoding))