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):
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from pathlib import Path
|
||||
from typing import Any, Optional
|
||||
from typing import Any
|
||||
|
||||
from ...util import reflect
|
||||
from ..core import Pipe
|
||||
@@ -16,7 +16,7 @@ class FileData:
|
||||
".json": "json:load",
|
||||
}
|
||||
|
||||
def __init__(self, filepath: Path, loader: Optional[str] = None):
|
||||
def __init__(self, filepath: Path, loader: str | None = None):
|
||||
self._filepath = filepath
|
||||
self._loader = loader
|
||||
self._data = None
|
||||
@@ -38,6 +38,7 @@ class FileData:
|
||||
self._load_data()
|
||||
if self._data:
|
||||
return self._data.get(key)
|
||||
return None
|
||||
|
||||
def __bool__(self):
|
||||
return self._filepath.exists()
|
||||
@@ -46,11 +47,11 @@ class FileData:
|
||||
class FilePipe(Pipe[None, Any]):
|
||||
type = "file"
|
||||
|
||||
def __init__(self, filename: Optional[str], loader: Optional[str] = None):
|
||||
def __init__(self, filename: str | None, loader: str | None = None):
|
||||
self._filename = filename
|
||||
self._loader = loader
|
||||
|
||||
def apply(self, value: Optional[str], basepath: Path) -> Optional[Any]:
|
||||
def apply(self, value: str | None, basepath: Path) -> Any | None:
|
||||
filepath = Path(value or self._filename) # TODO: raise none filepath exception
|
||||
if not filepath.is_absolute():
|
||||
filepath = basepath / filepath
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from pathlib import Path
|
||||
from typing import Any, Optional
|
||||
from typing import Any
|
||||
|
||||
from ..core import Pipe
|
||||
|
||||
@@ -12,7 +12,7 @@ class GetPipe(Pipe[dict, Any]):
|
||||
def __init__(self, keypath: str):
|
||||
self._keypath = keypath
|
||||
|
||||
def apply(self, value: dict, basepath: Path) -> Optional[Any]:
|
||||
def apply(self, value: dict, basepath: Path) -> Any | None:
|
||||
path = self._keypath.split(".")
|
||||
result = value.get(path[0])
|
||||
for key in path[1:]:
|
||||
@@ -24,9 +24,9 @@ class GetPipe(Pipe[dict, Any]):
|
||||
class KeyPipe(Pipe[dict, Any]):
|
||||
type = "key"
|
||||
|
||||
def __init__(self, keyindex: Optional[str]):
|
||||
def __init__(self, keyindex: str | None):
|
||||
self._keyindex = int(keyindex) if keyindex else 0
|
||||
|
||||
def apply(self, value: dict, basepath: Path) -> Optional[Any]:
|
||||
def apply(self, value: dict, basepath: Path) -> Any | None:
|
||||
keys = list(value.keys())
|
||||
return keys[self._keyindex]
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from ..core import Pipe
|
||||
|
||||
@@ -14,7 +13,8 @@ class SearchPipe(Pipe[str, str]):
|
||||
self._patter = patter
|
||||
self._position = int(position)
|
||||
|
||||
def apply(self, value: str, basepath: Path) -> Optional[str]:
|
||||
def apply(self, value: str, basepath: Path) -> str | None:
|
||||
matched = re.compile(self._patter).search(value)
|
||||
if matched:
|
||||
return matched.group(self._position)
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user