The Gaudi Framework  master (9f3f8895)
Loading...
Searching...
No Matches
__init__.py
Go to the documentation of this file.
11import ctypes
12import os
13import sys
14
15from ._version import __version__, git_describe_tag, version_info # noqa: F401
16
17__configurables_module_fullname__ = __name__ + ".Configurables"
18__ignore_missing_configurables__ = False
19
20# Small class that allows to access all the configurables as attributes of the
21# instance.
22# Used as module to allow code like
23# @code
24# from Gaudi.Configuration import Configurables
25# Configurables.MyConf()
26# @endcode
27
28
30 # Initializes the instance
31 def __init__(self):
32 # If set to true, does not raise an AttributeError if the configurable is not found.
34 self.__name__ = __configurables_module_fullname__
35 self.__loader__ = None
36
37 def __getattr__(self, name):
38 # trigger the load of the configurables database
39 from Gaudi.CommonGaudiConfigurables import aliases
40 from Gaudi.Configuration import cfgDb, confDbGetConfigurable
41
42 # return value
43 retval = None
44 # handle the special cases (needed for modules): __all__, __path__
45 if name == "__all__":
46 retval = cfgDb.keys()
47 elif name == "__spec__":
48 import importlib
49
50 retval = importlib.machinery.ModuleSpec(
51 name=__configurables_module_fullname__,
52 loader=self.__loader__,
53 )
54 elif name == "__package__":
55 retval = self.__name__
56 elif name == "__path__":
57 raise AttributeError("'module' object has no attribute '__path__'")
58 elif name in cfgDb.keys(): # ignore private names
59 retval = confDbGetConfigurable(name)
60 elif name in aliases: # special case of aliases
61 retval = aliases[name]
63 import logging
64
65 logging.getLogger(__configurables_module_fullname__).warning(
66 "Configurable class %s not in database", name
67 )
68 else:
69 # We raise an AttributeError exception if the configurable could not be found
70 # to respect the Python semantic.
71 raise AttributeError(
72 f"module '{__configurables_module_fullname__}' does not have attribute '{name}'"
73 )
74 return retval
75
76
77# install the facade module instance as a module
78Configurables = _ConfigurablesModule()
79sys.modules[__configurables_module_fullname__] = Configurables
80
81_GaudiKernelLib = None
82
83
84class c_opt_t(ctypes.Structure):
85 _fields_ = [("key", ctypes.c_char_p), ("value", ctypes.c_char_p)]
86
87
89 def __init__(self, opts, appType="Gaudi::Application"):
90 global _GaudiKernelLib
91 if _GaudiKernelLib is None:
92 # Note: using CDLL instead of PyDLL means that every call to the Python C
93 # API must be protected acquiring the GIL
94 #
95 if sys.platform == "darwin":
96 # DYLD_LIBRARY_PATH cannot be used for dlopen on macos
97 _libpaths = os.environ.get("DYLD_LIBRARY_PATH")
98 if not _libpaths:
99 print("WARNING: DYLD_LIBRARY_PATH is empty!")
100 for _path in _libpaths.split(":"):
101 _lib = os.path.join(_path, "libGaudiKernel.dylib")
102 if os.path.isfile(_lib):
103 gkl = _GaudiKernelLib = ctypes.CDLL(
104 _lib,
105 mode=ctypes.RTLD_GLOBAL,
106 )
107 else:
108 gkl = _GaudiKernelLib = ctypes.CDLL(
109 "libGaudiKernel.so",
110 mode=ctypes.RTLD_GLOBAL,
111 )
112 gkl._py_Gaudi__Application__create.restype = ctypes.c_void_p
113 gkl._py_Gaudi__Application__run.argtypes = [ctypes.c_void_p]
114 gkl._py_Gaudi__Application__run.restype = ctypes.c_int
115 gkl._py_Gaudi__Application__delete.argtypes = [ctypes.c_void_p]
116
117 c_opts = (c_opt_t * len(opts))()
118 for idx, item in enumerate(opts.items()):
119 c_opts[idx].key = item[0].encode("ascii")
120 c_opts[idx].value = item[1].encode("ascii")
121
122 self._impl = _GaudiKernelLib._py_Gaudi__Application__create(
123 appType.encode("ascii"), c_opts, ctypes.c_ulong(len(c_opts))
124 )
125
126 @classmethod
127 def create(cls, appType, opts):
128 return cls(opts, appType=appType)
129
130 def run(self):
131 return _GaudiKernelLib._py_Gaudi__Application__run(self._impl)
132
133 def __del__(self):
134 _GaudiKernelLib._py_Gaudi__Application__delete(self._impl)
Gaudi application entry point.
Definition __init__.py:88
__init__(self, opts, appType="Gaudi::Application")
Definition __init__.py:89
create(cls, appType, opts)
Definition __init__.py:127
virtual int run()
Implement the application main logic: