diff --git a/k4FWCore/python/k4FWCore/utils.py b/k4FWCore/python/k4FWCore/utils.py index 0d42f6d6..6b2c672e 100644 --- a/k4FWCore/python/k4FWCore/utils.py +++ b/k4FWCore/python/k4FWCore/utils.py @@ -19,53 +19,7 @@ # import os from io import TextIOWrapper -from typing import Union, Optional, Dict, Any -import importlib.util -import importlib.abc -from importlib.machinery import SourceFileLoader - - -def import_from( - filename: Union[str, os.PathLike], - module_name: Optional[str] = None, - global_vars: Optional[Dict[str, Any]] = None, -) -> Any: - """Dynamically imports a module from the specified file path. - - This function imports a module from a given filename, with the option to - specify the module's name and inject global variables into the module before - it is returned. If `module_name` is not provided, the filename is used as - the module name after replacing '.' with '_'. Global variables can be passed - as a dictionary to `global_vars`, which will be injected into the module's - namespace. - - Args: - filename (str): The path to the file from which to import the module. - module_name (Optional[str]): The name to assign to the module. Defaults - to None, in which case the filename is used as the module name. - global_vars (Optional[Dict[str, Any]]): A dictionary of global variables - to inject into the module's namespace. Defaults to None. - - Returns: - Any: The imported module with the specified modifications. - - Raises: - FileNotFoundError: If the specified file does not exist. - ImportError: If there is an error during the import process. - - """ - filename = os.path.abspath(filename) - if not os.path.exists(filename): - raise FileNotFoundError(f"No such file: '{filename}'") - - module_name = module_name or os.path.basename(filename).replace(".", "_") - loader = SourceFileLoader(module_name, filename) - spec = importlib.util.spec_from_loader(loader.name, loader) - module = importlib.util.module_from_spec(spec) - if global_vars: - module.__dict__.update(global_vars) - loader.exec_module(module) - return module +from typing import Union def load_file(opt_file: Union[TextIOWrapper, str, os.PathLike]) -> None: @@ -95,65 +49,3 @@ def load_file(opt_file: Union[TextIOWrapper, str, os.PathLike]) -> None: code = compile(opt_file.read(), opt_file.name, "exec") exec(code, globals()) - - -class SequenceLoader: - """A class for loading algorithm sequences onto a list of algorithms - - It dynamically loads algorithms from Python files based on the given - sequence names. In the import process it will look for a Sequence of - algorithms which might have configuration constants that depend on some - global calibration configuration. These constants are provided during the - import of a sequence, such that the imported python files do not need to - define all of them. - """ - - def __init__(self, alg_list: list, global_vars: Optional[Dict[str, Any]] = None) -> None: - """Initialize the SequenceLoader - - This initializes a SequenceLoader with the list of algorithms to which - dynamically loaded algorithms should be appended to. It optionally takes - some global calibration constants that should be injected during import - of the sequence files - - Args: - alg_list (List): A list to store loaded sequence algorithms. - global_vars (Optional[Dict[str, Any]]): A dictionary of global - variables for the sequences. Defaults to None. The keys in this - dictionary will be the available variables in the imported - module and the values will be the values of these variables. - """ - self.alg_list = alg_list - self.global_vars = global_vars - - def load(self, sequence: str) -> None: - """Loads a sequence algorithm from a specified Python file and appends - it to the algorithm list - - The method constructs the filename from the sequence parameter name and - imports the sequence from the imported module. - - Args: - sequence (str): The name of the sequence to load. The sequence name - should correspond to a Python file and class name following the - pattern `{sequence}.py` and `{sequence}Sequence`, respectively. - - Examples: - >>> alg_list = [] - >>> seq_loader = SequenceLoader(alg_list) - >>> seq_loader.load("Tracking/TrackingDigi") - - This will import the file `Tracking/TrackingDigi.py` and add the - sequence of algorithms that is defined in `TrackingDigiSequence` in - that file to the alg_list - """ - filename = f"{sequence}.py" - seq_name = f"{sequence.split('/')[-1]}Sequence" - - seq_module = import_from( - filename, - global_vars=self.global_vars, - ) - - seq = getattr(seq_module, seq_name) - self.alg_list.extend(seq)