1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# -*- Mode: Python; py-indent-offset: 4 -*-
# vim: tabstop=4 shiftwidth=4 expandtab
#
# Copyright (C) 2005-2009 Johan Dahlin <johan@gnome.org>
#
# types.py: base types for introspected items.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA
from new import instancemethod
import gobject
from ._gi import \
InterfaceInfo, \
ObjectInfo, \
StructInfo
def Function(info):
def function(*args):
return info.invoke(*args)
function.__info__ = info
function.__name__ = info.get_name()
function.__module__ = info.get_namespace()
return function
class MetaClassHelper(object):
def _setup_methods(cls):
constructor_infos = []
method_infos = cls.__info__.get_methods()
for method_info in method_infos:
name = method_info.get_name()
function = Function(method_info)
if method_info.is_method():
method = instancemethod(function, None, cls)
elif method_info.is_constructor():
method = classmethod(function)
else:
method = staticmethod(function)
setattr(cls, name, method)
if method_info.is_constructor():
constructor_infos.append(method_info)
default_constructor_info = None
if len(constructor_infos) == 1:
(default_constructor_info,) = constructor_infos
else:
for constructor_info in constructor_infos:
if constructor_info.get_name() == 'new':
default_constructor_info = constructor_info
break
if default_constructor_info is not None:
function = Function(default_constructor_info)
cls.__new__ = staticmethod(function)
# Override the initializer because of the constructor's arguments
cls.__init__ = lambda self, *args, **kwargs: super(cls, self).__init__()
def _setup_fields(cls):
for field_info in cls.__info__.get_fields():
name = field_info.get_name().replace('-', '_')
setattr(cls, name, property(field_info.get_value, field_info.set_value))
class GObjectMeta(gobject.GObjectMeta, MetaClassHelper):
def __init__(cls, name, bases, dict_):
super(GObjectMeta, cls).__init__(name, bases, dict_)
# Avoid touching anything else than the base class.
if cls.__name__ != cls.__info__.get_name():
return;
cls._setup_methods()
if (isinstance(cls.__info__, ObjectInfo)):
cls._setup_fields()
cls.__info__.register_type(cls)
class StructMeta(type, MetaClassHelper):
def __init__(cls, name, bases, dict_):
super(StructMeta, cls).__init__(name, bases, dict_)
# Avoid touching anything else than the base class.
if cls.__name__ != cls.__info__.get_name():
return;
cls._setup_fields()
cls._setup_methods()
|