pyxora.utils.python
1import os 2import sys 3from os.path import basename, splitext 4from importlib.util import spec_from_file_location, module_from_spec 5from importlib import reload 6from types import ModuleType 7from sys import path as sys_path, modules 8from os import getcwd 9 10__all__ = ["get_filename","get_filetype","load_module","load_class"] 11 12def get_filename(file_path: str) -> str: 13 """ 14 Get the filename (without extension) from a full path. 15 16 Args: 17 file_path (str): Path to the file. 18 19 Returns: 20 str: Filename without extension. 21 """ 22 return splitext(basename(file_path))[0] 23 24def get_filetype(file_path: str) -> str: 25 """ 26 Get the filetype (without extension) from a full path. 27 28 Args: 29 file_path (str): Path to the file. 30 31 Returns: 32 str: Filename without extension. 33 """ 34 return splitext(basename(file_path))[1] 35 36 37def load_module(file_path: str) -> ModuleType: 38 """ 39 Dynamically load a Python module from a given file path. 40 41 Args: 42 file_path (str): Relative or absolute path to the `.py` file. 43 44 Returns: 45 module: The loaded module object. 46 47 Raises: 48 FileNotFoundError: If the file does not exist. 49 ImportError: If the module fails to load. 50 """ 51 full_path = os.path.normpath(os.path.join(os.getcwd(), file_path)) 52 53 if not os.path.exists(full_path): 54 raise FileNotFoundError(f"Module file not found: {full_path}") 55 56 main_dir = os.path.dirname(full_path) 57 if main_dir not in sys_path: 58 sys_path.append(main_dir) 59 60 module_name = get_filename(full_path) 61 spec = spec_from_file_location(module_name, full_path) 62 if spec is None or spec.loader is None: 63 raise ImportError(f"Couldn't create spec for module '{module_name}'.") 64 65 module = module_from_spec(spec) 66 modules[module_name] = module 67 spec.loader.exec_module(module) 68 69 return module 70 71def load_class(file_path: str,name:str) -> type: 72 """ 73 Load a class from a path 74 75 Args: 76 file_path (str): The class path. 77 name (str): The name of the class to retrieve. 78 79 Returns: 80 type: The loaded class object. 81 """ 82 module = load_module(file_path) 83 the_class = getattr(module, name, None) 84 return the_class
def
get_filename(file_path: str) -> str:
13def get_filename(file_path: str) -> str: 14 """ 15 Get the filename (without extension) from a full path. 16 17 Args: 18 file_path (str): Path to the file. 19 20 Returns: 21 str: Filename without extension. 22 """ 23 return splitext(basename(file_path))[0]
Get the filename (without extension) from a full path.
Arguments:
- file_path (str): Path to the file.
Returns:
str: Filename without extension.
def
get_filetype(file_path: str) -> str:
25def get_filetype(file_path: str) -> str: 26 """ 27 Get the filetype (without extension) from a full path. 28 29 Args: 30 file_path (str): Path to the file. 31 32 Returns: 33 str: Filename without extension. 34 """ 35 return splitext(basename(file_path))[1]
Get the filetype (without extension) from a full path.
Arguments:
- file_path (str): Path to the file.
Returns:
str: Filename without extension.
def
load_module(file_path: str) -> module:
38def load_module(file_path: str) -> ModuleType: 39 """ 40 Dynamically load a Python module from a given file path. 41 42 Args: 43 file_path (str): Relative or absolute path to the `.py` file. 44 45 Returns: 46 module: The loaded module object. 47 48 Raises: 49 FileNotFoundError: If the file does not exist. 50 ImportError: If the module fails to load. 51 """ 52 full_path = os.path.normpath(os.path.join(os.getcwd(), file_path)) 53 54 if not os.path.exists(full_path): 55 raise FileNotFoundError(f"Module file not found: {full_path}") 56 57 main_dir = os.path.dirname(full_path) 58 if main_dir not in sys_path: 59 sys_path.append(main_dir) 60 61 module_name = get_filename(full_path) 62 spec = spec_from_file_location(module_name, full_path) 63 if spec is None or spec.loader is None: 64 raise ImportError(f"Couldn't create spec for module '{module_name}'.") 65 66 module = module_from_spec(spec) 67 modules[module_name] = module 68 spec.loader.exec_module(module) 69 70 return module
Dynamically load a Python module from a given file path.
Arguments:
- file_path (str): Relative or absolute path to the
.py
file.
Returns:
module: The loaded module object.
Raises:
- FileNotFoundError: If the file does not exist.
- ImportError: If the module fails to load.
def
load_class(file_path: str, name: str) -> type:
72def load_class(file_path: str,name:str) -> type: 73 """ 74 Load a class from a path 75 76 Args: 77 file_path (str): The class path. 78 name (str): The name of the class to retrieve. 79 80 Returns: 81 type: The loaded class object. 82 """ 83 module = load_module(file_path) 84 the_class = getattr(module, name, None) 85 return the_class
Load a class from a path
Arguments:
- file_path (str): The class path.
- name (str): The name of the class to retrieve.
Returns:
type: The loaded class object.