home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / lib / site-packages / gtk-2.0 / gobject / __init__.py next >
Encoding:
Python Source  |  2008-05-23  |  3.4 KB  |  99 lines

  1. # -*- Mode: Python; py-indent-offset: 4 -*-
  2. # pygobject - Python bindings for the GObject library
  3. # Copyright (C) 2006  Johan Dahlin
  4. #
  5. #   gobject/__init__.py: initialisation file for gobject module
  6. #
  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.
  11. #
  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.
  16. #
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  20. # USA
  21.  
  22. # this can go when things are a little further along
  23. try:
  24.     import ltihooks
  25.     ltihooks # pyflakes
  26.     del ltihooks
  27. except ImportError:
  28.     pass
  29.  
  30. from gobject.constants import *
  31. from _gobject import *
  32. _PyGObject_API = _gobject._PyGObject_API
  33.  
  34. from propertyhelper import property
  35.  
  36. class GObjectMeta(type):
  37.     "Metaclass for automatically registering GObject classes"
  38.     def __init__(cls, name, bases, dict_):
  39.         type.__init__(cls, name, bases, dict_)
  40.         cls._install_properties()
  41.         cls._type_register(cls.__dict__)
  42.  
  43.     def _install_properties(cls):
  44.         gproperties = getattr(cls, '__gproperties__', {})
  45.  
  46.         props = []
  47.         for name, prop in cls.__dict__.items():
  48.             if isinstance(prop, property): # not same as the built-in
  49.                 if name in gproperties:
  50.                     raise ValueError
  51.                 prop.name = name
  52.                 gproperties[name] = prop.get_pspec_args()
  53.                 props.append(prop)
  54.  
  55.         if not props:
  56.             return
  57.  
  58.         cls.__gproperties__ = gproperties
  59.  
  60.         if ('do_get_property' in cls.__dict__ or
  61.             'do_set_property' in cls.__dict__):
  62.             for prop in props:
  63.                 if (prop.getter != prop._default_getter or
  64.                     prop.setter != prop._default_setter):
  65.                     raise TypeError(
  66.                         "GObject subclass %r defines do_get/set_property"
  67.                         " and it also uses a property which a custom setter"
  68.                         " or getter. This is not allowed" % (
  69.                         cls.__name__,))
  70.  
  71.         def obj_get_property(self, pspec):
  72.             name = pspec.name.replace('-', '_')
  73.             prop = getattr(cls, name, None)
  74.             if prop:
  75.                 return prop.getter(self)
  76.         cls.do_get_property = obj_get_property
  77.  
  78.         def obj_set_property(self, pspec, value):
  79.             name = pspec.name.replace('-', '_')
  80.             prop = getattr(cls, name, None)
  81.             if prop:
  82.                 prop.setter(self, value)
  83.         cls.do_set_property = obj_set_property
  84.  
  85.     def _type_register(cls, namespace):
  86.         ## don't register the class if already registered
  87.         if '__gtype__' in namespace:
  88.             return
  89.  
  90.         if not ('__gproperties__' in namespace or
  91.                 '__gsignals__' in namespace or
  92.                 '__gtype_name__' in namespace):
  93.             return
  94.  
  95.         type_register(cls, namespace.get('__gtype_name__'))
  96. _gobject._install_metaclass(GObjectMeta)
  97.  
  98. del _gobject
  99.