22 pathvar =
"DYLD_LIBRARY_PATH" if sys.platform ==
"darwin" else "LD_LIBRARY_PATH"
23 for path
in os.getenv(pathvar,
"").split(os.pathsep):
24 if not os.path.isdir(path):
28 for f
in os.listdir(path)
29 if f.endswith(
".confdb2")
and os.path.isfile(os.path.join(path, f))
32 for db
in [shelve.open(f,
"r")
for f
in dbfiles]:
34 if key
not in self.
_dbs:
38 return self.
_dbs[key][key]
41 return key
in self.
_dbs
44 return iter(self.
_dbs)
48 if "GAUDICONFIG2_DB" in os.environ:
50 "from {} import {} as _DB".
format(*os.environ[
"GAUDICONFIG2_DB"].rsplit(
".", 1))
56 _TRANS_TABLE = str.maketrans(
"<>&*,: ().",
"__rp__s___")
61 Translate a C++ type name (with templates etc.) to Python identifier.
63 return name.replace(
", ",
",").translate(_TRANS_TABLE)
68 Split a C++ qualified namespace in the tuple (top_namespace, rest) starting
69 searching the separator from pos.
71 >>> split_namespace('std::chrono::time_point')
72 ('std', 'chrono::time_point')
75 tpos = typename.find(
"<")
76 pos = typename.find(
"::")
79 if pos > 0
and (tpos < 0
or pos < tpos):
81 tail = typename[pos + 2 :]
90 Helper to expose Configurables classes (from Configurables database) as
91 a tree of subpackages, each mapped to a namespace.
96 @param modulename: name of the module
97 @param root: name of the root modules (that pointing to the root C++
98 namespace), None is ewuivalent to pass modulename as root
100 self.
_log = logging.getLogger(modulename)
101 self.
_log.debug(
"initializing module %r (with root %r)", modulename, root)
106 assert (
not root)
or modulename.startswith(
108 ),
"modulename should be (indirect submodule of root)"
110 self.
_namespace = modulename[len(root) + 1 :].replace(
".",
"::")
if root
else ""
111 self.
_log.debug(
"%r mapping namespace %r", modulename, self.
_namespace or "::")
117 if alt_name != cname:
120 self.
_alt_names[cname.replace(
" ",
"")] = cname
125 sys.modules[modulename] = self
127 for submodule
in self._namespaces:
136 Extract from the Configurables DB the namespaces and classes names in
137 the namespace this instance represents.
139 self.
_log.debug(
"getting list of entries under %r", self.
_namespace)
141 prefix_len = len(prefix)
146 if name.startswith(prefix):
154 "found %d namespaces and %d classes", len(namespaces), len(classes)
156 return (namespaces, classes)
160 Helper to instantiate on demand Configurable classes.
164 from ._configurables
import makeConfigurableClass
166 self.
_log.debug(
"generating %r (%s)", name, fullname)
171 __cpp_type__=fullname,
174 elif name.replace(
" ",
"")
in self.
_alt_names:
175 entry = getattr(self, self.
_alt_names[name.replace(
" ",
"")])
176 elif name ==
"__spec__":
179 entry = importlib.machinery.ModuleSpec(
180 name=self.__package__,
183 elif name ==
"__package__":
186 raise AttributeError(
189 setattr(self, name, entry)
194 Return a configurable from the fully qualified type name (relative to
195 the current namespace).any
199 return getattr(self, head).
getByType(tail)
201 return getattr(self, tail)