6 __all__ = [
'PropertyProxy',
'GaudiHandlePropertyProxy',
7 'GaudiHandleArrayPropertyProxy']
12 from GaudiKernel
import ConfigurableDb
16 log = logging.getLogger(
'PropertyProxy')
23 """A string version of isinstance(). 24 <derived> is either an object instance, or a type 25 <base> is a string containing the name of the base class (or <derived> class)""" 26 if not isinstance(derived, type):
27 derived =
type(derived)
28 if derived.__name__ == base:
30 for b
in derived.__bases__:
38 errmsg =
"received an instance of %s, but %s expected" % (
type(value), tp)
48 raise ValueError(errmsg)
49 elif (tp
in [list, tuple, dict]):
50 if (
type(value)
is tp):
55 raise ValueError(errmsg)
62 except (TypeError, ValueError):
63 raise ValueError(errmsg)
69 def __init__(self, descr, docString=None, default=None):
74 if default
is not None:
86 return (obj.getJobOptName()
or obj.getName()) +
'.' + self.descr.__name__
90 return self.descr.__get__(obj, type)
91 except AttributeError:
94 if self.__default.__class__
in [list, dict]:
96 obj, self.__default.__class__(self.
__default))
97 return self.descr.__get__(obj, type)
105 proptype, allowcompat =
None,
False 106 if hasattr(self,
'default'):
108 if self.descr.__name__ ==
'OutputLevel':
117 if proptype
and proptype !=
type(
None)
and \
126 oldvec = self.descr.__get__(obj, type)
128 tpo =
type(oldvec[0])
131 except AttributeError:
134 except ValueError, e:
136 log.error(
'inconsistent value types for %s.%s (%s)' %
137 (obj.getName(), self.descr.__name__, str(e)))
143 if not obj._isInSetDefaults()
or not obj
in self.
history:
145 if hasattr(self,
'default')
and self.
default ==
None:
146 obj.__iadd__(value, self.
descr)
148 self.descr.__set__(obj, value)
149 self.history.setdefault(obj, []).append(value)
154 self.descr.__delete__(obj)
158 """A class with some utilities for GaudiHandles and GaudiHandleArrays""" 160 def __init__(self, descr, docString, default, handleType, allowedType):
161 """<descr>: the real property in the object instance (from __slots__) 162 <docString>: the documentation string of this property 163 <default>: default value from C++ (via python generated by genconf) 164 <handleType>: real python handle type (e.g. PublicToolHandle, PrivateToolHandle, ...) 165 <allowedType>: allowed instance type for default 168 if not isinstance(default, allowedType):
169 raise TypeError(
"%s: %s default: %r is not a %s" %
170 (descr.__name__, self.__class__.__name__, default, allowedType.__name__))
171 PropertyProxy.__init__(self, descr, docString, default)
178 return self.descr.__get__(obj, type)
179 except AttributeError:
182 default = obj.__class__.getDefaultProperty(self.descr.__name__)
186 except AttributeError, e:
188 raise RuntimeError(*e.args)
190 return self.descr.__get__(obj, type)
195 if not obj._isInSetDefaults()
or not obj
in self.
history:
198 self.descr.__set__(obj, value)
200 self.history.setdefault(obj, []).append(value)
203 """Check if <value> is a handle of the correct type""" 207 """Check if <value> is a configurable of the correct type""" 211 """Return the configurable instance corresponding to the toolhandle if possible. 212 Otherwise return None""" 215 typeAndNameTuple = typeAndName.split(
'/')
216 confType = typeAndNameTuple[0]
217 confClass = ConfigurableDb.getConfigurable(confType)
220 log.error(
"%s: Configurable %s is not a %s",
224 confName = typeAndNameTuple[1]
228 return confClass(confName)
232 isString =
type(default) == str
233 if not isString
and self.
isConfig(default):
236 elif isString
or self.
isHandle(default):
239 typeAndName = default
242 typeAndName = default.typeAndName
243 if not self._handleType.isPublic:
251 except AttributeError, e:
253 raise RuntimeError(*e.args)
255 raise RuntimeError(
"%s: Default configurable for class %s not found in ConfigurableDb.CfgDb" %
259 raise TypeError(
"%s: default value %r is not of type %s or %s" %
267 isString =
type(value) == str
275 if self._handleType.isPublic:
278 if not value.isInToolSvc():
279 suggestion =
'You may need to add jobOptions lines something like:' + os.linesep + \
280 'from AthenaCommon.AppMgr import ToolSvc' + os.linesep + \
282 if value.getName() == value.getType():
283 suggestion +=
'%s()' % value.__class__.__name__
285 suggestion +=
'%s(%r)' % (value.__class__.__name__,
288 ': Public tool %s is not yet in ToolSvc. %s' %
289 (value.getJobOptName(), suggestion))
292 elif value.hasParent(obj.getJobOptName()):
297 value = obj.copyChildAndSetParent(value, obj.getJobOptName())
299 obj.allConfigurables[value.name()] = value
302 raise TypeError(
"Property %s value %r is not a %s nor a %s nor a string" %
310 GaudiHandlePropertyProxyBase.__init__(
311 self, descr, docString, default,
type(default), GaudiHandle)
316 """<descr>: the real property in the object instance (from __slots__) 317 <confTypeName>: string indicating the (base) class of allowed Configurables to be assigned. 318 <handleType>: real python handle type (e.g. PublicToolHandle, PrivateToolHandle, ...) 320 GaudiHandlePropertyProxyBase.__init__(
321 self, descr, docString, default,
type(default).handleType, GaudiHandleArray)
325 if not isinstance(value, list)
and not isinstance(value, self.
arrayType):
326 raise TypeError(
"%s: Value %r is not a list nor a %s" %
333 cd = GaudiHandlePropertyProxyBase.convertDefaultToBeSet(
336 newDefault.append(cd)
344 cv = GaudiHandlePropertyProxyBase.convertValueToBeSet(self, obj, v)
354 PropertyProxy.__init__(self, descr, docString, default)
358 return self.descr.__get__(obj, type)
359 except AttributeError:
362 default = obj.__class__.getDefaultProperty(self.descr.__name__)
366 except AttributeError, e:
368 raise RuntimeError(*e.args)
370 return self.descr.__get__(obj, type)
373 if not obj._isInSetDefaults()
or not obj
in self.
history:
376 self.descr.__set__(obj, value)
378 self.history.setdefault(obj, []).append(value)
384 if type(value) == str:
386 elif isinstance(value, DataObjectHandleBase):
393 if isinstance(default, GaudiHandleArray):
396 if isinstance(default, GaudiHandle):
399 if isinstance(default, DataObjectHandleBase):
def convertValueToBeSet(self, obj, value)
def getDefaultConfigurable(self, typeAndName, requester)
def __delete__(self, obj)
def convertDefaultToBeSet(self, obj, default)
def isConfig(self, value)
def isHandle(self, value)
def __get__(self, obj, type=None)
Gaudi::Details::PropertyBase * property(const std::string &name) const
def checkType(self, obj, value)
def convertValueToBeSet(self, obj, value)
def convertValueToBeSet(self, obj, value)
def __set__(self, obj, value)
def __get__(self, obj, type=None)
def __init__(self, descr, docString=None, default=None)
def __init__(self, descr, docString, default)
def __set__(self, obj, value)
def __init__(self, descr, docString, default, handleType, allowedType)
def __init__(self, descr, docString, default)
def __set__(self, obj, value)
def __get__(self, obj, type=None)
def derives_from(derived, base)
def PropertyProxyFactory(descr, doc, default)
def __init__(self, descr, docString, default)
def fullPropertyName(self, obj)
def convertDefaultToBeSet(self, obj, default)
def _isCompatible(tp, value)
def setDefault(self, value)