diff options
| author | Johan Dahlin <johan@src.gnome.org> | 2007-05-01 15:54:05 +0000 |
|---|---|---|
| committer | Johan Dahlin <johan@src.gnome.org> | 2007-05-01 15:54:05 +0000 |
| commit | a57fde73b509adcf56a73ae1fb97474d136fd2af (patch) | |
| tree | 808278f44be0091211dd0113d6293ea0d6510583 /gobject/__init__.py | |
| parent | ecbf8b6315dda927f4f90d8651033eb67c46a187 (diff) | |
| download | pygobject-a57fde73b509adcf56a73ae1fb97474d136fd2af.tar.gz pygobject-a57fde73b509adcf56a73ae1fb97474d136fd2af.tar.xz pygobject-a57fde73b509adcf56a73ae1fb97474d136fd2af.zip | |
Add a property helper, fixes #338098
* examples/properties.py:
* gobject/Makefile.am:
* gobject/__init__.py:
* gobject/constants.py.in:
* gobject/generate-constants.c: (main):
* gobject/propertyhelper.py:
* tests/Makefile.am:
* tests/test_enum.py:
* tests/test_interface.py:
* tests/test_properties.py:
Add a property helper, fixes #338098
svn path=/trunk/; revision=662
Diffstat (limited to 'gobject/__init__.py')
| -rw-r--r-- | gobject/__init__.py | 70 |
1 files changed, 46 insertions, 24 deletions
diff --git a/gobject/__init__.py b/gobject/__init__.py index 43a2cfd..d251d31 100644 --- a/gobject/__init__.py +++ b/gobject/__init__.py @@ -27,15 +27,61 @@ try: except ImportError: pass +from gobject.constants import * from _gobject import * _PyGObject_API = _gobject._PyGObject_API +from propertyhelper import property + class GObjectMeta(type): "Metaclass for automatically registering GObject classes" def __init__(cls, name, bases, dict_): type.__init__(cls, name, bases, dict_) + cls._install_properties() cls._type_register(cls.__dict__) + def _install_properties(cls): + gproperties = getattr(cls, '__gproperties__', {}) + props = {} + for name, prop in cls.__dict__.items(): + if isinstance(prop, property): # not same as the built-in + if name in gproperties: + raise ValueError + prop.name = name + props[name] = prop.get_pspec_args() + + if not props: + return + + if not gproperties: + cls.__gproperties__ = props + else: + gproperties.update(props) + + if (hasattr(cls, 'do_get_property') or + hasattr(cls, 'do_set_property')): + for prop in props: + if (prop.getter != prop.default_getter or + prop.setter != prop.default_setter): + raise TypeError( + "GObject subclass %r defines do_get/set_property" + " and it also uses a property which a custom setter" + " or getter. This is not allowed" % (cls,)) + + def obj_get_property(self, pspec): + name = pspec.name.replace('-', '_') + prop = getattr(cls, name, None) + if prop: + return prop.getter(self) + cls.do_get_property = obj_get_property + + def obj_set_property(self, pspec, value): + name = pspec.name.replace('-', '_') + prop = getattr(cls, name, None) + if prop: + prop.setter(self, value) + cls.do_set_property = obj_set_property + def _type_register(cls, namespace): ## don't register the class if already registered if '__gtype__' in namespace: @@ -50,28 +96,4 @@ class GObjectMeta(type): _gobject._install_metaclass(GObjectMeta) -# TYPE_INVALID defined in gobjectmodule.c -TYPE_NONE = type_from_name('void') -TYPE_INTERFACE = type_from_name('GInterface') -TYPE_CHAR = type_from_name('gchar') -TYPE_UCHAR = type_from_name('guchar') -TYPE_BOOLEAN = type_from_name('gboolean') -TYPE_INT = type_from_name('gint') -TYPE_UINT = type_from_name('guint') -TYPE_LONG = type_from_name('glong') -TYPE_ULONG = type_from_name('gulong') -TYPE_INT64 = type_from_name('gint64') -TYPE_UINT64 = type_from_name('guint64') -TYPE_ENUM = type_from_name('GEnum') -TYPE_FLAGS = type_from_name('GFlags') -TYPE_FLOAT = type_from_name('gfloat') -TYPE_DOUBLE = type_from_name('gdouble') -TYPE_STRING = type_from_name('gchararray') -TYPE_POINTER = type_from_name('gpointer') -TYPE_BOXED = type_from_name('GBoxed') -TYPE_PARAM = type_from_name('GParam') -TYPE_OBJECT = type_from_name('GObject') -TYPE_PYOBJECT = type_from_name('PyObject') -TYPE_UNICHAR = TYPE_UINT - del _gobject |
