1
|
|
|
""" |
2
|
|
|
Runner. |
3
|
|
|
""" |
4
|
|
|
|
5
|
|
|
from __future__ import annotations |
6
|
|
|
|
7
|
|
|
import logging |
8
|
|
|
import typing |
9
|
|
|
from pathlib import Path |
10
|
|
|
from typing import Optional, Union, Type |
11
|
|
|
import typer |
|
|
|
|
12
|
|
|
from pocketutils.core.dot_dict import NestedDotDict |
|
|
|
|
13
|
|
|
|
14
|
|
|
from mandos.model import InjectionError |
15
|
|
|
from mandos.entries.entries import Entries, Entry |
16
|
|
|
|
17
|
|
|
from mandos.entries.api_singletons import Apis |
18
|
|
|
|
19
|
|
|
logger = logging.getLogger(__package__) |
20
|
|
|
cli = typer.Typer() |
21
|
|
|
Apis.set_default() |
22
|
|
|
Chembl, Pubchem = Apis.Chembl, Apis.Pubchem |
23
|
|
|
|
24
|
|
|
EntriesByCmd = {e.cmd(): e for e in Entries} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
class MultiSearch: |
28
|
|
|
""" |
29
|
|
|
Ugh. |
30
|
|
|
""" |
31
|
|
|
|
32
|
|
|
def __init__(self, path: Path, config: Optional[Path]): |
33
|
|
|
if config is None: |
34
|
|
|
config = path.with_suffix(".toml") |
35
|
|
|
self.path = path |
36
|
|
|
if not self.path.exists(): |
37
|
|
|
raise FileNotFoundError(f"File {path} not found") |
38
|
|
|
if not config.exists(): |
39
|
|
|
raise FileNotFoundError(f"File {config} not found") |
40
|
|
|
self.toml_searches = NestedDotDict.read_toml(config).get_as("search", list, []) |
41
|
|
|
|
42
|
|
|
def search(self) -> None: |
|
|
|
|
43
|
|
|
cmds = self._build_commands() |
44
|
|
|
for key, (ent, params) in cmds.items(): |
|
|
|
|
45
|
|
|
ent.run(self.path, **params) |
46
|
|
|
|
47
|
|
|
def _build_commands( |
48
|
|
|
self, |
|
|
|
|
49
|
|
|
) -> typing.Dict[str, typing.Tuple[Type[Entry], typing.Mapping[str, Union[int, str, float]]]]: |
50
|
|
|
commands: typing.Dict[ |
51
|
|
|
str, typing.Tuple[Type[Entry], typing.Mapping[str, Union[int, str, float]]] |
52
|
|
|
] = {} |
53
|
|
|
for e in self.toml_searches: |
|
|
|
|
54
|
|
|
cmd = e.req_as("source", str) |
55
|
|
|
key = e.req_as("key", str) |
56
|
|
|
params = {k: v for k, v in e.items() if k != "source"} |
57
|
|
|
try: |
58
|
|
|
cmd = EntriesByCmd[cmd] |
59
|
|
|
except KeyError: |
60
|
|
|
raise InjectionError(f"Search command {cmd} (key {key}) does not exist") |
61
|
|
|
cmd.test(self.path, **params) |
62
|
|
|
if key in commands: |
63
|
|
|
raise ValueError(f"Repeated search key '{key}'") |
64
|
|
|
commands[key] = (cmd, params) |
65
|
|
|
return commands |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
__all__ = ["MultiSearch"] |
69
|
|
|
|