The Gaudi Framework  master (181af51f)
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 "module '%s' does not have attribute '%s'"
73 % (__configurables_module_fullname__, name)
74 )
75 return retval
76
77
78# install the facade module instance as a module
79Configurables = _ConfigurablesModule()
80sys.modules[__configurables_module_fullname__] = Configurables
81
82_GaudiKernelLib = None
83
84
85class c_opt_t(ctypes.Structure):
86 _fields_ = [("key", ctypes.c_char_p), ("value", ctypes.c_char_p)]
87
88
89class Application(object):
90 def __init__(self, opts, appType="Gaudi::Application"):
91 global _GaudiKernelLib
92 if _GaudiKernelLib is None:
93 # Note: using CDLL instead of PyDLL means that every call to the Python C
94 # API must be protected acquiring the GIL
95 #
96 if sys.platform == "darwin":
97 # LD_LIBRARY_PATH cannot be used for dlopen on macos;
98 # use custom variable GAUDI_PLUGIN_PATH instead
99 _libpaths = os.environ.get("GAUDI_PLUGIN_PATH")
100 if not _libpaths:
101 print("WARNING: GAUDI_PLUGIN_PATH is empty!")
102 for _path in _libpaths.split(":"):
103 _lib = os.path.join(_path, "libGaudiKernel.dylib")
104 if os.path.isfile(_lib):
105 gkl = _GaudiKernelLib = ctypes.CDLL(
106 _lib,
107 mode=ctypes.RTLD_GLOBAL,
108 )
109 else:
110 gkl = _GaudiKernelLib = ctypes.CDLL(
111 "libGaudiKernel.so",
112 mode=ctypes.RTLD_GLOBAL,
113 )
114 gkl._py_Gaudi__Application__create.restype = ctypes.c_void_p
115 gkl._py_Gaudi__Application__run.argtypes = [ctypes.c_void_p]
116 gkl._py_Gaudi__Application__run.restype = ctypes.c_int
117 gkl._py_Gaudi__Application__delete.argtypes = [ctypes.c_void_p]
118
119 c_opts = (c_opt_t * len(opts))()
120 for idx, item in enumerate(opts.items()):
121 c_opts[idx].key = item[0].encode("ascii")
122 c_opts[idx].value = item[1].encode("ascii")
123
124 self._impl = _GaudiKernelLib._py_Gaudi__Application__create(
125 appType.encode("ascii"), c_opts, ctypes.c_ulong(len(c_opts))
126 )
127
128 @classmethod
129 def create(cls, appType, opts):
130 return cls(opts, appType=appType)
131
132 def run(self):
133 return _GaudiKernelLib._py_Gaudi__Application__run(self._impl)
134
135 def __del__(self):
136 _GaudiKernelLib._py_Gaudi__Application__delete(self._impl)
Gaudi application entry point.
Definition Application.h:27
virtual int run()
Implement the application main logic:
create(cls, appType, opts)
Definition __init__.py:129
__init__(self, opts, appType="Gaudi::Application")
Definition __init__.py:90