1
|
|
|
import abc |
|
|
|
|
2
|
|
|
from pathlib import Path |
3
|
|
|
from typing import Generic, Type, Optional, Mapping, Union |
4
|
|
|
|
5
|
|
|
import typer |
|
|
|
|
6
|
|
|
from typer.models import OptionInfo |
|
|
|
|
7
|
|
|
|
8
|
|
|
from mandos import MANDOS_SETUP, logger |
9
|
|
|
from mandos.entries.entries import S |
10
|
|
|
from mandos.entries.searcher import Searcher |
11
|
|
|
from mandos.model.searches import Search |
12
|
|
|
from mandos.model.utils import ReflectionUtils |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class Entry(Generic[S], metaclass=abc.ABCMeta): |
|
|
|
|
16
|
|
|
@classmethod |
17
|
|
|
def cmd(cls) -> str: |
|
|
|
|
18
|
|
|
key = cls._get_default_key() |
19
|
|
|
if isinstance(key, typer.models.OptionInfo): |
20
|
|
|
key = key.default |
21
|
|
|
if key is None or not isinstance(key, str): |
22
|
|
|
raise AssertionError(f"Key for {cls.__name__} is {key}") |
23
|
|
|
return key |
24
|
|
|
|
25
|
|
|
@classmethod |
26
|
|
|
def describe(cls) -> str: |
|
|
|
|
27
|
|
|
lines = [line.strip() for line in cls.run.__doc__.splitlines() if line.strip() != ""] |
28
|
|
|
return lines[0] |
29
|
|
|
|
30
|
|
|
@classmethod |
31
|
|
|
def run(cls, path: Path, **params) -> None: |
|
|
|
|
32
|
|
|
raise NotImplementedError() |
33
|
|
|
|
34
|
|
|
@classmethod |
35
|
|
|
def get_search_type(cls) -> Type[S]: |
|
|
|
|
36
|
|
|
# noinspection PyTypeChecker |
37
|
|
|
return ReflectionUtils.get_generic_arg(cls, Search) |
38
|
|
|
|
39
|
|
|
# noinspection PyUnusedLocal |
40
|
|
|
@classmethod |
41
|
|
|
def test(cls, path: Path, **params) -> None: |
|
|
|
|
42
|
|
|
cls.run(path, **{**params, **dict(check=True)}) |
43
|
|
|
|
44
|
|
|
@classmethod |
45
|
|
|
def _run( |
|
|
|
|
46
|
|
|
cls, |
|
|
|
|
47
|
|
|
built: S, |
|
|
|
|
48
|
|
|
path: Path, |
|
|
|
|
49
|
|
|
to: Optional[Path], |
|
|
|
|
50
|
|
|
check: bool, |
|
|
|
|
51
|
|
|
log: Optional[Path], |
|
|
|
|
52
|
|
|
quiet: bool, |
|
|
|
|
53
|
|
|
verbose: bool, |
|
|
|
|
54
|
|
|
no_setup: bool, |
|
|
|
|
55
|
|
|
): |
56
|
|
|
MANDOS_SETUP(verbose, quiet, log, no_setup) |
57
|
|
|
searcher = cls._get_searcher(built, path, to) |
58
|
|
|
logger.notice(f"Searching {built.key} [{built.search_class}] on {path}") |
59
|
|
|
out = searcher.output_paths[built.key] |
60
|
|
|
if not check: |
61
|
|
|
searcher.search() |
62
|
|
|
logger.notice(f"Done! Wrote to {out}") |
63
|
|
|
return searcher |
64
|
|
|
|
65
|
|
|
@classmethod |
66
|
|
|
def _get_searcher( |
|
|
|
|
67
|
|
|
cls, |
|
|
|
|
68
|
|
|
built: S, |
|
|
|
|
69
|
|
|
path: Path, |
|
|
|
|
70
|
|
|
to: Optional[Path], |
|
|
|
|
71
|
|
|
): |
72
|
|
|
return Searcher([built], [to], path) |
73
|
|
|
|
74
|
|
|
@classmethod |
75
|
|
|
def default_param_values(cls) -> Mapping[str, Union[str, float, int, Path]]: |
|
|
|
|
76
|
|
|
return { |
77
|
|
|
param: (value.default if isinstance(value, OptionInfo) else value) |
78
|
|
|
for param, value in ReflectionUtils.default_arg_values(cls.run).items() |
79
|
|
|
if param not in {"key", "path"} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
@classmethod |
83
|
|
|
def _get_default_key(cls) -> str: |
84
|
|
|
vals = ReflectionUtils.default_arg_values(cls.run) |
85
|
|
|
try: |
86
|
|
|
return vals["key"] |
87
|
|
|
except KeyError: |
88
|
|
|
logger.error(f"key not in {vals.keys()} for {cls.__name__}") |
89
|
|
|
raise |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
__all__ = ["Entry"] |
93
|
|
|
|