16 __all__ = [
"PropertyProxy",
"GaudiHandlePropertyProxy",
"GaudiHandleArrayPropertyProxy"]
24 from GaudiKernel
import ConfigurableDb
26 log = logging.getLogger(
"PropertyProxy")
30 """A string version of isinstance().
31 <derived> is either an object instance, or a type
32 <base> is a string containing the name of the base class (or <derived> class)"""
33 if not isinstance(derived, type):
34 derived =
type(derived)
35 if derived.__name__ == base:
37 for b
in derived.__bases__:
45 errmsg =
"received an instance of %s, but %s expected" % (
type(value), tp)
54 elif isinstance(value, DataHandle):
59 raise ValueError(errmsg)
60 elif tp
in (list, tuple, dict, set):
64 elif tp
is set
and isinstance(value, list):
68 raise ValueError(errmsg)
75 except (TypeError, ValueError):
76 raise ValueError(errmsg)
82 def __init__(self, descr, docString=None, default=None):
88 if "[[deprecated]]" in docString:
90 if default
is not None:
99 default = property(getDefault, setDefault)
102 return (obj.getJobOptName()
or obj.getName()) +
"." + self.
descr.__name__
107 except AttributeError:
110 if isinstance(self.
__default, (list, dict, set)):
122 "Property %s is deprecated: %s",
128 proptype, allowcompat =
None,
False
129 if hasattr(self,
"default"):
131 if self.
descr.__name__ ==
"OutputLevel":
142 and not isinstance(
None, proptype)
154 tpo =
type(oldvec[0])
157 except AttributeError:
160 except ValueError
as e:
163 "inconsistent value types for %s.%s (%s)"
164 % (obj.getName(), self.
descr.__name__, str(e))
171 if not obj._isInSetDefaults()
or obj
not in self.
history:
173 if hasattr(self,
"default")
and self.
default is None:
174 obj.__iadd__(value, self.
descr)
177 self.
history.setdefault(obj, []).append(value)
186 """A class with some utilities for GaudiHandles and GaudiHandleArrays"""
188 def __init__(self, descr, docString, default, handleType, allowedType):
189 """<descr>: the real property in the object instance (from __slots__)
190 <docString>: the documentation string of this property
191 <default>: default value from C++ (via python generated by genconf)
192 <handleType>: real python handle type (e.g. PublicToolHandle, PrivateToolHandle, ...)
193 <allowedType>: allowed instance type for default
196 if not isinstance(default, allowedType):
198 "%s: %s default: %r is not a %s"
201 self.__class__.__name__,
203 allowedType.__name__,
206 PropertyProxy.__init__(self, descr, docString, default)
213 except AttributeError:
216 default = obj.__class__.getDefaultProperty(self.
descr.__name__)
218 if default
is not None:
221 except AttributeError
as e:
223 raise RuntimeError(*e.args)
230 if not obj._isInSetDefaults()
or obj
not in self.
history:
235 self.
history.setdefault(obj, []).append(value)
238 """Check if <value> is a handle of the correct type"""
242 """Check if <value> is a configurable of the correct type"""
246 """Return the configurable instance corresponding to the toolhandle if possible.
247 Otherwise return None"""
250 typeAndNameTuple = typeAndName.split(
"/")
251 confType = typeAndNameTuple[0]
252 confClass = ConfigurableDb.getConfigurable(confType)
256 "%s: Configurable %s is not a %s",
263 confName = typeAndNameTuple[1]
267 return confClass(confName)
271 isString = isinstance(default, str)
272 if not isString
and self.
isConfig(default):
275 elif isString
or self.
isHandle(default):
278 typeAndName = default
281 typeAndName = default.typeAndName
288 if "/" in typeAndName:
289 typeAndName = typeAndName.replace(
"/",
"/{}.".
format(obj.name()), 1)
291 typeAndName =
"{0}/{1}.{0}".
format(typeAndName, obj.name())
298 except AttributeError
as e:
300 raise RuntimeError(*e.args)
303 "%s: Default configurable for class %s not found in ConfigurableDb.CfgDb"
309 "%s: default value %r is not of type %s or %s"
323 isString = isinstance(value, str)
334 if not value.isInToolSvc():
336 "You may need to add jobOptions lines something like:"
338 +
"from AthenaCommon.AppMgr import ToolSvc"
342 if value.getName() == value.getType():
343 suggestion +=
"%s()" % value.__class__.__name__
345 suggestion +=
"%s(%r)" % (
346 value.__class__.__name__,
351 +
": Public tool %s is not yet in ToolSvc. %s"
352 % (value.getJobOptName(), suggestion)
356 elif value.hasParent(obj.getJobOptName()):
361 value = obj.copyChildAndSetParent(value, obj.getJobOptName())
363 obj.allConfigurables[value.name()] = value
367 "Property %s value %r is not a %s nor a %s nor a string"
381 GaudiHandlePropertyProxyBase.__init__(
382 self, descr, docString, default,
type(default), GaudiHandle
388 """<descr>: the real property in the object instance (from __slots__)
389 <confTypeName>: string indicating the (base) class of allowed Configurables to be assigned.
390 <handleType>: real python handle type (e.g. PublicToolHandle, PrivateToolHandle, ...)
392 GaudiHandlePropertyProxyBase.__init__(
393 self, descr, docString, default,
type(default).handleType, GaudiHandleArray
398 if not isinstance(value, list)
and not isinstance(value, self.
arrayType):
400 "%s: Value %r is not a list nor a %s"
408 cd = GaudiHandlePropertyProxyBase.convertDefaultToBeSet(self, obj, d)
410 newDefault.append(cd)
418 cv = GaudiHandlePropertyProxyBase.convertValueToBeSet(self, obj, v)
427 PropertyProxy.__init__(self, descr, docString, default)
432 except AttributeError:
435 default = obj.__class__.getDefaultProperty(self.
descr.__name__)
439 except AttributeError
as e:
441 raise RuntimeError(*e.args)
446 if not obj._isInSetDefaults()
or obj
not in self.
history:
451 self.
history.setdefault(obj, []).append(value)
457 mode = obj.__class__.getDefaultProperty(self.
descr.__name__).mode()
458 _type = obj.__class__.getDefaultProperty(self.
descr.__name__).
type()
459 if isinstance(value, str):
461 elif isinstance(value, DataHandle):
462 return DataHandle(value.__str__(), mode, _type)
465 "received an instance of %s, but %s expected"
466 % (
type(value),
"str or DataHandle")
473 if isinstance(default, GaudiHandleArray):
476 if isinstance(default, GaudiHandle):
479 if isinstance(default, DataHandle):