The Gaudi Framework  master (9da49fa3)
Loading...
Searching...
No Matches
ConfigurableMeta.py
Go to the documentation of this file.
13
14import GaudiKernel.PropertyProxy as PropertyProxy
15
16# data
17__version__ = "1.0.1"
18__author__ = "Wim Lavrijsen (WLavrijsen@lbl.gov)"
19
20__all__ = ["ConfigurableMeta"]
21
22# this metaclass installs PropertyProxy descriptors for Gaudi properties
23
24
25class ConfigurableMeta(type):
26 """The setting of Gaudi component properties needs to be deferred and
27 history of who set what where needs to be collected. This is done
28 by using PropertyProxy descriptors rather than the default ones."""
29
30 def __new__(self, name, bases, dct):
31 # enfore use of classmethod for getType() and setDefaults()
32 if "getType" in dct and not isinstance(dct["getType"], classmethod):
33 dct["getType"] = classmethod(dct["getType"])
34
35 if "setDefaults" in dct and not isinstance(dct["setDefaults"], classmethod):
36 dct["setDefaults"] = classmethod(dct["setDefaults"])
37
38 # collect what are properties (basically, any public name; C++ variables
39 # shouldn't start with an '_' because of portability constraints, hence
40 # it is safe to assume that any such vars are python private ones)
41 newclass = type.__new__(self, name, bases, dct)
42
43 # cache references of instances by name for duplicate removal
44 newclass.configurables = {}
45
46 # loop over slots, which are all assumed to be properties, create proxies, defaults
47 properties = {}
48 slots = dct.get("__slots__")
49 if slots:
50 props = [x for x in slots if x[0] != "_"]
51 propDict = dct.get("_propertyDocDct")
52 for prop in props:
53 docString = propDict and propDict.get(prop)
54 if isinstance(slots, dict):
55 default = slots[prop]
56 else:
57 default = None
58 proxy = PropertyProxy.PropertyProxyFactory(
59 getattr(newclass, prop), docString, default
60 )
61
62 properties[prop] = proxy
63 setattr(newclass, prop, proxy)
64
65 # complete set of properties includes those from base classes
66 for base in bases:
67 try:
68 bprops = base._properties.copy()
69 bprops.update(properties)
70 properties = bprops
71 except AttributeError:
72 pass
73
74 newclass._properties = properties
75
76 return newclass
77
78 def __call__(cls, *args, **kwargs):
79 """To Gaudi, any object with the same type/name is the same object. Hence,
80 this is mimicked in the configuration: instantiating a new Configurable
81 of a type with the same name will return the same instance."""
82
83 # Get the instance: `singleton' logic needs to be in __new__, not here,
84 # for compatibililty with pickling.)
85 cfg = cls.__new__(cls, *args, **kwargs)
86
87 # Initialize the object, if not done already.
88 if not hasattr(cfg, "_initok") or not cfg._initok:
89 cls.__init__(cfg, *args, **kwargs)
90
91 return cfg