The Gaudi Framework  v41r0 (bdbe7744)
Loading...
Searching...
No Matches
GaudiKernel.PropertyProxy.GaudiHandlePropertyProxyBase Class Reference
Inheritance diagram for GaudiKernel.PropertyProxy.GaudiHandlePropertyProxyBase:
Collaboration diagram for GaudiKernel.PropertyProxy.GaudiHandlePropertyProxyBase:

Public Member Functions

 __init__ (self, descr, docString, default, handleType, allowedType)
 __get__ (self, obj, type=None)
 __set__ (self, obj, value)
 isHandle (self, value)
 isConfig (self, value)
 getDefaultConfigurable (self, typeAndName, requester)
 convertDefaultToBeSet (self, obj, default)
 convertValueToBeSet (self, obj, value)
Public Member Functions inherited from GaudiKernel.PropertyProxy.PropertyProxy
 __init__ (self, descr, docString=None, default=None)
 setDefault (self, value)
 getDefault (self)
 fullPropertyName (self, obj)
 __get__ (self, obj, type=None)
 __set__ (self, obj, value)
 __delete__ (self, obj)

Protected Attributes

 _handleType = handleType
str _confTypeName = "Configurable" + handleType.componentType

Additional Inherited Members

Public Attributes inherited from GaudiKernel.PropertyProxy.PropertyProxy
dict history = {}
 descr = descr
bool deprecated = False
Properties inherited from GaudiKernel.PropertyProxy.PropertyProxy
 default = property(getDefault, setDefault)

Detailed Description

A class with some utilities for GaudiHandles and GaudiHandleArrays

Definition at line 192 of file PropertyProxy.py.

Constructor & Destructor Documentation

◆ __init__()

GaudiKernel.PropertyProxy.GaudiHandlePropertyProxyBase.__init__ ( self,
descr,
docString,
default,
handleType,
allowedType )
<descr>: the real property in the object instance (from __slots__)
<docString>: the documentation string of this property
<default>: default value from C++ (via python generated by genconf)
<handleType>: real python handle type (e.g. PublicToolHandle, PrivateToolHandle, ...)
<allowedType>: allowed instance type for default

Definition at line 195 of file PropertyProxy.py.

195 def __init__(self, descr, docString, default, handleType, allowedType):
196 """<descr>: the real property in the object instance (from __slots__)
197 <docString>: the documentation string of this property
198 <default>: default value from C++ (via python generated by genconf)
199 <handleType>: real python handle type (e.g. PublicToolHandle, PrivateToolHandle, ...)
200 <allowedType>: allowed instance type for default
201 """
202 # check that default is of allowed type for this proxy
203 if not isinstance(default, allowedType):
204 raise TypeError(
205 f"{descr.__name__}: {self.__class__.__name__} default: {default!r} is not a {allowedType.__name__}"
206 )
207 PropertyProxy.__init__(self, descr, docString, default)
208 self._handleType = handleType
209 self._confTypeName = "Configurable" + handleType.componentType
210

Member Function Documentation

◆ __get__()

GaudiKernel.PropertyProxy.GaudiHandlePropertyProxyBase.__get__ ( self,
obj,
type = None )

Definition at line 211 of file PropertyProxy.py.

211 def __get__(self, obj, type=None):
212 try:
213 return self.descr.__get__(obj, type)
214 except AttributeError:
215 # Get default
216 try:
217 default = obj.__class__.getDefaultProperty(self.descr.__name__)
218 default = self.convertDefaultToBeSet(obj, default)
219 if default is not None:
220 self.__set__(obj, default)
221
222 except AttributeError as e:
223 # change type of exception to avoid false error message
224 raise RuntimeError(*e.args)
225
226 return self.descr.__get__(obj, type)
227

◆ __set__()

GaudiKernel.PropertyProxy.GaudiHandlePropertyProxyBase.__set__ ( self,
obj,
value )

Definition at line 228 of file PropertyProxy.py.

228 def __set__(self, obj, value):
229 # allow a property to be set if we're in non-default mode, or if it
230 # simply hasn't been set before
231 if not obj._isInSetDefaults() or obj not in self.history:
232 value = self.convertValueToBeSet(obj, value)
233 # assign the value
234 self.descr.__set__(obj, value)
235 log.debug("Setting %s = %r", self.fullPropertyName(obj), value)
236 self.history.setdefault(obj, []).append(value)
237

◆ convertDefaultToBeSet()

GaudiKernel.PropertyProxy.GaudiHandlePropertyProxyBase.convertDefaultToBeSet ( self,
obj,
default )

Reimplemented in GaudiKernel.PropertyProxy.GaudiHandleArrayPropertyProxy.

Definition at line 270 of file PropertyProxy.py.

