23 pathvar =
"DYLD_LIBRARY_PATH" if sys.platform ==
"darwin" else "LD_LIBRARY_PATH"
24 for path
in os.getenv(pathvar,
"").split(os.pathsep):
25 if not os.path.isdir(path):
29 for f
in os.listdir(path)
30 if f.endswith(
".confdb2")
and os.path.isfile(os.path.join(path, f))
33 for db
in [shelve.open(f,
"r")
for f
in dbfiles]:
35 if key
not in self.
_dbs:
39 return self.
_dbs[key][key]
42 return key
in self.
_dbs
45 return iter(self.
_dbs)
49 if "GAUDICONFIG2_DB" in os.environ:
51 "from {} import {} as _DB".
format(*os.environ[
"GAUDICONFIG2_DB"].rsplit(
".", 1))
64 atexit.register(_fix_clean_up)
66 if sys.version_info >= (3,):
67 maketrans = str.maketrans
69 from string
import maketrans
70 _TRANS_TABLE =
maketrans(
"<>&*,: ().",
"__rp__s___")
75 Translate a C++ type name (with templates etc.) to Python identifier.
77 return name.replace(
", ",
",").translate(_TRANS_TABLE)
82 Split a C++ qualified namespace in the tuple (top_namespace, rest) starting
83 searching the separator from pos.
85 >>> split_namespace('std::chrono::time_point')
86 ('std', 'chrono::time_point')
89 tpos = typename.find(
"<")
90 pos = typename.find(
"::")
93 if pos > 0
and (tpos < 0
or pos < tpos):
95 tail = typename[pos + 2 :]
104 Helper to expose Configurables classes (from Configurables database) as
105 a tree of subpackages, each mapped to a namespace.
110 @param modulename: name of the module
111 @param root: name of the root modules (that pointing to the root C++
112 namespace), None is ewuivalent to pass modulename as root
114 self.
_log = logging.getLogger(modulename)
115 self.
_log.debug(
"initializing module %r (with root %r)", modulename, root)
120 assert (
not root)
or modulename.startswith(
122 ),
"modulename should be (indirect submodule of root)"
124 self.
_namespace = modulename[len(root) + 1 :].replace(
".",
"::")
if root
else ""
125 self.
_log.debug(
"%r mapping namespace %r", modulename, self.
_namespace or "::")
131 if alt_name != cname:
134 self.
_alt_names[cname.replace(
" ",
"")] = cname
139 sys.modules[modulename] = self
141 for submodule
in self._namespaces:
150 Extract from the Configurables DB the namespaces and classes names in
151 the namespace this instance represents.
153 self.
_log.debug(
"getting list of entries under %r", self.
_namespace)
155 prefix_len = len(prefix)
160 if name.startswith(prefix):
168 "found %d namespaces and %d classes", len(namespaces), len(classes)
170 return (namespaces, classes)
174 Helper to instantiate on demand Configurable classes.
178 from ._configurables
import makeConfigurableClass
180 self.
_log.debug(
"generating %r (%s)", name, fullname)
185 __cpp_type__=fullname,
188 elif name.replace(
" ",
"")
in self.
_alt_names:
189 entry = getattr(self, self.
_alt_names[name.replace(
" ",
"")])
190 elif name ==
"__spec__":
193 entry = importlib.machinery.ModuleSpec(
194 name=self.__package__,
197 elif name ==
"__package__":
200 raise AttributeError(
203 setattr(self, name, entry)
208 Return a configurable from the fully qualified type name (relative to
209 the current namespace).any
213 return getattr(self, head).
getByType(tail)
215 return getattr(self, tail)