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

mandos.cli.Commands.go_search()   A

Complexity

Conditions 1

Size

Total Lines 29
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
nop 9
dl 0
loc 29
rs 9.304
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
"""
2
Command-line interface for mandos.
3
"""
4
5
from __future__ import annotations
6
7
import logging
8
from pathlib import Path
9
import typer
0 ignored issues
show
introduced by
Unable to import 'typer'
Loading history...
10
from mandos.model.settings import MANDOS_SETTINGS
11
from mandos.model.taxonomy_caches import TaxonomyFactories
12
from mandos.entries.entries import Entries, _Typer
13
14
from mandos.entries.api_singletons import Apis
15
from mandos.entries.multi_searches import MultiSearch
16
from mandos.entries.searcher import SearcherUtils
17
18
logger = logging.getLogger(__package__)
19
# IMPORTANT!
20
Apis.set_default()
21
cli = typer.Typer()
22
23
24
class Commands:
25
    """
26
    Entry points for mandos.
27
    """
28
29
    @staticmethod
30
    def search(
31
        path: Path = _Typer.path,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
32
        config: Path = typer.Argument(
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
33
            None,
34
            help=".toml config file. See docs.",
35
            exists=True,
36
            dir_okay=False,
37
            readable=True,
38
        ),
39
    ) -> None:
40
        """
41
        Run multiple searches.
42
        """
43
        MultiSearch(path, config).search()
44
45
    @staticmethod
46
    def find(
47
        path: Path = _Typer.path,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
48
        pubchem: bool = typer.Option(True, help="Download data from PubChem"),
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
49
        chembl: bool = typer.Option(True, help="Download data from ChEMBL"),
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
50
        hmdb: bool = typer.Option(True, help="Download data from HMDB"),
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
51
    ) -> None:
52
        """
53
        Fetches and caches compound data.
54
        Useful to check what you can see before running a search.
55
        """
56
        out_path = path.with_suffix(".ids.csv")
57
        if out_path.exists():
58
            raise FileExistsError(out_path)
59
        inchikeys = SearcherUtils.read(path)
60
        df = SearcherUtils.dl(inchikeys, pubchem=pubchem, chembl=chembl, hmdb=hmdb)
0 ignored issues
show
Coding Style Naming introduced by
Variable name "df" 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...
61
        df.to_csv(out_path)
62
        typer.echo(f"Wrote to {out_path}")
63
64
    @staticmethod
65
    def dl_tax(
66
        taxon: int,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
67
    ) -> None:
68
        """
69
        Preps a new taxonomy file for use in mandos.
70
        Just returns if a corresponding file already exists in the resources dir or mandos cache (``~/.mandos``).
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (113/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
71
        Otherwise, downloads a tab-separated file from UniProt.
72
        (To find manually, follow the ``All lower taxonomy nodes`` link and click ``Download``.)
73
        Then applies fixes and reduces the file size, creating a new file alongside.
74
        Puts both the raw data and fixed data in the cache under ``~/.mandos/taxonomy/``.
75
76
        Args:
77
            taxon: The **ID** of the UniProt taxon
78
        """
79
        TaxonomyFactories.from_uniprot(MANDOS_SETTINGS.taxonomy_cache_path).load(taxon)
80
81
82
# Oh dear this is a nightmare
83
# it's really hard to create typer commands with dynamically configured params --
84
# we really need to rely on its inferring of params
85
# that makes this really hard to do well
86
for entry in Entries:
87
    from typer.models import CommandInfo
0 ignored issues
show
introduced by
Unable to import 'typer.models'
Loading history...
88
89
    info = CommandInfo(entry.cmd(), callback=entry.run)
90
    cli.registered_commands.append(info)
91
    setattr(Commands, entry.cmd(), entry.run)
92
93
cli.registered_commands.extend(
94
    [
95
        CommandInfo("@search", callback=Commands.search),
0 ignored issues
show
introduced by
The variable CommandInfo does not seem to be defined in case the for loop on line 86 is not entered. Are you sure this can never be the case?
Loading history...
96
        CommandInfo("@dl-tax", callback=Commands.dl_tax, hidden=True),
97
    ]
98
)
99
100
101
if __name__ == "__main__":
102
    cli()
103
104
105
__all__ = ["Commands"]
106