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__:
45 f
"received an instance of {type(value)}, but {tp} expected for property {name}"
52 if isinstance(value, str)
or derives_from(value,
"Configurable"):
55 elif isinstance(value, DataHandle):
60 raise ValueError(errmsg)
61 elif tp
in (list, tuple, dict, set):
65 elif tp
is set
and isinstance(value, list):
69 raise ValueError(errmsg)
76 except (TypeError, ValueError):
77 raise ValueError(errmsg)
83 allConfigurables[c.name()] = c
84 for cc
in c.getAllChildren():
90 def __init__(self, descr, docString=None, default=None):
96 if "[[deprecated]]" in docString:
98 if default
is not None:
107 default = property(getDefault, setDefault)
110 return (obj.getJobOptName()
or obj.getName()) +
"." + self.
descr.__name__
115 except AttributeError:
118 if isinstance(self.
__default, (list, dict, set)):
130 "Property %s is deprecated: %s",
136 proptype, allowcompat =
None,
False
137 if hasattr(self,
"default"):
139 if self.
descr.__name__ ==
"OutputLevel":
150 and not isinstance(
None, proptype)
162 tpo =
type(oldvec[0])
165 except AttributeError:
168 except ValueError
as e:
171 "inconsistent value types for %s.%s (%s)"
172 % (obj.getName(), self.
descr.__name__, str(e))
179 if not obj._isInSetDefaults()
or obj
not in self.
history:
181 if hasattr(self,
"default")
and self.
default is None:
182 obj.__iadd__(value, self.
descr)
185 self.
history.setdefault(obj, []).append(value)
194 """A class with some utilities for GaudiHandles and GaudiHandleArrays"""
196 def __init__(self, descr, docString, default, handleType, allowedType):
197 """<descr>: the real property in the object instance (from __slots__)
198 <docString>: the documentation string of this property
199 <default>: default value from C++ (via python generated by genconf)
200 <handleType>: real python handle type (e.g. PublicToolHandle, PrivateToolHandle, ...)
201 <allowedType>: allowed instance type for default
204 if not isinstance(default, allowedType):
206 "%s: %s default: %r is not a %s"
209 self.__class__.__name__,
211 allowedType.__name__,
214 PropertyProxy.__init__(self, descr, docString, default)
221 except AttributeError:
224 default = obj.__class__.getDefaultProperty(self.
descr.__name__)
226 if default
is not None:
229 except AttributeError
as e:
231 raise RuntimeError(*e.args)
238 if not obj._isInSetDefaults()
or obj
not in self.
history:
243 self.
history.setdefault(obj, []).append(value)
246 """Check if <value> is a handle of the correct type"""
250 """Check if <value> is a configurable of the correct type"""
254 """Return the configurable instance corresponding to the toolhandle if possible.
255 Otherwise return None"""
258 typeAndNameTuple = typeAndName.split(
"/")
259 confType = typeAndNameTuple[0]
260 confClass = ConfigurableDb.getConfigurable(confType)
264 "%s: Configurable %s is not a %s",
271 confName = typeAndNameTuple[1]
275 return confClass(confName)
279 isString = isinstance(default, str)
280 if not isString
and self.
isConfig(default):
283 elif isString
or self.
isHandle(default):
286 typeAndName = default
289 typeAndName = default.typeAndName
296 if "/" in typeAndName:
297 typeAndName = typeAndName.replace(
"/",
"/{}.".
format(obj.name()), 1)
299 typeAndName =
"{0}/{1}.{0}".
format(typeAndName, obj.name())
306 except AttributeError
as e:
308 raise RuntimeError(*e.args)
311 "%s: Default configurable for class %s not found in ConfigurableDb.CfgDb"
317 "%s: default value %r is not of type %s or %s"
331 isString = isinstance(value, str)
342 if not value.isInToolSvc():
344 "You may need to add jobOptions lines something like:"
346 +
"from AthenaCommon.AppMgr import ToolSvc"
350 if value.getName() == value.getType():
351 suggestion +=
"%s()" % value.__class__.__name__
353 suggestion +=
"%s(%r)" % (
354 value.__class__.__name__,
359 +
": Public tool %s is not yet in ToolSvc. %s"
360 % (value.getJobOptName(), suggestion)
364 elif value.hasParent(obj.getJobOptName()):
369 value = obj.copyChildAndSetParent(value, obj.getJobOptName())
376 "Property %s value %r is not a %s nor a %s nor a string"
390 GaudiHandlePropertyProxyBase.__init__(
391 self, descr, docString, default,
type(default), GaudiHandle
397 """<descr>: the real property in the object instance (from __slots__)
398 <confTypeName>: string indicating the (base) class of allowed Configurables to be assigned.
399 <handleType>: real python handle type (e.g. PublicToolHandle, PrivateToolHandle, ...)
401 GaudiHandlePropertyProxyBase.__init__(
402 self, descr, docString, default,
type(default).handleType, GaudiHandleArray
407 if not isinstance(value, list)
and not isinstance(value, self.
arrayType):
409 "%s: Value %r is not a list nor a %s"
417 cd = GaudiHandlePropertyProxyBase.convertDefaultToBeSet(self, obj, d)
419 newDefault.append(cd)
427 cv = GaudiHandlePropertyProxyBase.convertValueToBeSet(self, obj, v)
436 PropertyProxy.__init__(self, descr, docString, default)
441 except AttributeError:
444 default = obj.__class__.getDefaultProperty(self.
descr.__name__)
448 except AttributeError
as e:
450 raise RuntimeError(*e.args)
455 if not obj._isInSetDefaults()
or obj
not in self.
history:
460 self.
history.setdefault(obj, []).append(value)
466 mode = obj.__class__.getDefaultProperty(self.
descr.__name__).mode()
467 _type = obj.__class__.getDefaultProperty(self.
descr.__name__).
type()
468 if isinstance(value, str):
470 elif isinstance(value, DataHandle):
471 return DataHandle(value.__str__(), mode, _type)
474 "received an instance of %s, but %s expected"
475 % (
type(value),
"str or DataHandle")
482 if isinstance(default, GaudiHandleArray):
485 if isinstance(default, GaudiHandle):
488 if isinstance(default, DataHandle):