Skip to content
Snippets Groups Projects
Forked from atlas / athena
17369 commits behind the upstream repository.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
__init__.py 4.44 KiB
# Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration

# @file PyUtils/python/AthFile/__init__.py
# @purpose a simple abstraction of a file to retrieve informations out of it
# @author Sebastien Binet <binet@cern.ch>
# @date October 2008

__doc__ = "a simple abstraction of a file to retrieve informations out of it"
__author__  = "Sebastien Binet <binet@cern.ch>"

__all__        = []
__pseudo_all__ = [
    'AthFile',
    'ftype',
    'fopen',
    'exists',
    'server',
    ]

import functools
import PyUtils.Decorators as _decos
from . import impl as _impl
AthFile = _impl.AthFile

def _update_cache(fct):
    @functools.wraps(fct)
    def wrapper(*args):
        res = fct(*args)
        import PyUtils.AthFile as af
        if af.server._do_pers_cache:
            try:
                af.server.load_cache()
            except Exception:
                pass
        return res
    return wrapper

### classes -------------------------------------------------------------------
import types
class ModuleFacade(types.ModuleType):
    """a helper class to manage the instantiation of the ``AthFileMgr`` and
    ``AthFileServer`` objects and allow attribute-like access to methods
    (stolen from PyRoot)
    """
    def __init__( self, module ):
        types.ModuleType.__init__(self, module.__name__)
        self.__dict__['module'] = module
        self.__dict__[ '__doc__'  ] = module.__doc__
        self.__dict__[ '__name__' ] = module.__name__
        self.__dict__[ '__file__' ] = module.__file__

        self.__dict__['_impl']  = _impl

        self.__dict__['server'] = _impl.g_server
        
        import atexit
        atexit.register(self.shutdown)
        del atexit
        
    def __getattr__(self, k):
        if k in self.__dict__:
            return self.__dict__.get(k)
        if k.startswith('__'):
            return types.ModuleType.__getattribute__(self, k)
        return object.__getattribute__(self, k)
    
    def restart_server(self):
        return
    
    def shutdown(self):
        #self.server._cleanup_pyroot()
        return
    
    @property
    def msg(self):
        return self.server.msg()
    
    @property
    def cache(self):
        return self.server.cache()

    @property
    def save_cache(self):
        return self.server.save_cache

    @property
    def load_cache(self):
        return self.server.load_cache

    @property
    def flush_cache(self):
        return self.server.flush_cache
    
    @_decos.forking
    def ftype(self, fname):
        return self.server.ftype(fname)

    @_decos.forking
    def fname(self, fname):
        return self.server.fname(fname)

    @_decos.forking
    def exists(self, fname):
        return self.server.exists(fname)

    @_update_cache  # also decorate with _update_cache to pick-up the changes 
    @_decos.forking # from the forked athfile server...
    def fopen(self, fnames, evtmax=1):
        """
        helper function to create @c AthFile instances
        @param `fnames` name of the file (or a list of names of files) to inspect
        @param `nentries` number of entries to process (for each file)
        
        Note that if `fnames` is a list of filenames, then `fopen` returns a list
        of @c AthFile instances.
        """
        if isinstance(fnames, (list, tuple)):
            infos = []
            for fname in fnames:
                info = self.server.fopen(fname, evtmax)
                infos.append(info)
                pass
            return infos
        return self.server.fopen(fnames, evtmax)

    @_update_cache  # also decorate with _update_cache to pick-up the changes 
    @_decos.forking # from the forked athfile server...
    def pfopen(self, fnames, evtmax=1):
        """
        helper function to create @c AthFile instances
        @param `fnames` name of the file (or a list of names of files) to inspect
        @param `nentries` number of entries to process (for each file)
        
        Note that if `fnames` is a list of filenames, then `fopen` returns a list
        of @c AthFile instances.

        This is a parallel (multi-threaded) version of ``fopen``.
        """
        return self.server.pfopen(fnames, evtmax)

    ## def __del__(self):
    ##     self._mgr.shutdown()
    ##     return super(ModuleFacade, self).__del__()
    
    pass # class ModuleFacade

### exec at import ------------------------------------------------------------
import sys
sys.modules[ __name__ ] = ModuleFacade( sys.modules[ __name__ ] )
del ModuleFacade