270 def convertDefaultToBeSet(self, obj, default):
271 # turn string into handle
272 isString = isinstance(default, str)
273 if not isString and self.isConfig(default):
274 # print self.fullPropertyName(obj) + ": Setting default configurable: %r" % default
275 return default
276 elif isString or self.isHandle(default):
277 if isString:
278 # convert string into handle
279 typeAndName = default
280 default = self._handleType(typeAndName)
281 else:
282 typeAndName = default.typeAndName
283 if not self._handleType.isPublic:
284 if not typeAndName:
285 return None
286 # Find corresponding default configurable of private handles
287 # (make sure the name used to instantiate the private tool
288 # includes the name of the owner, see https://gitlab.cern.ch/gaudi/Gaudi/-/issues/141)
289 if "/" in typeAndName:
290 typeAndName = typeAndName.replace("/", f"/{obj.name()}.", 1)
291 else:
292 typeAndName = f"{typeAndName}/{obj.name()}.{typeAndName}"
293 try:
294 conf = self.getDefaultConfigurable(
295 typeAndName, self.fullPropertyName(obj)
296 )
297
298 # print self.fullPropertyName(obj) + ": Setting default private configurable (from default handle): %r" % conf
299 except AttributeError as e:
300 # change type of exception to avoid false error message
301 raise RuntimeError(*e.args)
302 if conf is None:
303 raise RuntimeError(
304 f"{self.fullPropertyName(obj)}: Default configurable for class {default.getType()} not found in ConfigurableDb.CfgDb"
305 )
306 return conf
307 else: # not a config, not a handle, not a string
308 raise TypeError(
309 f"{self.fullPropertyName(obj)}: default value {default!r} is not of type {self._confTypeName} or {self._handleType.__name__}"
310 )
311
312 return default
313

◆ convertValueToBeSet()

GaudiKernel.PropertyProxy.GaudiHandlePropertyProxyBase.convertValueToBeSet ( self,
obj,
value )

Reimplemented in GaudiKernel.PropertyProxy.GaudiHandleArrayPropertyProxy.

Definition at line 314 of file PropertyProxy.py.

314 def convertValueToBeSet(self, obj, value):
315 if value is None:
316 value = ""
317 isString = isinstance(value, str)
318 if isString:
319 # create an new handle
320 return self._handleType(value)
321 elif self.isHandle(value):
322 # make a copy of the handle
323 return self._handleType(value.toStringProperty())
324 elif self.isConfig(value):
325 if self._handleType.isPublic:
326 # A public tool must be registered to ToolSvc before assigning it
327 if derives_from(value, "ConfigurableAlgTool"):
328 if not value.isInToolSvc():
329 suggestion = (
330 "You may need to add jobOptions lines something like:"
331 + os.linesep
332 + "from AthenaCommon.AppMgr import ToolSvc"
333 + os.linesep
334 + "ToolSvc += "
335 )
336 if value.getName() == value.getType(): # using default name
337 suggestion += f"{value.__class__.__name__}()"
338 else: # using user-defined name
339 suggestion += (
340 f"{value.__class__.__name__}({value.getName()!r})"
341 )
342 raise RuntimeError(
343 self.fullPropertyName(obj)
344 + f": Public tool {value.getJobOptName()} is not yet in ToolSvc. {suggestion}"
345 )
346 # make it read-only
347 return self._handleType(value.toStringProperty())
348 elif value.hasParent(obj.getJobOptName()):
349 # is already a child, keep as-is
350 return value
351 else:
352 # make a copy of the configurable
353 value = obj.copyChildAndSetParent(value, obj.getJobOptName())
354 # ensure that the new object (as well as all its children)
355 # are in allConfigurables
356 _registerConfigurables(obj.allConfigurables, value)
357 return value
358 else:
359 raise TypeError(
360 f"Property {self.fullPropertyName(obj)} value {value!r} is not a {self._confTypeName} nor a {self._handleType.__name__} nor a string"
361 )
362
363 return value
364
365

◆ getDefaultConfigurable()

GaudiKernel.PropertyProxy.GaudiHandlePropertyProxyBase.getDefaultConfigurable ( self,
typeAndName,
requester )
Return the configurable instance corresponding to the toolhandle if possible.
Otherwise return None

Definition at line 246 of file PropertyProxy.py.

246 def getDefaultConfigurable(self, typeAndName, requester):
247 """Return the configurable instance corresponding to the toolhandle if possible.
248 Otherwise return None"""
249 global log
250 # find the module
251 typeAndNameTuple = typeAndName.split("/")
252 confType = typeAndNameTuple[0]
253 confClass = ConfigurableDb.getConfigurable(confType)
254 # check the type of the configurable
255 if not derives_from(confClass, self._confTypeName):
256 log.error(
257 "%s: Configurable %s is not a %s",
258 requester,
259 confType,
260 self._confTypeName,
261 )
262 return None
263 try:
264 confName = typeAndNameTuple[1]
265 except IndexError:
266 return confClass() # use default name
267 else:
268 return confClass(confName)
269

◆ isConfig()

GaudiKernel.PropertyProxy.GaudiHandlePropertyProxyBase.isConfig ( self,
value )
Check if <value> is a configurable of the correct type

Definition at line 242 of file PropertyProxy.py.

242 def isConfig(self, value):
243 """Check if <value> is a configurable of the correct type"""
244 return derives_from(value, self._confTypeName)
245

◆ isHandle()

GaudiKernel.PropertyProxy.GaudiHandlePropertyProxyBase.isHandle ( self,
value )
Check if <value> is a handle of the correct type

Definition at line 238 of file PropertyProxy.py.

238 def isHandle(self, value):
239 """Check if <value> is a handle of the correct type"""
240 return isinstance(value, self._handleType)
241

Member Data Documentation

◆ _confTypeName

GaudiKernel.PropertyProxy.GaudiHandlePropertyProxyBase._confTypeName = "Configurable" + handleType.componentType
protected

Definition at line 209 of file PropertyProxy.py.

◆ _handleType

GaudiKernel.PropertyProxy.GaudiHandlePropertyProxyBase._handleType = handleType
protected

Definition at line 208 of file PropertyProxy.py.


The documentation for this class was generated from the following file: