7 from __future__
import print_function
9 __doc__ =
"""Module containing a set of Python base classes for PyAthena"""
10 __version__ =
"$Revision: 1.16 $"
11 __author__ =
"Sebastien Binet <binet@cern.ch>"
15 __pseudo_all__ = [
'StatusCode',
27 """ return the OS-native name from an OS-indenpendent one """
30 if plat.count(
'linux')>0:
31 lib_prefix,lib_suffix =
'lib',
'.so'
33 lib_prefix,lib_suffix =
'',
'.dll'
34 elif plat ==
'darwin':
35 lib_prefix,lib_suffix =
'lib',
'.dylib'
37 raise RuntimeError (
"sorry platform [%s] is not (yet?) supported"%plat)
38 _sys_libname = libname
39 if not _sys_libname.startswith (lib_prefix):
40 _sys_libname =
''.
join([lib_prefix,_sys_libname])
41 if not _sys_libname.endswith (lib_suffix):
42 _sys_libname =
''.
join([_sys_libname, lib_suffix])
47 Helper method to load a library by its natural name, not the OS-native name.
48 But if the OS-native name is given, it is safely handled too. Note that there's
49 a problem on MacOSX, for which the OS-native name ends with .dylib, but for
50 which ATLAS builds .so libraries. Override the OS-native name (which should probably
51 be replaced by two attempts; one with the .dylib and the other with .so)
53 >>> load_library ('AthenaServices')
54 >>> load_library ('AthenaServicesDict')
57 from sys
import platform
58 if platform ==
'darwin':
59 _sys_libname = _sys_libname.replace(
'.dylib',
'.so')
61 return ctypes.cdll.LoadLibrary (_sys_libname)
65 Helper function to find the (full)path to a library given its natural name.
66 @return None on failure
69 >>> find_library('AthenaServices')
70 '/afs/cern.ch/.../AtlasCore/[release]/InstallArea/.../libAthenaServices.so
81 if os.name !=
'posix':
82 raise RuntimeError(
'sorry OS [%s] is not supported' % os.name)
84 if 'LD_LIBRARY_PATH' in os.environ:
85 for d
in os.environ[
'LD_LIBRARY_PATH'].
split(os.pathsep):
86 lib = os.path.join(d, _sys_libname)
87 if os.path.exists(lib):
93 Helper method to reload a python module by name.
94 This is useful in the usual following case:
95 >>> from Foo import MyAlg
96 >>> assert (not 'Foo' in dir())
97 >>> reload(Foo) # won't work
98 >>> PyAthena.reload_module ('Foo') # will work
99 >>> PyAthena.reload_module (Foo) # will work too
102 from imp
import reload
103 if isinstance (modname, types.ModuleType):
104 modname = modname.__name__
105 if modname
in sys.modules:
106 return reload (sys.modules[modname])
107 raise ValueError(
'no module [%s] could be found'%modname)
110 """simple minded function to reload objects, methods and modules
113 >>> # edit and modify the execute methods of some PyAthena.Alg
114 ... # class, then load back that definition
115 >>> PyAthena.py_reload (alg1.execute, alg2.execute)
116 >>> PyAthena.py_reload (alg1.execute)
117 >>> alg1.execute() # will use the new definition
118 >>> theApp.nextEvent() # will also use the new definitions
119 ... # of both alg1 and alg2
122 from imp
import reload
123 for i,arg
in enumerate(args):
124 if isinstance (arg, types.ModuleType):
127 elif isinstance (arg, types.StringType):
130 elif isinstance (arg, types.MethodType):
133 modname = arg.im_self.__class__.__module__
134 module = reload (sys.modules[modname])
136 klass = getattr (module, obj.__class__.__name__)
138 fct_name = arg.im_func.__name__
139 new_fct = getattr (klass, fct_name)
142 setattr (obj, fct_name,
143 new_fct.__get__(obj))
144 elif hasattr (arg,
'__class__'):
147 modname = arg.__class__.__module__
148 module = reload (sys.modules[modname])
150 klass = getattr (module, arg.__class__.__name__)
152 cfg_methods =
dir(Configurable)
153 d = (k
for k
in dir(klass)
154 if not k.startswith(
'__')
and k
not in cfg_methods)
156 if not hasattr (arg, k):
157 v = getattr (klass, k)
160 except AttributeError:
163 setattr (arg, k, v.__get__(arg))
171 print (
"*** unhandled type: [%s] (arg #%i) ***" % (
type(arg),i))
176 from AthenaPython
import PyAthenaComps
177 from AthenaPython.Bindings
import _PyAthenaBindingsCatalog
as _pycat
182 """a helper class to allow easy retrieval of automatically generated
183 configurables (stolen from PyRoot)
186 types.ModuleType.__init__(self, module.__name__)
187 self.__dict__[
'module' ] = module
188 self.__dict__[
'__doc__' ] = module.__doc__
189 self.__dict__[
'__name__' ] = module.__name__
190 self.__dict__[
'__file__' ] = module.__file__
192 from .Bindings
import py_svc
193 self.__dict__ [
'py_svc'] = py_svc
195 from .Bindings
import py_tool
196 self.__dict__ [
'py_tool'] = py_tool
198 from .Bindings
import py_alg
199 self.__dict__ [
'py_alg'] = py_alg
201 self.__dict__ [
'load_library'] = load_library
202 self.__dict__ [
'find_library'] = find_library
203 self.__dict__ [
'reload_module'] = reload_module
204 self.__dict__ [
'py_reload'] = py_reload
207 if k
in self.__dict__:
208 return self.__dict__.
get(k)
209 if k.startswith(
'__'):
210 return types.ModuleType.__getattribute__(self, k)
211 if k
in __pseudo_all__: v = getattr(PyAthenaComps, k)
212 else: v = _pycat.init(k)
213 object.__getattribute__(self,
'__dict__')[k] = v
217 sys.modules[ __name__ ] =
ModuleFacade( sys.modules[ __name__ ] )