diff options
author | Johan Dahlin <jdahlin@async.com.br> | 2008-04-20 19:52:33 +0000 |
---|---|---|
committer | Johan Dahlin <johan@src.gnome.org> | 2008-04-20 19:52:33 +0000 |
commit | c33c7f10785cafdcee85cc61860b7e926040a328 (patch) | |
tree | f2327216c5b832447efa5fbb316f50c084ad190d /giscanner | |
parent | e01880c5205ee16eebb7b0cedc7e207a08d4df3d (diff) | |
download | gobject-introspection-c33c7f10785cafdcee85cc61860b7e926040a328.tar.gz |
Add support for classes and methods
2008-04-20 Johan Dahlin <jdahlin@async.com.br>
* giscanner/cgobject.py:
* giscanner/gidlwriter.py:
* giscanner/gobjecttreebuilder.py:
* giscanner/treebuilder.py:
Add support for classes and methods
svn path=/trunk/; revision=181
Diffstat (limited to 'giscanner')
-rw-r--r-- | giscanner/cgobject.py | 3 | ||||
-rw-r--r-- | giscanner/gidlwriter.py | 24 | ||||
-rw-r--r-- | giscanner/gobjecttreebuilder.py | 64 | ||||
-rw-r--r-- | giscanner/treebuilder.py | 10 |
4 files changed, 87 insertions, 14 deletions
diff --git a/giscanner/cgobject.py b/giscanner/cgobject.py index 64dfb498..9dfd560b 100644 --- a/giscanner/cgobject.py +++ b/giscanner/cgobject.py @@ -102,6 +102,9 @@ def type_from_name(name): def type_fundamental(type_id): return _gobj.g_type_fundamental(type_id) +def type_parent(type_id): + return _gobj.g_type_parent(type_id) + _gobj.g_type_class_ref.restype = ctypes.POINTER(GTypeClass) def type_class_ref(type_id): fundamental_type = type_fundamental(type_id) diff --git a/giscanner/gidlwriter.py b/giscanner/gidlwriter.py index 3287622d..018f0a69 100644 --- a/giscanner/gidlwriter.py +++ b/giscanner/gidlwriter.py @@ -1,7 +1,9 @@ -from giscanner.gobjecttreebuilder import GLibEnum, GLibEnumMember, GLibFlags -from giscanner.treebuilder import Enum, Function +from giscanner.gobjecttreebuilder import (GLibEnum, GLibEnumMember, GLibFlags, + GLibObject) +from giscanner.treebuilder import Class, Enum, Function from giscanner.xmlwriter import XMLWriter + class GIDLWriter(XMLWriter): def __init__(self, namespace, nodes): super(GIDLWriter, self).__init__() @@ -23,6 +25,8 @@ class GIDLWriter(XMLWriter): self._write_function(node) elif isinstance(node, Enum): self._write_enum(node) + elif isinstance(node, Class): + self._write_class(node) else: print 'WRITER: Unhandled node', node @@ -32,6 +36,12 @@ class GIDLWriter(XMLWriter): self._write_parameters(func.parameters) self.pop_tag() + def _write_method(self, method): + self.push_tag('method', [('name', method.name)]) + self._write_return_type(method.retval) + self._write_parameters(method.parameters) + self.pop_tag() + def _write_return_type(self, return_): if not return_: return @@ -68,3 +78,13 @@ class GIDLWriter(XMLWriter): if isinstance(member, GLibEnumMember): attrs.append(('nick', member.nick)) self.write_tag('member', attrs) + + def _write_class(self, class_): + attrs = [('name', class_.name), + ('parent', class_.parent)] + if isinstance(class_, GLibObject): + attrs.append(('get-type', class_.get_type)) + self.push_tag('object', attrs) + for method in class_.methods: + self._write_method(method) + self.pop_tag() diff --git a/giscanner/gobjecttreebuilder.py b/giscanner/gobjecttreebuilder.py index 3576da41..14a7a7f6 100644 --- a/giscanner/gobjecttreebuilder.py +++ b/giscanner/gobjecttreebuilder.py @@ -1,7 +1,7 @@ import ctypes from giscanner import cgobject -from giscanner.treebuilder import Enum, Function, Member, Struct +from giscanner.treebuilder import Class, Enum, Function, Member, Struct from giscanner.odict import odict @@ -28,6 +28,12 @@ class GLibEnumMember(Member): self.nick = nick +class GLibObject(Class): + def __init__(self, name, parent, methods, get_type): + Class.__init__(self, name, parent, methods) + self.get_type = get_type + + class GObjectTreeBuilder(object): def __init__(self): self.nodes = [] @@ -75,15 +81,43 @@ class GObjectTreeBuilder(object): self._add_attribute(enum) def _parse_function(self, func): - symbol = func.name - # GType *_get_type(void) - if (symbol.endswith('_get_type') and - func.retval.type == 'GType' and - not func.parameters): - type_id = self._call_get_type_function(symbol) - self._parse_gtype(type_id, symbol) + if self._parse_get_type_function(func): return - self._add_attribute(func) + elif self._parse_method(func): + return + else: + self._add_attribute(func) + + def _parse_get_type_function(self, func): + # GType *_get_type(void) + symbol = func.name + if not symbol.endswith('_get_type'): + return False + if func.retval.type != 'GType': + return False + if func.parameters: + return False + + type_id = self._call_get_type_function(symbol) + self._parse_gtype(type_id, symbol) + return True + + + def _parse_method(self, func): + if not func.parameters: + return False + + first_arg = func.parameters[0].type + if first_arg.count('*') != 1: + return False + + object_name = first_arg.replace('*', '') + class_ = self._namespace.get(object_name, None) + if class_ is None or not isinstance(class_, GLibObject): + return False + + class_.methods.append(func) + return True def _parse_struct(self, struct): if (struct.name.startswith('_') or @@ -96,9 +130,9 @@ class GObjectTreeBuilder(object): fundamental_type_id = cgobject.type_fundamental(type_id) if (fundamental_type_id == cgobject.TYPE_ENUM or fundamental_type_id == cgobject.TYPE_FLAGS): - self._introspect_enum(fundamental_type_id, type_id, symbol) - #elif fundamental_type == cgobject.TYPE_OBJECT: - # pass + pass #self._introspect_enum(fundamental_type_id, type_id, symbol) + elif fundamental_type_id == cgobject.TYPE_OBJECT: + self._introspect_object(type_id, symbol) else: print 'unhandled GType: %s' % (cgobject.type_name(type_id),) @@ -116,3 +150,9 @@ class GObjectTreeBuilder(object): klass = (GLibFlags if ftype_id == cgobject.TYPE_FLAGS else GLibEnum) cenum = klass(cgobject.type_name(type_id), members, symbol) self._add_attribute(cenum, replace=True) + + def _introspect_object(self, type_id, symbol): + type_name = cgobject.type_name(type_id) + parent_name = cgobject.type_name(cgobject.type_parent(type_id)) + node = GLibObject(type_name, parent_name, [], symbol) + self._add_attribute(node, replace=True) diff --git a/giscanner/treebuilder.py b/giscanner/treebuilder.py index 35fab487..19413aa2 100644 --- a/giscanner/treebuilder.py +++ b/giscanner/treebuilder.py @@ -57,6 +57,16 @@ class Return(Node): return 'Return(%r)' % (self.type,) +class Class(Node): + def __init__(self, name, parent, methods): + self.name = name + self.parent = parent + self.methods = methods + + def __repr__(self): + return 'Class(%r, %r)' % (self.name, self.methods) + + class TreeBuilder(object): def __init__(self, generator): self.generator = generator |