[glibmm/glibmm-gir-gmmproc: 1/4] gmmproc: generating enums defs using gobject-introspection file



commit 480b7b26e61e4aa9c1117fcbe2e6b7491fcd95fc
Author: Marcin Kolny <marcin kolny gmail com>
Date:   Fri Oct 3 20:31:39 2014 +0200

    gmmproc: generating enums defs using gobject-introspection file
    
        * tools/defs_gen/gir-defs-generator.py: entry point of defs generator.
        * tools/defs_gen/girparser.py: main gir file parser, enumeration
          parser.
        * tools/defs_gen/girprinter.py: defs printer class.

 tools/defs_gen/gir-defs-generator.py |   34 ++++++++++++++++++++++++++
 tools/defs_gen/girparser.py          |   43 ++++++++++++++++++++++++++++++++++
 tools/defs_gen/girprinter.py         |   26 ++++++++++++++++++++
 3 files changed, 103 insertions(+), 0 deletions(-)
---
diff --git a/tools/defs_gen/gir-defs-generator.py b/tools/defs_gen/gir-defs-generator.py
new file mode 100755
index 0000000..7203b47
--- /dev/null
+++ b/tools/defs_gen/gir-defs-generator.py
@@ -0,0 +1,34 @@
+#!/usr/bin/env python
+
+import sys
+
+from girparser import GirParser
+from girprinter import GirPrinter
+
+# Possible types:
+# function
+# callback
+# union
+# constant
+# alias
+# class
+# record
+# interface
+# bitfield
+# enumeration
+
+
+def main(args):
+    if len(args) < 2:
+        print('Must specify one input filename')
+        return -1
+
+    parser = GirParser()
+    parser.parse_file(args[1])
+    printer = GirPrinter(parser)
+
+    printer.print_enumerations()
+
+
+if __name__ == '__main__':
+    sys.exit(main(sys.argv))
diff --git a/tools/defs_gen/girparser.py b/tools/defs_gen/girparser.py
new file mode 100644
index 0000000..2360d03
--- /dev/null
+++ b/tools/defs_gen/girparser.py
@@ -0,0 +1,43 @@
+import xml.dom.minidom
+
+
+class EnumMember:
+    def __init__(self, name, c_name, value):
+        self.name = name
+        self.c_name = c_name
+        self.value = value
+
+
+class EnumObject:
+    def __init__(self, xml_root, module):
+        self.type_name = xml_root.getAttribute("name")
+        self.is_flag = self.type_name.endswith("Flags")
+        self.c_type_name = xml_root.getAttribute("c:type")
+        self.module = module
+        self.members = []
+
+        for value in xml_root.childNodes:
+            if value.nodeType == xml.dom.Node.ELEMENT_NODE and value.tagName == "member":
+                self.members.append(
+                    EnumMember(
+                        value.getAttribute("name"),
+                        value.getAttribute("c:identifier"),
+                        value.getAttribute("value")))
+
+
+class GirParser:
+    def __init__(self):
+        self._enums = []
+
+    def parse_file(self, gir_file):
+        print("trying to parse", gir_file)
+        document = xml.dom.minidom.parse(gir_file)
+        for namespace in document.documentElement.getElementsByTagName('namespace'):
+            module = namespace.getAttribute("c:identifier-prefixes")
+            for element in namespace.childNodes:
+                if element.nodeType == xml.dom.Node.ELEMENT_NODE and \
+                        (element.tagName == "enumeration" or element.tagName == "bitfield"):
+                    self._enums.append(EnumObject(element, module))
+
+    def get_enumerations(self):
+        return self._enums
diff --git a/tools/defs_gen/girprinter.py b/tools/defs_gen/girprinter.py
new file mode 100644
index 0000000..c02cdcc
--- /dev/null
+++ b/tools/defs_gen/girprinter.py
@@ -0,0 +1,26 @@
+class GirPrinter:
+    def __init__(self, parser):
+        self._parser = parser
+
+    @staticmethod
+    def _print_single_enumeration(enum_object):
+        if enum_object.is_flag:
+            enum_type = "flags"
+        else:
+            enum_type = "extended"
+
+        print("(define-enum-" + enum_type + " " + enum_object.type_name)
+        print("  (in-module \"" + enum_object.module + "\")")
+        print("  (c-name \"" + enum_object.c_type_name + "\")")
+        print("  (values")
+
+        for member in enum_object.members:
+            print("    '(\"" + member.name + "\" " +
+                  "\"" + member.c_name + "\" " +
+                  "\"" + member.value + "\")")
+        print("  )")
+        print(")\n")
+
+    def print_enumerations(self):
+        for enumeration in self._parser.get_enumerations():
+            GirPrinter._print_single_enumeration(enumeration)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]