gobject-introspection r175 - in trunk: . giscanner tools



Author: johan
Date: Fri Apr 18 20:37:51 2008
New Revision: 175
URL: http://svn.gnome.org/viewvc/gobject-introspection?rev=175&view=rev

Log:
2008-04-18  Johan Dahlin  <jdahlin async com br>

    * giscanner/gidlwriter.py:
    * giscanner/xmlwriter.py:
    * tools/g-ir-scanner:
    Add a simplistic gidl writer, which can't do too much.



Added:
   trunk/giscanner/gidlwriter.py   (contents, props changed)
   trunk/giscanner/xmlwriter.py   (contents, props changed)
Modified:
   trunk/ChangeLog
   trunk/tools/g-ir-scanner

Added: trunk/giscanner/gidlwriter.py
==============================================================================
--- (empty file)
+++ trunk/giscanner/gidlwriter.py	Fri Apr 18 20:37:51 2008
@@ -0,0 +1,48 @@
+from giscanner.treebuilder import Function
+from giscanner.xmlwriter import XMLWriter
+
+
+class GIDLWriter(XMLWriter):
+    def __init__(self, namespace, nodes):
+        super(GIDLWriter, self).__init__()
+        self._write_api(namespace, nodes)
+
+    def _write_api(self, namespace, nodes):
+        self.push_tag('api', [('version', '1.0')])
+        self._write_namespace(namespace, nodes)
+        self.pop_tag()
+
+    def _write_namespace(self, namespace, nodes):
+        self.push_tag('namespace')
+        for node in nodes:
+            self._write_node(node)
+        self.pop_tag()
+
+    def _write_node(self, node):
+        if isinstance(node, Function):
+            self._write_function(node)
+        else:
+            print 'Unhandled node', node
+
+    def _write_function(self, func):
+        self.push_tag('function')
+        self._write_return_type(func.retval)
+        self._write_parameters(func.parameters)
+        self.pop_tag()
+
+    def _write_return_type(self, return_):
+        if not return_:
+            return
+        self.write_tag('return-type', [('type', return_.type)])
+
+    def _write_parameters(self, parameters):
+        if not parameters:
+            return
+        self.push_tag('parameters')
+        for parameter in parameters:
+            self._write_parameter(parameter)
+        self.pop_tag()
+
+    def _write_parameter(self, parameter):
+        self.write_tag('parameter', [('name', parameter.name),
+                                     ('type', parameter.type)])

Added: trunk/giscanner/xmlwriter.py
==============================================================================
--- (empty file)
+++ trunk/giscanner/xmlwriter.py	Fri Apr 18 20:37:51 2008
@@ -0,0 +1,55 @@
+from cStringIO import StringIO
+from xml.sax.saxutils import quoteattr
+
+
+class XMLWriter(object):
+    def __init__(self):
+        self._data = StringIO()
+        self._tag_stack = []
+        self._indent = 0
+
+    # Private
+
+    def _collect_attributes(self, attributes):
+        attrValue = ''
+        if attributes:
+            for attr, value in attributes:
+                assert value is not None, attr
+                attrValue += ' %s=%s' % (attr, quoteattr(value))
+        return attrValue
+
+    def _open_tag(self, tag_name, attributes=None):
+        attrs = self._collect_attributes(attributes)
+        self.write_line('<%s%s>' % (tag_name, attrs))
+
+    def _close_tag(self, tag_name):
+        self.write_line('</%s>' % (tag_name,))
+
+    # Public API
+
+    def get_xml(self):
+        return self._data.getvalue()
+
+    def write_line(self, line=''):
+        self._data.write('%s%s\n' % (' ' * self._indent, line))
+
+    def write_tag(self, tag_name, attributes):
+        attrs = self._collect_attributes(attributes)
+        self.write_line('<%s%s/>' % (tag_name, attrs))
+
+    def write_tag_with_data(self, tag_name, data, attributes=None):
+        if data is None:
+            self.write_tag(tag_name, attributes)
+        attrs = self._collect_attributes(attributes)
+        self.write_line('<%s%s>%s</%s>' % (tag_name, attrs, data, tag_name))
+
+    def push_tag(self, tag_name, attributes=None):
+        self._open_tag(tag_name, attributes)
+        self._tag_stack.append(tag_name)
+        self._indent += 2
+
+    def pop_tag(self):
+        self._indent -= 2
+        tag_name = self._tag_stack.pop()
+        self._close_tag(tag_name)
+        return tag_name

Modified: trunk/tools/g-ir-scanner
==============================================================================
--- trunk/tools/g-ir-scanner	(original)
+++ trunk/tools/g-ir-scanner	Fri Apr 18 20:37:51 2008
@@ -4,6 +4,7 @@
 
 sys.path.insert(0, '.')
 
+from giscanner.gidlwriter import GIDLWriter
 from giscanner.sourcescanner import SourceScanner
 from giscanner.treebuilder import TreeBuilder
 
@@ -44,8 +45,8 @@
     ss.parse_macros()
 
     builder = TreeBuilder(ss)
-    import pprint
-    pprint.pprint(list(builder.get_nodes()))
+    writer = GIDLWriter('Foo', builder.get_nodes())
+    print writer.get_xml()
 
 
 sys.exit(main(sys.argv))



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