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  """ 
 23  PyGTK helper functions 
 24  """ 
 25   
 26  # moving this down causes havoc when running this file directly for some reason 
 27  from flumotion.common import errors 
 28   
 29  import sys 
 30   
 31  import gobject 
 32   
33 -def gobject_set_property(object, property, value):
34 """ 35 Set the given property to the given value on the given object. 36 37 @type object: L{gobject.GObject} 38 @type property: string 39 @param value: value to set property to 40 """ 41 for pspec in gobject.list_properties(object): 42 if pspec.name == property: 43 break 44 else: 45 raise errors.PropertyError( 46 "Property '%s' in element '%s' does not exist" % ( 47 property, object.get_property('name'))) 48 49 if pspec.value_type in (gobject.TYPE_INT, gobject.TYPE_UINT, 50 gobject.TYPE_INT64, gobject.TYPE_UINT64): 51 try: 52 value = int(value) 53 except ValueError: 54 msg = "Invalid value given for property '%s' in element '%s'" % ( 55 property, object.get_property('name')) 56 raise errors.PropertyError(msg) 57 58 elif pspec.value_type == gobject.TYPE_BOOLEAN: 59 if value == 'False': 60 value = False 61 elif value == 'True': 62 value = True 63 else: 64 value = bool(value) 65 elif pspec.value_type in (gobject.TYPE_DOUBLE, gobject.TYPE_FLOAT): 66 value = float(value) 67 elif pspec.value_type == gobject.TYPE_STRING: 68 value = str(value) 69 # FIXME: this is superevil ! we really need to find a better way 70 # of checking if this property is a param enum 71 # also, we only allow int for now 72 elif repr(pspec.__gtype__).startswith("<GType GParamEnum"): 73 value = int(value) 74 else: 75 raise errors.PropertyError('Unknown property type: %s' % 76 pspec.value_type) 77 78 object.set_property(property, value)
79
80 -def gsignal(name, *args):
81 """ 82 Add a GObject signal to the current object. 83 To be used from class definition scope. 84 85 @type name: string 86 @type args: mixed 87 """ 88 frame = sys._getframe(1) 89 _locals = frame.f_locals 90 91 if not '__gsignals__' in _locals: 92 _dict = _locals['__gsignals__'] = {} 93 else: 94 _dict = _locals['__gsignals__'] 95 96 _dict[name] = (gobject.SIGNAL_RUN_FIRST, None, args)
97 98 PARAM_CONSTRUCT = 1<<9 99
100 -def with_construct_properties(__init__):
101 """ 102 Wrap a class' __init__ method in a procedure that will construct 103 gobject properties. This is necessary because pygtk's object 104 construction is a bit broken. 105 106 Usage:: 107 108 class Foo(GObject): 109 def __init__(self): 110 GObject.__init(self) 111 __init__ = with_construct_properties(__init__) 112 """ 113 frame = sys._getframe(1) 114 _locals = frame.f_locals 115 gproperties = _locals['__gproperties__'] 116 def hacked_init(self, *args, **kwargs): 117 __init__(self, *args, **kwargs) 118 self.__gproperty_values = {} 119 for p, v in gproperties.items(): 120 if v[-1] & PARAM_CONSTRUCT: 121 self.set_property(p, v[3])
122 return hacked_init 123
124 -def gproperty(type_, name, desc, *args, **kwargs):
125 """ 126 Add a GObject property to the current object. 127 To be used from class definition scope. 128 129 @type type_: type object 130 @type name: string 131 @type desc: string 132 @type args: mixed 133 """ 134 frame = sys._getframe(1) 135 _locals = frame.f_locals 136 flags = 0 137 138 def _do_get_property(self, prop): 139 try: 140 return self._gproperty_values[prop.name] 141 except (AttributeError, KeyError): 142 raise AttributeError('Property was never set', self, prop)
143 144 def _do_set_property(self, prop, value): 145 if not getattr(self, '_gproperty_values', None): 146 self._gproperty_values = {} 147 self._gproperty_values[prop.name] = value 148 149 _locals['do_get_property'] = _do_get_property 150 _locals['do_set_property'] = _do_set_property 151 152 if not '__gproperties__' in _locals: 153 _dict = _locals['__gproperties__'] = {} 154 else: 155 _dict = _locals['__gproperties__'] 156 157 for i in 'readable', 'writable': 158 if not i in kwargs: 159 kwargs[i] = True 160 161 for k, v in kwargs.items(): 162 if k == 'construct': 163 flags |= PARAM_CONSTRUCT 164 elif k == 'construct_only': 165 flags |= gobject.PARAM_CONSTRUCT_ONLY 166 elif k == 'readable': 167 flags |= gobject.PARAM_READABLE 168 elif k == 'writable': 169 flags |= gobject.PARAM_WRITABLE 170 elif k == 'lax_validation': 171 flags |= gobject.PARAM_LAX_VALIDATION 172 else: 173 raise Exception('Invalid GObject property flag: %r=%r' % (k, v)) 174 175 _dict[name] = (type_, name, desc) + args + tuple((flags,)) 176 177 # gobject.type_register() can be done automatically in pygtk 2.8 178 # We don't want to get the loud deprecation warnings from PyGtk for using 179 # gobject.type_register() if we don't need it 180 # PYGTK 2.6
181 -def type_register(klass):
182 if gobject.pygtk_version < (2, 8, 0): 183 gobject.type_register(klass) 184 elif klass.__gtype__.pytype is not klass: 185 # all subclasses will at least have a __gtype__ from their 186 # parent, make sure it corresponds to the exact class 187 gobject.type_register(klass)
188