24 if sys.platform ==
"darwin"
25 else [
"GAUDI_PLUGIN_PATH",
"LD_LIBRARY_PATH"]
27 for pathvar
in pathvars:
28 for path
in os.getenv(pathvar,
"").split(os.pathsep):
29 if not os.path.isdir(path):
33 for f
in os.listdir(path)
34 if f.endswith(
".confdb2")
and os.path.isfile(os.path.join(path, f))
37 for db
in [shelve.open(f,
"r")
for f
in dbfiles]:
39 if key
not in self.
_dbs:
43 return self.
_dbs[key][key]
46 return key
in self.
_dbs
49 return iter(self.
_dbs)
53 if "GAUDICONFIG2_DB" in os.environ:
55 "from {} import {} as _DB".
format(*os.environ[
"GAUDICONFIG2_DB"].rsplit(
".", 1))
61 _TRANS_TABLE = str.maketrans(
"<>&*,: ().",
"__rp__s___")
66 Translate a C++ type name (with templates etc.) to Python identifier.
68 return name.replace(
", ",
",").translate(_TRANS_TABLE)
73 Split a C++ qualified namespace in the tuple (top_namespace, rest) starting
74 searching the separator from pos.
76 >>> split_namespace('std::chrono::time_point')
77 ('std', 'chrono::time_point')
80 tpos = typename.find(
"<")
81 pos = typename.find(
"::")
84 if pos > 0
and (tpos < 0
or pos < tpos):
86 tail = typename[pos + 2 :]
95 Helper to expose Configurables classes (from Configurables database) as
96 a tree of subpackages, each mapped to a namespace.
101 @param modulename: name of the module
102 @param root: name of the root modules (that pointing to the root C++
103 namespace), None is ewuivalent to pass modulename as root
105 self.
_log = logging.getLogger(modulename)
106 self.
_log.debug(
"initializing module %r (with root %r)", modulename, root)
111 assert (
not root)
or modulename.startswith(
113 ),
"modulename should be (indirect submodule of root)"
115 self.
_namespace = modulename[len(root) + 1 :].replace(
".",
"::")
if root
else ""
116 self.
_log.debug(
"%r mapping namespace %r", modulename, self.
_namespace or "::")
122 if alt_name != cname:
125 self.
_alt_names[cname.replace(
" ",
"")] = cname
130 sys.modules[modulename] = self
132 for submodule
in self._namespaces:
141 Extract from the Configurables DB the namespaces and classes names in
142 the namespace this instance represents.
144 self.
_log.debug(
"getting list of entries under %r", self.
_namespace)
146 prefix_len = len(prefix)
151 if name.startswith(prefix):
159 "found %d namespaces and %d classes", len(namespaces), len(classes)
161 return (namespaces, classes)
165 Helper to instantiate on demand Configurable classes.
169 from ._configurables
import makeConfigurableClass
171 self.
_log.debug(
"generating %r (%s)", name, fullname)
176 __cpp_type__=fullname,
179 elif name.replace(
" ",
"")
in self.
_alt_names:
180 entry = getattr(self, self.
_alt_names[name.replace(
" ",
"")])
181 elif name ==
"__spec__":
184 entry = importlib.machinery.ModuleSpec(
185 name=self.__package__,
188 elif name ==
"__package__":
191 raise AttributeError(
194 setattr(self, name, entry)
199 Return a configurable from the fully qualified type name (relative to
200 the current namespace).any
204 return getattr(self, head).
getByType(tail)
206 return getattr(self, tail)