Passed
Push — dependabot/pip/flake8-bugbear-... ( 82a4d5...16d864 )
by
unknown
02:18
created

mandos.entries.multi_searches   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A MultiSearch.__init__() 0 9 4
A MultiSearch.search() 0 4 2
A MultiSearch._build_commands() 0 19 4
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
0 ignored issues
show
introduced by
Unable to import 'typer'
Loading history...
12
from pocketutils.core.dot_dict import NestedDotDict
0 ignored issues
show
introduced by
Unable to import 'pocketutils.core.dot_dict'
Loading history...
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:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
43
        cmds = self._build_commands()
44
        for key, (ent, params) in cmds.items():
0 ignored issues
show
Unused Code introduced by
The variable key seems to be unused.
Loading history...
45
            ent.run(self.path, **params)
46
47
    def _build_commands(
48
        self,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
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:
0 ignored issues
show
Coding Style Naming introduced by
Variable name "e" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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