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):
75 if '[[deprecated]]' in docString:
77 if default
is not None:
89 return (obj.getJobOptName()
or obj.getName()) +
'.' + self.descr.__name__
93 return self.descr.__get__(obj, type)
94 except AttributeError:
97 if self.__default.__class__
in [list, dict]:
99 obj, self.__default.__class__(self.
__default))
100 return self.descr.__get__(obj, type)
109 log.warning(
'Property %s is deprecated: %s',
113 proptype, allowcompat =
None,
False 114 if hasattr(self,
'default'):
116 if self.descr.__name__ ==
'OutputLevel':
125 if proptype
and proptype !=
type(
None)
and \
134 oldvec = self.descr.__get__(obj, type)
136 tpo =
type(oldvec[0])
139 except AttributeError:
142 except ValueError, e:
144 log.error(
'inconsistent value types for %s.%s (%s)' %
145 (obj.getName(), self.descr.__name__, str(e)))
151 if not obj._isInSetDefaults()
or not obj
in self.
history:
153 if hasattr(self,
'default')
and self.
default ==
None:
154 obj.__iadd__(value, self.
descr)
156 self.descr.__set__(obj, value)
157 self.history.setdefault(obj, []).append(value)
162 self.descr.__delete__(obj)
166 """A class with some utilities for GaudiHandles and GaudiHandleArrays""" 168 def __init__(self, descr, docString, default, handleType, allowedType):
169 """<descr>: the real property in the object instance (from __slots__) 170 <docString>: the documentation string of this property 171 <default>: default value from C++ (via python generated by genconf) 172 <handleType>: real python handle type (e.g. PublicToolHandle, PrivateToolHandle, ...) 173 <allowedType>: allowed instance type for default 176 if not isinstance(default, allowedType):
177 raise TypeError(
"%s: %s default: %r is not a %s" %
178 (descr.__name__, self.__class__.__name__, default, allowedType.__name__))
179 PropertyProxy.__init__(self, descr, docString, default)
186 return self.descr.__get__(obj, type)
187 except AttributeError:
190 default = obj.__class__.getDefaultProperty(self.descr.__name__)
194 except AttributeError, e:
196 raise RuntimeError(*e.args)
198 return self.descr.__get__(obj, type)
203 if not obj._isInSetDefaults()
or not obj
in self.
history:
206 self.descr.__set__(obj, value)
208 self.history.setdefault(obj, []).append(value)
211 """Check if <value> is a handle of the correct type""" 215 """Check if <value> is a configurable of the correct type""" 219 """Return the configurable instance corresponding to the toolhandle if possible. 220 Otherwise return None""" 223 typeAndNameTuple = typeAndName.split(
'/')
224 confType = typeAndNameTuple[0]
225 confClass = ConfigurableDb.getConfigurable(confType)
228 log.error(
"%s: Configurable %s is not a %s",
232 confName = typeAndNameTuple[1]
236 return confClass(confName)
240 isString =
type(default) == str
241 if not isString
and self.
isConfig(default):
244 elif isString
or self.
isHandle(default):
247 typeAndName = default
250 typeAndName = default.typeAndName
251 if not self._handleType.isPublic:
259 except AttributeError, e:
261 raise RuntimeError(*e.args)
263 raise RuntimeError(
"%s: Default configurable for class %s not found in ConfigurableDb.CfgDb" %
267 raise TypeError(
"%s: default value %r is not of type %s or %s" %
275 isString =
type(value) == str
283 if self._handleType.isPublic:
286 if not value.isInToolSvc():
287 suggestion =
'You may need to add jobOptions lines something like:' + os.linesep + \
288 'from AthenaCommon.AppMgr import ToolSvc' + os.linesep + \
290 if value.getName() == value.getType():
291 suggestion +=
'%s()' % value.__class__.__name__
293 suggestion +=
'%s(%r)' % (value.__class__.__name__,
296 ': Public tool %s is not yet in ToolSvc. %s' %
297 (value.getJobOptName(), suggestion))
300 elif value.hasParent(obj.getJobOptName()):
305 value = obj.copyChildAndSetParent(value, obj.getJobOptName())
307 obj.allConfigurables[value.name()] = value
310 raise TypeError(
"Property %s value %r is not a %s nor a %s nor a string" %
318 GaudiHandlePropertyProxyBase.__init__(
319 self, descr, docString, default,
type(default), GaudiHandle)
324 """<descr>: the real property in the object instance (from __slots__) 325 <confTypeName>: string indicating the (base) class of allowed Configurables to be assigned. 326 <handleType>: real python handle type (e.g. PublicToolHandle, PrivateToolHandle, ...) 328 GaudiHandlePropertyProxyBase.__init__(
329 self, descr, docString, default,
type(default).handleType, GaudiHandleArray)
333 if not isinstance(value, list)
and not isinstance(value, self.
arrayType):
334 raise TypeError(
"%s: Value %r is not a list nor a %s" %
341 cd = GaudiHandlePropertyProxyBase.convertDefaultToBeSet(
344 newDefault.append(cd)
352 cv = GaudiHandlePropertyProxyBase.convertValueToBeSet(self, obj, v)
362 PropertyProxy.__init__(self, descr, docString, default)
366 return self.descr.__get__(obj, type)
367 except AttributeError:
370 default = obj.__class__.getDefaultProperty(self.descr.__name__)
374 except AttributeError, e:
376 raise RuntimeError(*e.args)
378 return self.descr.__get__(obj, type)
381 if not obj._isInSetDefaults()
or not obj
in self.
history:
384 self.descr.__set__(obj, value)
386 self.history.setdefault(obj, []).append(value)
392 if type(value) == str:
394 elif isinstance(value, DataObjectHandleBase):
397 raise ValueError(
"received an instance of %s, but %s expected" %
398 (
type(value),
'str or DataObjectHandleBase'))
404 if isinstance(default, GaudiHandleArray):
407 if isinstance(default, GaudiHandle):
410 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)