7 __doc__ =
"""Module containing a set of Python base classes for PyAthena"""
8 __author__ =
"Sebastien Binet <binet@cern.ch>"
12 __pseudo_all__ = [
'StatusCode',
24 """ return the OS-native name from an OS-indenpendent one """
27 if plat.count(
'linux')>0:
28 lib_prefix,lib_suffix =
'lib',
'.so'
30 lib_prefix,lib_suffix =
'',
'.dll'
31 elif plat ==
'darwin':
32 lib_prefix,lib_suffix =
'lib',
'.dylib'
34 raise RuntimeError (
"sorry platform [%s] is not (yet?) supported"%plat)
35 _sys_libname = libname
36 if not _sys_libname.startswith (lib_prefix):
37 _sys_libname =
''.
join([lib_prefix,_sys_libname])
38 if not _sys_libname.endswith (lib_suffix):
39 _sys_libname =
''.
join([_sys_libname, lib_suffix])
44 Helper method to load a library by its natural name, not the OS-native name.
45 But if the OS-native name is given, it is safely handled too. Note that there's
46 a problem on MacOSX, for which the OS-native name ends with .dylib, but for
47 which ATLAS builds .so libraries. Override the OS-native name (which should probably
48 be replaced by two attempts; one with the .dylib and the other with .so)
50 >>> load_library ('AthenaServices')
51 >>> load_library ('AthenaServicesDict')
54 from sys
import platform
55 if platform ==
'darwin':
56 _sys_libname = _sys_libname.replace(
'.dylib',
'.so')
58 return ctypes.cdll.LoadLibrary (_sys_libname)
62 Helper function to find the (full)path to a library given its natural name.
63 @return None on failure
66 >>> find_library('AthenaServices')
67 '/afs/cern.ch/.../AtlasCore/[release]/InstallArea/.../libAthenaServices.so
78 if os.name !=
'posix':
79 raise RuntimeError(
'sorry OS [%s] is not supported' % os.name)
81 if 'LD_LIBRARY_PATH' in os.environ:
82 for d
in os.environ[
'LD_LIBRARY_PATH'].
split(os.pathsep):
83 lib = os.path.join(d, _sys_libname)
84 if os.path.exists(lib):
90 Helper method to reload a python module by name.
91 This is useful in the usual following case:
92 >>> from Foo import MyAlg
93 >>> assert (not 'Foo' in dir())
94 >>> reload(Foo) # won't work
95 >>> PyAthena.reload_module ('Foo') # will work
96 >>> PyAthena.reload_module (Foo) # will work too
99 from importlib
import reload
100 if isinstance (modname, types.ModuleType):
101 modname = modname.__name__
102 if modname
in sys.modules:
103 return reload (sys.modules[modname])
104 raise ValueError(
'no module [%s] could be found'%modname)
107 """simple minded function to reload objects, methods and modules
110 >>> # edit and modify the execute methods of some PyAthena.Alg
111 ... # class, then load back that definition
112 >>> PyAthena.py_reload (alg1.execute, alg2.execute)
113 >>> PyAthena.py_reload (alg1.execute)
114 >>> alg1.execute() # will use the new definition
115 >>> theApp.nextEvent() # will also use the new definitions
116 ... # of both alg1 and alg2
119 from importlib
import reload
120 for i,arg
in enumerate(args):
121 if isinstance (arg, types.ModuleType):
124 elif isinstance (arg, types.StringType):
127 elif isinstance (arg, types.MethodType):
130 modname = arg.im_self.__class__.__module__
131 module = reload (sys.modules[modname])
133 klass = getattr (module, obj.__class__.__name__)
135 fct_name = arg.im_func.__name__
136 new_fct = getattr (klass, fct_name)
139 setattr (obj, fct_name,
140 new_fct.__get__(obj))
141 elif hasattr (arg,
'__class__'):
144 modname = arg.__class__.__module__
145 module = reload (sys.modules[modname])
147 klass = getattr (module, arg.__class__.__name__)
149 cfg_methods =
dir(Configurable)
150 d = (k
for k
in dir(klass)
151 if not k.startswith(
'__')
and k
not in cfg_methods)
153 if not hasattr (arg, k):
154 v = getattr (klass, k)
157 except AttributeError:
160 setattr (arg, k, v.__get__(arg))
168 print (
"*** unhandled type: [%s] (arg #%i) ***" % (
type(arg),i))
173 from AthenaPython
import PyAthenaComps
174 from AthenaPython.Bindings
import _PyAthenaBindingsCatalog
as _pycat
179 """a helper class to allow easy retrieval of automatically generated
180 configurables (stolen from PyRoot)
183 types.ModuleType.__init__(self, module.__name__)
184 self.__dict__[
'module' ] = module
185 self.__dict__[
'__doc__' ] = module.__doc__
186 self.__dict__[
'__name__' ] = module.__name__
187 self.__dict__[
'__file__' ] = module.__file__
189 from .Bindings
import py_svc
190 self.__dict__ [
'py_svc'] = py_svc
192 from .Bindings
import py_tool
193 self.__dict__ [
'py_tool'] = py_tool
195 from .Bindings
import py_alg
196 self.__dict__ [
'py_alg'] = py_alg
198 self.__dict__ [
'load_library'] = load_library
199 self.__dict__ [
'find_library'] = find_library
200 self.__dict__ [
'reload_module'] = reload_module
201 self.__dict__ [
'py_reload'] = py_reload
204 if k
in self.__dict__:
205 return self.__dict__.
get(k)
206 if k.startswith(
'__'):
207 return types.ModuleType.__getattribute__(self, k)
208 if k
in __pseudo_all__: v = getattr(PyAthenaComps, k)
209 else: v = _pycat.init(k)
210 object.__getattribute__(self,
'__dict__')[k] = v
214 sys.modules[ __name__ ] =
ModuleFacade( sys.modules[ __name__ ] )