refactor: update project code
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import importlib
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Any, Generic, List, Optional, Type, TypeVar
|
||||
from typing import Any, Generic, TypeVar
|
||||
|
||||
__all__ = ("Pipe", "PipeRegistry", "PipeParser", "PipeRunner", "PIPE_REGISTRY")
|
||||
|
||||
@@ -12,7 +12,7 @@ O = TypeVar("O")
|
||||
class Pipe(Generic[I, O]):
|
||||
type: str
|
||||
|
||||
def apply(self, value: I, basepath: Path) -> Optional[O]:
|
||||
def apply(self, value: I, basepath: Path) -> O | None:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@@ -20,12 +20,13 @@ class PipeRegistry:
|
||||
def __init__(self):
|
||||
self._pipes = {}
|
||||
|
||||
def register(self, pipe_type: Type[Pipe]):
|
||||
def register(self, pipe_type: type[Pipe]):
|
||||
self._pipes[pipe_type.type] = pipe_type
|
||||
|
||||
def register_module(self, module_name: str):
|
||||
module = importlib.import_module(module_name)
|
||||
modulepath = Path(module.__file__).parent
|
||||
# pylint:disable=too-many-nested-blocks
|
||||
for item in modulepath.iterdir():
|
||||
if item.suffix == ".py" and not item.name.startswith("__"):
|
||||
sub_module = importlib.import_module(f"{module_name}.{item.stem}")
|
||||
@@ -35,14 +36,14 @@ class PipeRegistry:
|
||||
if baseclass == Pipe:
|
||||
self.register(v)
|
||||
|
||||
def get(self, pipe_type: str) -> Type[Pipe]:
|
||||
def get(self, pipe_type: str) -> type[Pipe]:
|
||||
return self._pipes[pipe_type]
|
||||
|
||||
|
||||
class PipeParser:
|
||||
_PIPE_PATTERN = re.compile("^(\\w+)\\((.*)\\)")
|
||||
|
||||
def parse(self, pipeline: str) -> List[Pipe]:
|
||||
def parse(self, pipeline: str) -> list[Pipe]:
|
||||
chain = [item.strip() for item in pipeline.split("|")]
|
||||
result = []
|
||||
for part in chain:
|
||||
@@ -59,11 +60,11 @@ class PipeParser:
|
||||
|
||||
|
||||
class PipeRunner:
|
||||
def __init__(self, basepath: Path, parser: Optional[PipeParser] = None):
|
||||
def __init__(self, basepath: Path, parser: PipeParser | None = None):
|
||||
self._basepath = basepath
|
||||
self._parser = parser or PipeParser()
|
||||
|
||||
def run(self, pipes: List[Pipe]) -> Any:
|
||||
def run(self, pipes: list[Pipe]) -> Any:
|
||||
result = None
|
||||
for pipe in pipes:
|
||||
result = pipe.apply(result, self._basepath)
|
||||
@@ -75,7 +76,7 @@ class PipeRunner:
|
||||
pipe = self._parser.parse(pipeline)
|
||||
return self.run(pipe)
|
||||
|
||||
def check_conditions(self, conditions: Optional[List[str]]) -> bool:
|
||||
def check_conditions(self, conditions: list[str] | None) -> bool:
|
||||
if conditions:
|
||||
for condition in conditions:
|
||||
if not self.resolve_value(condition):
|
||||
|
||||
Reference in New Issue
Block a user