home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / gst-0.10 / gst / extend / pygobject.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  5.0 KB  |  161 lines

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