1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """
23 PyGTK helper functions
24 """
25
26
27 from flumotion.common import errors
28
29 import sys
30
31 import gobject
32
34 """
35 Set the given property to the given value on the given object.
36
37 @type object: L{gobject.GObject}
38 @type property: string
39 @param value: value to set property to
40 """
41 for pspec in gobject.list_properties(object):
42 if pspec.name == property:
43 break
44 else:
45 raise errors.PropertyError(
46 "Property '%s' in element '%s' does not exist" % (
47 property, object.get_property('name')))
48
49 if pspec.value_type in (gobject.TYPE_INT, gobject.TYPE_UINT,
50 gobject.TYPE_INT64, gobject.TYPE_UINT64):
51 try:
52 value = int(value)
53 except ValueError:
54 msg = "Invalid value given for property '%s' in element '%s'" % (
55 property, object.get_property('name'))
56 raise errors.PropertyError(msg)
57
58 elif pspec.value_type == gobject.TYPE_BOOLEAN:
59 if value == 'False':
60 value = False
61 elif value == 'True':
62 value = True
63 else:
64 value = bool(value)
65 elif pspec.value_type in (gobject.TYPE_DOUBLE, gobject.TYPE_FLOAT):
66 value = float(value)
67 elif pspec.value_type == gobject.TYPE_STRING:
68 value = str(value)
69
70
71
72 elif repr(pspec.__gtype__).startswith("<GType GParamEnum"):
73 value = int(value)
74 else:
75 raise errors.PropertyError('Unknown property type: %s' %
76 pspec.value_type)
77
78 object.set_property(property, value)
79
81 """
82 Add a GObject signal to the current object.
83 To be used from class definition scope.
84
85 @type name: string
86 @type args: mixed
87 """
88 frame = sys._getframe(1)
89 _locals = frame.f_locals
90
91 if not '__gsignals__' in _locals:
92 _dict = _locals['__gsignals__'] = {}
93 else:
94 _dict = _locals['__gsignals__']
95
96 _dict[name] = (gobject.SIGNAL_RUN_FIRST, None, args)
97
98 PARAM_CONSTRUCT = 1<<9
99
101 """
102 Wrap a class' __init__ method in a procedure that will construct
103 gobject properties. This is necessary because pygtk's object
104 construction is a bit broken.
105
106 Usage::
107
108 class Foo(GObject):
109 def __init__(self):
110 GObject.__init(self)
111 __init__ = with_construct_properties(__init__)
112 """
113 frame = sys._getframe(1)
114 _locals = frame.f_locals
115 gproperties = _locals['__gproperties__']
116 def hacked_init(self, *args, **kwargs):
117 __init__(self, *args, **kwargs)
118 self.__gproperty_values = {}
119 for p, v in gproperties.items():
120 if v[-1] & PARAM_CONSTRUCT:
121 self.set_property(p, v[3])
122 return hacked_init
123
124 -def gproperty(type_, name, desc, *args, **kwargs):
125 """
126 Add a GObject property to the current object.
127 To be used from class definition scope.
128
129 @type type_: type object
130 @type name: string
131 @type desc: string
132 @type args: mixed
133 """
134 frame = sys._getframe(1)
135 _locals = frame.f_locals
136 flags = 0
137
138 def _do_get_property(self, prop):
139 try:
140 return self._gproperty_values[prop.name]
141 except (AttributeError, KeyError):
142 raise AttributeError('Property was never set', self, prop)
143
144 def _do_set_property(self, prop, value):
145 if not getattr(self, '_gproperty_values', None):
146 self._gproperty_values = {}
147 self._gproperty_values[prop.name] = value
148
149 _locals['do_get_property'] = _do_get_property
150 _locals['do_set_property'] = _do_set_property
151
152 if not '__gproperties__' in _locals:
153 _dict = _locals['__gproperties__'] = {}
154 else:
155 _dict = _locals['__gproperties__']
156
157 for i in 'readable', 'writable':
158 if not i in kwargs:
159 kwargs[i] = True
160
161 for k, v in kwargs.items():
162 if k == 'construct':
163 flags |= PARAM_CONSTRUCT
164 elif k == 'construct_only':
165 flags |= gobject.PARAM_CONSTRUCT_ONLY
166 elif k == 'readable':
167 flags |= gobject.PARAM_READABLE
168 elif k == 'writable':
169 flags |= gobject.PARAM_WRITABLE
170 elif k == 'lax_validation':
171 flags |= gobject.PARAM_LAX_VALIDATION
172 else:
173 raise Exception('Invalid GObject property flag: %r=%r' % (k, v))
174
175 _dict[name] = (type_, name, desc) + args + tuple((flags,))
176
177
178
179
180
188