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