Passed
Push — main ( 9ff912...d08a4e )
by Douglas
03:54
created

mandos.cli.Commands.binding()   A

Complexity

Conditions 1

Size

Total Lines 27
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nop 8
dl 0
loc 27
rs 9.352
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
from typing import Optional
10
import typer
0 ignored issues
show
introduced by
Unable to import 'typer'
Loading history...
11
from mandos.model.settings import MANDOS_SETTINGS
12
from mandos.model.taxonomy_caches import TaxonomyFactories
13
from mandos.search.entries import Entries
14
15
from mandos.search.api_singletons import Apis
16
from mandos.search.multi_searches import MultiSearch
17
from mandos.search.searcher import Searcher, SearcherUtils
18
19
logger = logging.getLogger(__package__)
20
# IMPORTANT!
21
Apis.set_default()
22
cli = typer.Typer()
23
24
25
class Commands:
26
    """
27
    Entry points for mandos.
28
    """
29
30
    @staticmethod
31
    def search(
32
        path: Path,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
33
        config: Optional[Path] = None,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
34
    ) -> None:
35
        """
36
        Runs multiple searches.
37
38
        Args:
39
            path:
40
            config:
41
        """
42
        MultiSearch(path, config).search()
43
44
    @staticmethod
45
    def find(
46
        path: Path,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
47
        pubchem: bool = True,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
48
        chembl: bool = True,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
49
    ) -> None:
50
        """
51
        Fetches and caches compound data.
52
        Useful to check what you can see before running a search.
53
        """
54
        out_path = path.with_suffix(".ids.csv")
55
        if out_path.exists():
56
            raise FileExistsError(out_path)
57
        inchikeys = SearcherUtils.read(path)
58
        df = SearcherUtils.dl(inchikeys, pubchem=pubchem, chembl=chembl)
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...
59
        df.to_csv(out_path)
60
        typer.echo(f"Wrote to {out_path}")
61
62
    @staticmethod
63
    def dl_tax(
64
        taxon: int,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
65
    ) -> None:
66
        """
67
        Preps a new taxonomy file for use in mandos.
68
        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...
69
        Otherwise, downloads a tab-separated file from UniProt.
70
        (To find manually, follow the ``All lower taxonomy nodes`` link and click ``Download``.)
71
        Then applies fixes and reduces the file size, creating a new file alongside.
72
        Puts both the raw data and fixed data in the cache under ``~/.mandos/taxonomy/``.
73
74
        Args:
75
            taxon: The **ID** of the UniProt taxon
76
        """
77
        TaxonomyFactories.from_uniprot(MANDOS_SETTINGS.taxonomy_cache_path).load(taxon)
78
79
80
# Oh dear this is a nightmare
81
# it's really hard to create typer commands with dynamically configured params --
82
# we really need to rely on its inferring of params
83
# that makes this really hard to do well
84
for entry in Entries:
85
    from typer.models import CommandInfo
0 ignored issues
show
introduced by
Unable to import 'typer.models'
Loading history...
86
87
    info = CommandInfo(entry.cmd(), callback=entry.run)
88
    cli.registered_commands.append(info)
89
    setattr(Commands, entry.cmd(), entry.run)
90
91
cli.registered_commands.extend(
92
    [
93
        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 84 is not entered. Are you sure this can never be the case?
Loading history...
94
        CommandInfo("dl_tax", callback=Commands.dl_tax, hidden=True),
95
    ]
96
)
97
98
99
if __name__ == "__main__":
100
    cli()
101
102
103
__all__ = ["Commands", "Searcher"]
104