Package flumotion :: Package common :: Module pygobject
[hide private]

Source Code for Module flumotion.common.pygobject

  1  # -*- Mode: Python; test-case-name: flumotion.test.test_common_pygobject -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  """pygobject helper functions 
 23  """ 
 24   
 25  # moving this down causes havoc when running this file directly for some reason 
 26  from flumotion.common import errors 
 27   
 28  import sys 
 29   
 30  import gobject 
 31   
 32  __version__ = "$Rev: 7054 $" 
 33   
 34   
35 -def gobject_set_property(object, property, value):
36 """ 37 Set the given property to the given value on the given object. 38 39 @type object: L{gobject.GObject} 40 @type property: string 41 @param value: value to set property to 42 """ 43 for pspec in gobject.list_properties(object): 44 if pspec.name == property: 45 break 46 else: 47 raise errors.PropertyError( 48 "Property '%s' in element '%s' does not exist" % ( 49 property, object.get_property('name'))) 50 51 if pspec.value_type in (gobject.TYPE_INT, gobject.TYPE_UINT, 52 gobject.TYPE_INT64, gobject.TYPE_UINT64): 53 try: 54 value = int(value) 55 except ValueError: 56 msg = "Invalid value given for property '%s' in element '%s'" % ( 57 property, object.get_property('name')) 58 raise errors.PropertyError(msg) 59 60 elif pspec.value_type == gobject.TYPE_BOOLEAN: 61 if value == 'False': 62 value = False 63 elif value == 'True': 64 value = True 65 else: 66 value = bool(value) 67 elif pspec.value_type in (gobject.TYPE_DOUBLE, gobject.TYPE_FLOAT): 68 value = float(value) 69 elif pspec.value_type == gobject.TYPE_STRING: 70 value = str(value) 71 # FIXME: this is superevil ! we really need to find a better way 72 # of checking if this property is a param enum 73 # also, we only allow int for now 74 elif repr(pspec.__gtype__).startswith("<GType GParamEnum"): 75 value = int(value) 76 else: 77 raise errors.PropertyError('Unknown property type: %s' % 78 pspec.value_type) 79 80 object.set_property(property, value)
81
82 -def gsignal(name, *args):
83 """ 84 Add a GObject signal to the current object. 85 To be used from class definition scope. 86 87 @type name: string 88 @type args: mixed 89 """ 90 frame = sys._getframe(1) 91 _locals = frame.f_locals 92 93 if not '__gsignals__' in _locals: 94 _dict = _locals['__gsignals__'] = {} 95 else: 96 _dict = _locals['__gsignals__'] 97 98 _dict[name] = (gobject.SIGNAL_RUN_FIRST, None, args)
99 100 PARAM_CONSTRUCT = 1<<9 101
102 -def gproperty(type_, name, desc, *args, **kwargs):
103 """ 104 Add a GObject property to the current object. 105 To be used from class definition scope. 106 107 @type type_: type object 108 @type name: string 109 @type desc: string 110 @type args: mixed 111 """ 112 frame = sys._getframe(1) 113 _locals = frame.f_locals 114 flags = 0 115 116 def _do_get_property(self, prop): 117 try: 118 return self._gproperty_values[prop.name] 119 except (AttributeError, KeyError): 120 raise AttributeError('Property was never set', self, prop)
121 122 def _do_set_property(self, prop, value): 123 if not getattr(self, '_gproperty_values', None): 124 self._gproperty_values = {} 125 self._gproperty_values[prop.name] = value 126 127 _locals['do_get_property'] = _do_get_property 128 _locals['do_set_property'] = _do_set_property 129 130 if not '__gproperties__' in _locals: 131 _dict = _locals['__gproperties__'] = {} 132 else: 133 _dict = _locals['__gproperties__'] 134 135 for i in 'readable', 'writable': 136 if not i in kwargs: 137 kwargs[i] = True 138 139 for k, v in kwargs.items(): 140 if k == 'construct': 141 flags |= PARAM_CONSTRUCT 142 elif k == 'construct_only': 143 flags |= gobject.PARAM_CONSTRUCT_ONLY 144 elif k == 'readable': 145 flags |= gobject.PARAM_READABLE 146 elif k == 'writable': 147 flags |= gobject.PARAM_WRITABLE 148 elif k == 'lax_validation': 149 flags |= gobject.PARAM_LAX_VALIDATION 150 else: 151 raise Exception('Invalid GObject property flag: %r=%r' % (k, v)) 152 153 _dict[name] = (type_, name, desc) + args + tuple((flags,)) 154
155 -def type_register(klass):
156 if klass.__gtype__.pytype is not klass: 157 # all subclasses will at least have a __gtype__ from their 158 # parent, make sure it corresponds to the exact class 159 gobject.type_register(klass)
160