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$" 
 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
83 -def gsignal(name, *args):
84 """ 85 Add a GObject signal to the current object. 86 To be used from class definition scope. 87 88 @type name: string 89 @type args: mixed 90 """ 91 frame = sys._getframe(1) 92 _locals = frame.f_locals 93 94 if not '__gsignals__' in _locals: 95 _dict = _locals['__gsignals__'] = {} 96 else: 97 _dict = _locals['__gsignals__'] 98 99 _dict[name] = (gobject.SIGNAL_RUN_FIRST, None, args)
100 101 PARAM_CONSTRUCT = 1<<9 102 103
104 -def gproperty(type_, name, desc, *args, **kwargs):
105 """ 106 Add a GObject property to the current object. 107 To be used from class definition scope. 108 109 @type type_: type object 110 @type name: string 111 @type desc: string 112 @type args: mixed 113 """ 114 frame = sys._getframe(1) 115 _locals = frame.f_locals 116 flags = 0 117 118 def _do_get_property(self, prop): 119 try: 120 return self._gproperty_values[prop.name] 121 except (AttributeError, KeyError): 122 raise AttributeError('Property was never set', self, prop)
123 124 def _do_set_property(self, prop, value): 125 if not getattr(self, '_gproperty_values', None): 126 self._gproperty_values = {} 127 self._gproperty_values[prop.name] = value 128 129 _locals['do_get_property'] = _do_get_property 130 _locals['do_set_property'] = _do_set_property 131 132 if not '__gproperties__' in _locals: 133 _dict = _locals['__gproperties__'] = {} 134 else: 135 _dict = _locals['__gproperties__'] 136 137 for i in 'readable', 'writable': 138 if not i in kwargs: 139 kwargs[i] = True 140 141 for k, v in kwargs.items(): 142 if k == 'construct': 143 flags |= PARAM_CONSTRUCT 144 elif k == 'construct_only': 145 flags |= gobject.PARAM_CONSTRUCT_ONLY 146 elif k == 'readable': 147 flags |= gobject.PARAM_READABLE 148 elif k == 'writable': 149 flags |= gobject.PARAM_WRITABLE 150 elif k == 'lax_validation': 151 flags |= gobject.PARAM_LAX_VALIDATION 152 else: 153 raise Exception('Invalid GObject property flag: %r=%r' % (k, v)) 154 155 _dict[name] = (type_, name, desc) + args + tuple((flags, )) 156 157
158 -def type_register(klass):
159 if klass.__gtype__.pytype is not klass: 160 # all subclasses will at least have a __gtype__ from their 161 # parent, make sure it corresponds to the exact class 162 gobject.type_register(klass)
163