The Gaudi Framework  master (37c0b60a)
__init__.py
Go to the documentation of this file.
1 
11 import ctypes
12 import os
13 import sys
14 
15 __configurables_module_fullname__ = __name__ + ".Configurables"
16 __ignore_missing_configurables__ = False
17 
18 # Small class that allows to access all the configurables as attributes of the
19 # instance.
20 # Used as module to allow code like
21 # @code
22 # from Gaudi.Configuration import Configurables
23 # Configurables.MyConf()
24 # @endcode
25 
26 
27 class _ConfigurablesModule(object):
28  # Initializes the instance
29  def __init__(self):
30  # If set to true, does not raise an AttributeError if the configurable is not found.
32  self.__name__ = __configurables_module_fullname__
33  self.__loader__ = None
34 
35  def __getattr__(self, name):
36  # trigger the load of the configurables database
37  from Gaudi.CommonGaudiConfigurables import aliases
38  from Gaudi.Configuration import cfgDb, confDbGetConfigurable
39 
40  # return value
41  retval = None
42  # handle the special cases (needed for modules): __all__, __path__
43  if name == "__all__":
44  retval = cfgDb.keys()
45  elif name == "__spec__":
46  import importlib
47 
48  retval = importlib.machinery.ModuleSpec(
49  name=__configurables_module_fullname__,
50  loader=self.__loader__,
51  )
52  elif name == "__package__":
53  retval = self.__name__
54  elif name == "__path__":
55  raise AttributeError("'module' object has no attribute '__path__'")
56  elif name in cfgDb.keys(): # ignore private names
57  retval = confDbGetConfigurable(name)
58  elif name in aliases: # special case of aliases
59  retval = aliases[name]
61  import logging
62 
63  logging.getLogger(__configurables_module_fullname__).warning(
64  "Configurable class %s not in database", name
65  )
66  else:
67  # We raise an AttributeError exception if the configurable could not be found
68  # to respect the Python semantic.
69  raise AttributeError(
70  "module '%s' does not have attribute '%s'"
71  % (__configurables_module_fullname__, name)
72  )
73  return retval
74 
75 
76 # install the facade module instance as a module
77 Configurables = _ConfigurablesModule()
78 sys.modules[__configurables_module_fullname__] = Configurables
79 
80 _GaudiKernelLib = None
81 
82 
83 class c_opt_t(ctypes.Structure):
84  _fields_ = [("key", ctypes.c_char_p), ("value", ctypes.c_char_p)]
85 
86 
87 class Application(object):
88  def __init__(self, opts, appType="Gaudi::Application"):
89  global _GaudiKernelLib
90  if _GaudiKernelLib is None:
91  # Note: using CDLL instead of PyDLL means that every call to the Python C
92  # API must be protected acquiring the GIL
93  #
94  if sys.platform == "darwin":
95  # LD_LIBRARY_PATH cannot be used for dlopen on macos;
96  # use custom variable GAUDI_PLUGIN_PATH instead
97  _libpaths = os.environ.get("GAUDI_PLUGIN_PATH")
98  if not _libpaths:
99  print("WARNING: GAUDI_PLUGIN_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._ConfigurablesModule.ignoreMissingConfigurables
ignoreMissingConfigurables
Definition: __init__.py:31
Gaudi.CommonGaudiConfigurables
Definition: CommonGaudiConfigurables.py:1
Gaudi.Application.create
def create(cls, appType, opts)
Definition: __init__.py:127
Gaudi.Application
Gaudi application entry point.
Definition: __init__.py:87
Gaudi._ConfigurablesModule.__init__
def __init__(self)
Definition: __init__.py:29
Gaudi._ConfigurablesModule.__getattr__
def __getattr__(self, name)
Definition: __init__.py:35
Gaudi._ConfigurablesModule.__name__
__name__
Definition: __init__.py:32
Gaudi._ConfigurablesModule
Definition: __init__.py:27
Gaudi.Configuration
Definition: Configuration.py:1
Gaudi.Application::run
virtual int run()
Implement the application main logic:
Definition: Application.cpp:75
Gaudi._ConfigurablesModule.__loader__
__loader__
Definition: __init__.py:33
Gaudi.Application._impl
_impl
Definition: __init__.py:122
Gaudi.Application.__init__
def __init__(self, opts, appType="Gaudi::Application")
Definition: __init__.py:88
Gaudi.c_opt_t
Definition: __init__.py:83
Gaudi.CommonGaudiConfigurables.cls
cls
Definition: CommonGaudiConfigurables.py:43
Gaudi.Application.__del__
def __del__(self)
Definition: __init__.py:133