1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # pygobject - Python bindings for the GObject library
3 # Copyright (C) 2007 Johan Dahlin
5 # gobject/propertyhelper.py: GObject property wrapper/helper
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
24 from . import _gobject
26 from .constants import \
27 TYPE_NONE, TYPE_INTERFACE, TYPE_CHAR, TYPE_UCHAR, \
28 TYPE_BOOLEAN, TYPE_INT, TYPE_UINT, TYPE_LONG, \
29 TYPE_ULONG, TYPE_INT64, TYPE_UINT64, TYPE_ENUM, \
30 TYPE_FLAGS, TYPE_FLOAT, TYPE_DOUBLE, TYPE_STRING, \
31 TYPE_POINTER, TYPE_BOXED, TYPE_PARAM, TYPE_OBJECT, \
33 from .constants import \
34 G_MINFLOAT, G_MAXFLOAT, G_MINDOUBLE, G_MAXDOUBLE, \
35 G_MININT, G_MAXINT, G_MAXUINT, G_MINLONG, G_MAXLONG, \
38 if sys.version_info >= (3, 0):
42 _basestring = basestring
45 class property(object):
47 Creates a new property which in conjunction with GObject subclass will
48 create a property proxy:
50 >>> class MyObject(gobject.GObject):
51 >>> ... prop = gobject.property(type=str)
54 >>> obj.prop = 'value'
59 The API is similar to the builtin property:
61 class AnotherObject(gobject.GObject):
66 Which will create a read-only property called prop.
69 class __metaclass__(type):
71 return "<class 'gobject.property'>"
73 def __init__(self, getter=None, setter=None, type=None, default=None,
74 nick='', blurb='', flags=_gobject.PARAM_READWRITE,
75 minimum=None, maximum=None):
77 @param getter: getter to get the value of the property
78 @type getter: callable
79 @param setter: setter to set the value of the property
80 @type setter: callable
81 @param type: type of property
83 @param default: default value
84 @param nick: short description
86 @param blurb: long description
88 @param flags: parameter flags, one of:
89 - gobject.PARAM_READABLE
90 - gobject.PARAM_READWRITE
91 - gobject.PARAM_WRITABLE
92 - gobject.PARAM_CONSTRUCT
93 - gobject.PARAM_CONSTRUCT_ONLY
94 - gobject.PARAM_LAX_VALIDATION
95 @keyword minimum: minimum allowed value (int, float, long only)
96 @keyword maximum: maximum allowed value (int, float, long only)
99 if getter and not setter:
100 setter = self._readonly_setter
101 elif setter and not getter:
102 getter = self._writeonly_getter
103 elif not setter and not getter:
104 getter = self._default_getter
105 setter = self._default_setter
111 self.type = self._type_from_python(type)
112 self.default = self._get_default(default)
113 self._check_default()
115 if not isinstance(nick, _basestring):
116 raise TypeError("nick must be a string")
119 if not isinstance(blurb, _basestring):
120 raise TypeError("blurb must be a string")
123 if flags < 0 or flags > 32:
124 raise TypeError("invalid flag value: %r" % (flags,))
127 if minimum is not None:
128 if minimum < self._get_minimum():
130 "Minimum for type %s cannot be lower than %d" % (
131 self.type, self._get_minimum()))
133 minimum = self._get_minimum()
134 self.minimum = minimum
135 if maximum is not None:
136 if maximum > self._get_maximum():
138 "Maximum for type %s cannot be higher than %d" % (
139 self.type, self._get_maximum()))
141 maximum = self._get_maximum()
142 self.maximum = maximum
149 return '<gobject property %s (%s)>' % (
150 self.name or '(uninitialized)',
151 _gobject.type_name(self.type))
153 def __get__(self, instance, klass):
158 value = instance.get_property(self.name)
166 def __set__(self, instance, value):
171 instance.set_property(self.name, value)
177 def _type_from_python(self, type_):
188 elif type_ == object:
190 elif (isinstance(type_, type) and
191 issubclass(type_, (_gobject.GObject,
193 return type_.__gtype__
194 elif type_ in [TYPE_NONE, TYPE_INTERFACE, TYPE_CHAR, TYPE_UCHAR,
195 TYPE_INT, TYPE_UINT, TYPE_BOOLEAN, TYPE_LONG,
196 TYPE_ULONG, TYPE_INT64, TYPE_UINT64,
197 TYPE_FLOAT, TYPE_DOUBLE, TYPE_POINTER,
198 TYPE_BOXED, TYPE_PARAM, TYPE_OBJECT, TYPE_STRING,
202 raise TypeError("Unsupported type: %r" % (type_,))
204 def _get_default(self, default):
206 if default is not None:
209 if ptype in [TYPE_INT, TYPE_UINT, TYPE_LONG, TYPE_ULONG,
210 TYPE_INT64, TYPE_UINT64]:
212 elif ptype == TYPE_STRING:
214 elif ptype == TYPE_FLOAT or ptype == TYPE_DOUBLE:
219 def _check_default(self):
221 default = self.default
222 if (ptype == TYPE_BOOLEAN and (default not in (True, False))):
224 "default must be True or False, not %r" % (default,))
225 elif ptype == TYPE_PYOBJECT:
226 if default is not None:
227 raise TypeError("object types does not have default values")
228 elif _gobject.type_is_a(ptype, TYPE_ENUM):
230 raise TypeError("enum properties needs a default value")
231 elif not _gobject.type_is_a(default, ptype):
232 raise TypeError("enum value %s must be an instance of %r" %
235 def _get_minimum(self):
237 if ptype in [TYPE_UINT, TYPE_ULONG, TYPE_UINT64]:
239 # Remember that G_MINFLOAT and G_MINDOUBLE are something different.
240 elif ptype == TYPE_FLOAT:
242 elif ptype == TYPE_DOUBLE:
244 elif ptype == TYPE_INT:
246 elif ptype == TYPE_LONG:
248 elif ptype == TYPE_INT64:
253 def _get_maximum(self):
255 if ptype == TYPE_UINT:
257 elif ptype == TYPE_ULONG:
259 elif ptype == TYPE_INT64:
261 elif ptype == TYPE_UINT64:
263 elif ptype == TYPE_FLOAT:
265 elif ptype == TYPE_DOUBLE:
267 elif ptype == TYPE_INT:
269 elif ptype == TYPE_LONG:
278 def _default_setter(self, instance, value):
279 setattr(instance, '_property_helper_'+self.name, value)
281 def _default_getter(self, instance):
282 return getattr(instance, '_property_helper_'+self.name, self.default)
284 def _readonly_setter(self, instance, value):
285 self._exc = TypeError("%s property of %s is read-only" % (
286 self.name, type(instance).__name__))
288 def _writeonly_getter(self, instance):
289 self._exc = TypeError("%s property of %s is write-only" % (
290 self.name, type(instance).__name__))
296 def get_pspec_args(self):
298 if ptype in [TYPE_INT, TYPE_UINT, TYPE_LONG, TYPE_ULONG,
299 TYPE_INT64, TYPE_UINT64, TYPE_FLOAT, TYPE_DOUBLE]:
300 args = self._get_minimum(), self._get_maximum(), self.default
301 elif (ptype == TYPE_STRING or ptype == TYPE_BOOLEAN or
302 ptype.is_a(TYPE_ENUM)):
303 args = (self.default,)
304 elif ptype == TYPE_PYOBJECT:
306 elif ptype.is_a(TYPE_OBJECT):
309 raise NotImplementedError(ptype)
311 return (self.type, self.nick, self.blurb) + args + (self.flags,)