16 __all__ = [
"PropertyProxy",
"GaudiHandlePropertyProxy",
"GaudiHandleArrayPropertyProxy"]
21 from GaudiKernel
import ConfigurableDb
25 log = logging.getLogger(
"PropertyProxy")
29 """A string version of isinstance().
30 <derived> is either an object instance, or a type
31 <base> is a string containing the name of the base class (or <derived> class)"""
32 if not isinstance(derived, type):
33 derived =
type(derived)
34 if derived.__name__ == base:
36 for b
in derived.__bases__:
44 errmsg =
"received an instance of %s, but %s expected" % (
type(value), tp)
50 if isinstance(value, str)
or derives_from(value,
"Configurable"):
53 elif isinstance(value, DataHandle):
58 raise ValueError(errmsg)
59 elif tp
in (list, tuple, dict, set):
63 elif tp
is set
and isinstance(value, list):
67 raise ValueError(errmsg)
74 except (TypeError, ValueError):
75 raise ValueError(errmsg)
81 def __init__(self, descr, docString=None, default=None):
87 if "[[deprecated]]" in docString:
89 if default
is not None:
98 default = property(getDefault, setDefault)
101 return (obj.getJobOptName()
or obj.getName()) +
"." + self.
descr.__name__
106 except AttributeError:
109 if isinstance(self.
__default, (list, dict, set)):
121 "Property %s is deprecated: %s",
127 proptype, allowcompat =
None,
False
128 if hasattr(self,
"default"):
130 if self.
descr.__name__ ==
"OutputLevel":
141 and not isinstance(
None, proptype)
153 tpo =
type(oldvec[0])
156 except AttributeError:
159 except ValueError
as e:
162 "inconsistent value types for %s.%s (%s)"
163 % (obj.getName(), self.
descr.__name__, str(e))
170 if not obj._isInSetDefaults()
or obj
not in self.
history:
172 if hasattr(self,
"default")
and self.
default is None:
173 obj.__iadd__(value, self.
descr)
176 self.
history.setdefault(obj, []).append(value)
185 """A class with some utilities for GaudiHandles and GaudiHandleArrays"""
187 def __init__(self, descr, docString, default, handleType, allowedType):
188 """<descr>: the real property in the object instance (from __slots__)
189 <docString>: the documentation string of this property
190 <default>: default value from C++ (via python generated by genconf)
191 <handleType>: real python handle type (e.g. PublicToolHandle, PrivateToolHandle, ...)
192 <allowedType>: allowed instance type for default
195 if not isinstance(default, allowedType):
197 "%s: %s default: %r is not a %s"
200 self.__class__.__name__,
202 allowedType.__name__,
205 PropertyProxy.__init__(self, descr, docString, default)
212 except AttributeError:
215 default = obj.__class__.getDefaultProperty(self.
descr.__name__)
217 if default
is not None:
220 except AttributeError
as e:
222 raise RuntimeError(*e.args)
229 if not obj._isInSetDefaults()
or obj
not in self.
history:
234 self.
history.setdefault(obj, []).append(value)
237 """Check if <value> is a handle of the correct type"""
241 """Check if <value> is a configurable of the correct type"""
245 """Return the configurable instance corresponding to the toolhandle if possible.
246 Otherwise return None"""
249 typeAndNameTuple = typeAndName.split(
"/")
250 confType = typeAndNameTuple[0]
251 confClass = ConfigurableDb.getConfigurable(confType)
255 "%s: Configurable %s is not a %s",
262 confName = typeAndNameTuple[1]
266 return confClass(confName)
270 isString = isinstance(default, str)
271 if not isString
and self.
isConfig(default):
274 elif isString
or self.
isHandle(default):
277 typeAndName = default
280 typeAndName = default.typeAndName
287 if "/" in typeAndName:
288 typeAndName = typeAndName.replace(
"/",
"/{}.".
format(obj.name()), 1)
290 typeAndName =
"{0}/{1}.{0}".
format(typeAndName, obj.name())
297 except AttributeError
as e:
299 raise RuntimeError(*e.args)
302 "%s: Default configurable for class %s not found in ConfigurableDb.CfgDb"
308 "%s: default value %r is not of type %s or %s"
322 isString = isinstance(value, str)
333 if not value.isInToolSvc():
335 "You may need to add jobOptions lines something like:"
337 +
"from AthenaCommon.AppMgr import ToolSvc"
341 if value.getName() == value.getType():
342 suggestion +=
"%s()" % value.__class__.__name__
344 suggestion +=
"%s(%r)" % (
345 value.__class__.__name__,
350 +
": Public tool %s is not yet in ToolSvc. %s"
351 % (value.getJobOptName(), suggestion)
355 elif value.hasParent(obj.getJobOptName()):
360 value = obj.copyChildAndSetParent(value, obj.getJobOptName())
362 obj.allConfigurables[value.name()] = value
366 "Property %s value %r is not a %s nor a %s nor a string"
380 GaudiHandlePropertyProxyBase.__init__(
381 self, descr, docString, default,
type(default), GaudiHandle
387 """<descr>: the real property in the object instance (from __slots__)
388 <confTypeName>: string indicating the (base) class of allowed Configurables to be assigned.
389 <handleType>: real python handle type (e.g. PublicToolHandle, PrivateToolHandle, ...)
391 GaudiHandlePropertyProxyBase.__init__(
392 self, descr, docString, default,
type(default).handleType, GaudiHandleArray
397 if not isinstance(value, list)
and not isinstance(value, self.
arrayType):
399 "%s: Value %r is not a list nor a %s"
407 cd = GaudiHandlePropertyProxyBase.convertDefaultToBeSet(self, obj, d)
409 newDefault.append(cd)
417 cv = GaudiHandlePropertyProxyBase.convertValueToBeSet(self, obj, v)
426 PropertyProxy.__init__(self, descr, docString, default)
431 except AttributeError:
434 default = obj.__class__.getDefaultProperty(self.
descr.__name__)
438 except AttributeError
as e:
440 raise RuntimeError(*e.args)
445 if not obj._isInSetDefaults()
or obj
not in self.
history:
450 self.
history.setdefault(obj, []).append(value)
456 mode = obj.__class__.getDefaultProperty(self.
descr.__name__).mode()
457 _type = obj.__class__.getDefaultProperty(self.
descr.__name__).
type()
458 if isinstance(value, str):
460 elif isinstance(value, DataHandle):
461 return DataHandle(value.__str__(), mode, _type)
464 "received an instance of %s, but %s expected"
465 % (
type(value),
"str or DataHandle")
472 if isinstance(default, GaudiHandleArray):
475 if isinstance(default, GaudiHandle):
478 if isinstance(default, DataHandle):