hojichar.utils.load_compose
1from __future__ import annotations 2 3import importlib.util 4import logging 5import sys 6from collections.abc import Callable 7from os import PathLike 8from pathlib import Path 9from types import ModuleType 10from typing import Any 11 12import hojichar 13 14logger = logging.getLogger(__name__) 15 16 17def _load_module(path: str | PathLike[str]) -> ModuleType: 18 path_obj = Path(path) 19 module_name = path_obj.stem 20 spec = importlib.util.spec_from_file_location(module_name, path_obj) 21 if spec and spec.loader: 22 module = importlib.util.module_from_spec(spec) 23 sys.modules[module_name] = module 24 spec.loader.exec_module(module) 25 return module 26 27 28def load_filter_from_file( 29 profile_path: str | PathLike[str], 30) -> hojichar.Compose | hojichar.AsyncCompose: 31 """ 32 Loading a profile which has `FILTER` variable. 33 Given path is added to sys.path automaticaly. 34 35 Args: 36 profile_path (str): Path to a Python file that implements custom filter 37 hojichar.Compose must be defined as FILTER variable in the file. 38 39 Raises: 40 NotImplementedError: 41 42 Returns: 43 hojichar.Compose | hojichar.AsyncCompose: 44 """ 45 sys.path.append(str(Path(profile_path).parent)) 46 module = _load_module(profile_path) 47 if hasattr(module, "FILTER"): 48 filter = getattr(module, "FILTER") 49 if not isinstance(filter, (hojichar.Compose, hojichar.AsyncCompose)): 50 raise TypeError("FILTER must be Compose or AsyncCompose object.") 51 return filter 52 else: 53 raise NotImplementedError("FILTER is not defined in the profile.") 54 55 56def load_factory_from_file( 57 profile_path: str | PathLike[str], 58) -> Callable[[Any | None], hojichar.Compose | hojichar.AsyncCompose]: 59 """ 60 Loading a function by a profile which has `FACTORY` variable. 61 Given path is added to sys.path automaticaly. 62 63 Args: 64 profile_path (PathLike): Path to a Python file that implements custom filter 65 66 Raises: 67 NotImplementedError: 68 69 Returns: 70 Callable[[Any | None], hojichar.Compose | hojichar.AsyncCompose]: 71 An alias of the function which returns Compose or AsyncCompose. 72 """ 73 sys.path.append(str(Path(profile_path).parent)) 74 module = _load_module(profile_path) 75 if hasattr(module, "FACTORY"): 76 factory: Callable[[Any | None], hojichar.Compose | hojichar.AsyncCompose] = getattr( 77 module, "FACTORY" 78 ) 79 return factory 80 else: 81 raise NotImplementedError("FACTORY is not defined in the profile") 82 83 84def load_parametrized_filter_from_file( 85 profile_path: str | PathLike[str], *factory_args: str 86) -> hojichar.Compose | hojichar.AsyncCompose: 87 factory = load_factory_from_file(profile_path) 88 filter = factory(*factory_args) 89 return filter 90 91 92def load_compose( 93 profile_path: str | PathLike[str], *factory_args: str 94) -> hojichar.Compose | hojichar.AsyncCompose: 95 """ 96 Loading a Compose file from profile. Loading a Compose file from the profile. 97 Both `FILTER` and `FACTORY` pattern of the profile is loaded. 98 99 Args: 100 profile_path (PathLike): 101 102 Returns: 103 hojichar.Compose: 104 """ 105 try: 106 filter = load_filter_from_file(profile_path) 107 _check_args_num_mismatch(len(factory_args)) 108 return filter 109 except NotImplementedError: 110 return load_parametrized_filter_from_file(profile_path, *factory_args) 111 112 113def _check_args_num_mismatch(num_args: int) -> None: 114 if num_args > 0: 115 logger.warning(f"Warning: {num_args} arguments are ignored.")
29def load_filter_from_file( 30 profile_path: str | PathLike[str], 31) -> hojichar.Compose | hojichar.AsyncCompose: 32 """ 33 Loading a profile which has `FILTER` variable. 34 Given path is added to sys.path automaticaly. 35 36 Args: 37 profile_path (str): Path to a Python file that implements custom filter 38 hojichar.Compose must be defined as FILTER variable in the file. 39 40 Raises: 41 NotImplementedError: 42 43 Returns: 44 hojichar.Compose | hojichar.AsyncCompose: 45 """ 46 sys.path.append(str(Path(profile_path).parent)) 47 module = _load_module(profile_path) 48 if hasattr(module, "FILTER"): 49 filter = getattr(module, "FILTER") 50 if not isinstance(filter, (hojichar.Compose, hojichar.AsyncCompose)): 51 raise TypeError("FILTER must be Compose or AsyncCompose object.") 52 return filter 53 else: 54 raise NotImplementedError("FILTER is not defined in the profile.")
Loading a profile which has FILTER variable.
Given path is added to sys.path automaticaly.
Args: profile_path (str): Path to a Python file that implements custom filter hojichar.Compose must be defined as FILTER variable in the file.
Raises: NotImplementedError:
Returns: hojichar.Compose | hojichar.AsyncCompose:
57def load_factory_from_file( 58 profile_path: str | PathLike[str], 59) -> Callable[[Any | None], hojichar.Compose | hojichar.AsyncCompose]: 60 """ 61 Loading a function by a profile which has `FACTORY` variable. 62 Given path is added to sys.path automaticaly. 63 64 Args: 65 profile_path (PathLike): Path to a Python file that implements custom filter 66 67 Raises: 68 NotImplementedError: 69 70 Returns: 71 Callable[[Any | None], hojichar.Compose | hojichar.AsyncCompose]: 72 An alias of the function which returns Compose or AsyncCompose. 73 """ 74 sys.path.append(str(Path(profile_path).parent)) 75 module = _load_module(profile_path) 76 if hasattr(module, "FACTORY"): 77 factory: Callable[[Any | None], hojichar.Compose | hojichar.AsyncCompose] = getattr( 78 module, "FACTORY" 79 ) 80 return factory 81 else: 82 raise NotImplementedError("FACTORY is not defined in the profile")
Loading a function by a profile which has FACTORY variable.
Given path is added to sys.path automaticaly.
Args: profile_path (PathLike): Path to a Python file that implements custom filter
Raises: NotImplementedError:
Returns: Callable[[Any | None], hojichar.Compose | hojichar.AsyncCompose]: An alias of the function which returns Compose or AsyncCompose.
93def load_compose( 94 profile_path: str | PathLike[str], *factory_args: str 95) -> hojichar.Compose | hojichar.AsyncCompose: 96 """ 97 Loading a Compose file from profile. Loading a Compose file from the profile. 98 Both `FILTER` and `FACTORY` pattern of the profile is loaded. 99 100 Args: 101 profile_path (PathLike): 102 103 Returns: 104 hojichar.Compose: 105 """ 106 try: 107 filter = load_filter_from_file(profile_path) 108 _check_args_num_mismatch(len(factory_args)) 109 return filter 110 except NotImplementedError: 111 return load_parametrized_filter_from_file(profile_path, *factory_args)
Loading a Compose file from profile. Loading a Compose file from the profile.
Both FILTER and FACTORY pattern of the profile is loaded.
Args: profile_path (PathLike):
Returns: hojichar.Compose: