Passed
Push — main ( 65730f...fad324 )
by Douglas
06:54 queued 02:27
created

mandos.cli.MandosCli.as_cli()   A

Complexity

Conditions 1

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nop 1
dl 0
loc 21
rs 9.55
c 0
b 0
f 0
1
"""
2
Command-line interface for mandos.
3
"""
4
5
from __future__ import annotations
6
7
from typing import Type
8
9
import typer
0 ignored issues
show
introduced by
Unable to import 'typer'
Loading history...
10
from pocketutils.core import DictNamespace
0 ignored issues
show
introduced by
Unable to import 'pocketutils.core'
Loading history...
11
from typer.models import CommandInfo
0 ignored issues
show
introduced by
Unable to import 'typer.models'
Loading history...
12
13
from mandos.model.utils.globals import Globals
14
15
cli = typer.Typer()
16
17
18
class CmdNamespace(DictNamespace):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
19
    @classmethod
20
    def make(cls) -> CmdNamespace:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
21
        from mandos.entry.calc_commands import CalcCommands
0 ignored issues
show
introduced by
Import outside toplevel (mandos.entry.calc_commands.CalcCommands)
Loading history...
22
        from mandos.entry.entry_commands import Entries
0 ignored issues
show
introduced by
Import outside toplevel (mandos.entry.entry_commands.Entries)
Loading history...
23
        from mandos.entry.misc_commands import (
0 ignored issues
show
introduced by
Import outside toplevel (mandos.entry.misc_commands.MiscCommands, mandos.entry.misc_commands._InsertedCommandListSingleton)
Loading history...
24
            MiscCommands,
25
            _InsertedCommandListSingleton,
26
        )
27
        from mandos.entry.plot_commands import PlotCommands
0 ignored issues
show
introduced by
Import outside toplevel (mandos.entry.plot_commands.PlotCommands)
Loading history...
28
29
        cli.registered_commands += [
30
            CommandInfo(":document", callback=MiscCommands.document),
31
            CommandInfo(":search", callback=MiscCommands.search),
32
            CommandInfo(":init", callback=MiscCommands.init, hidden=True),
33
            CommandInfo(":settings", callback=MiscCommands.list_settings, hidden=True),
34
            CommandInfo(":fill", callback=MiscCommands.fill),
35
            CommandInfo(":cache:data", callback=MiscCommands.cache_data),
36
            CommandInfo(":cache:taxa", callback=MiscCommands.cache_taxa),
37
            CommandInfo(":cache:g2p", callback=MiscCommands.cache_g2p),
38
            CommandInfo(":cache:clear", callback=MiscCommands.cache_clear),
39
            CommandInfo(":export:taxa", callback=MiscCommands.export_taxa),
40
            CommandInfo(":concat", callback=MiscCommands.concat),
41
            CommandInfo(":filter", callback=MiscCommands.filter),
42
            CommandInfo(":export:copy", callback=MiscCommands.export_copy),
43
            CommandInfo(":export:state", callback=MiscCommands.export_state),
44
            CommandInfo(":export:reify", callback=MiscCommands.export_reify),
45
            CommandInfo(":export:db", callback=MiscCommands.export_db, hidden=True),
46
            CommandInfo(":init-db", callback=MiscCommands.init_db, hidden=True),
47
            CommandInfo(":serve", callback=MiscCommands.serve, hidden=True),
48
            CommandInfo(":calc:enrichment", callback=CalcCommands.calc_enrichment),
49
            CommandInfo(":calc:phi", callback=CalcCommands.calc_phi),
50
            CommandInfo(":calc:psi", callback=CalcCommands.calc_psi),
51
            CommandInfo(":calc:ecfp", callback=CalcCommands.calc_ecfp, hidden=True),
52
            CommandInfo(":calc:psi-projection", callback=CalcCommands.calc_projection),
53
            CommandInfo(":calc:tau", callback=CalcCommands.calc_tau),
54
            CommandInfo(":plot:enrichment", callback=PlotCommands.plot_enrichment),
55
            CommandInfo(":plot:psi-projection", callback=PlotCommands.plot_projection),
56
            CommandInfo(":plot:psi-heatmap", callback=PlotCommands.plot_heatmap),
57
            CommandInfo(":plot:phi-vs-psi", callback=PlotCommands.plot_phi_psi),
58
            CommandInfo(":plot:tau", callback=PlotCommands.plot_tau),
59
        ]
60
        commands = {c.name: c for c in cli.registered_commands}
61
        # Oh dear this is a nightmare
62
        # it's really hard to create typer commands with dynamically configured params --
63
        # we really need to rely on its inferring of params
64
        # that makes this really hard to do well
65
        for entry in Entries:
66
            cmd = entry.cmd()
67
            info = CommandInfo(cmd, callback=entry.run)
68
            cli.registered_commands.append(info)
69
            # print(f"Registered {entry.cmd()} to {entry}")
70
            commands[cmd] = entry.run
71
        _InsertedCommandListSingleton.commands = cli.registered_commands
72
        return cls(**commands)
73
74
75
class MandosCli:
76
    """
77
    Global entry point for various stuff.
78
    """
79
80
    cli = cli
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable cli does not seem to be defined.
Loading history...
81
    commands = None
82
83
    @classmethod
84
    def as_library(cls) -> Type[MandosCli]:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
85
        from mandos.model.utils.setup import LOG_SETUP
0 ignored issues
show
introduced by
Import outside toplevel (mandos.model.utils.setup.LOG_SETUP)
Loading history...
86
87
        Globals.is_cli = False
88
        (
89
            LOG_SETUP.set_control(False)
90
            .config_levels(
91
                levels=LOG_SETUP.defaults.levels_extended,
92
                icons=LOG_SETUP.defaults.icons_extended,
93
                colors=LOG_SETUP.defaults.colors_extended,
94
            )
95
            .add_log_methods()
96
        )
97
        cls.start()
98
        cls.commands = CmdNamespace.make()
99
        return cls
100
101
    @classmethod
102
    def as_cli(cls) -> Type[MandosCli]:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
103
        from mandos.model.utils.setup import LOG_SETUP
0 ignored issues
show
introduced by
Import outside toplevel (mandos.model.utils.setup.LOG_SETUP)
Loading history...
104
105
        Globals.is_cli = True
106
        LOG_SETUP.logger.remove(None)
107
        (
108
            LOG_SETUP.set_control(True)
109
            .disable("chembl_webresource_client", "requests_cache", "urllib3")
110
            .config_levels(
111
                levels=LOG_SETUP.defaults.levels_extended,
112
                icons=LOG_SETUP.defaults.icons_extended,
113
                colors=LOG_SETUP.defaults.colors_red_green_safe,
114
            )
115
            .add_log_methods()
116
            .config_main(fmt=LOG_SETUP.defaults.fmt_simplified)
117
        )
118
        cls.start()
119
        cls.commands = CmdNamespace.make()
120
        cls.init_apis()
121
        return cls
122
123
    @classmethod
124
    def init_apis(cls):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
125
        from mandos.entry.api_singletons import Apis
0 ignored issues
show
introduced by
Import outside toplevel (mandos.entry.api_singletons.Apis)
Loading history...
126
127
        Apis.set_default()
128
129
    @classmethod
130
    def start(cls):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
131
        from mandos import MandosMetadata
0 ignored issues
show
introduced by
Import outside toplevel (mandos.MandosMetadata)
Loading history...
132
        from mandos.model.utils.setup import logger
0 ignored issues
show
introduced by
Import outside toplevel (mandos.model.utils.setup.logger)
Loading history...
133
134
        if MandosMetadata.version is None:
135
            logger.error("Could not load package metadata for mandos. Is it installed?")
136
        else:
137
            logger.info(f"Mandos v{MandosMetadata.version}")
138
139
140
if __name__ == "__main__":
141
    MandosCli.as_cli().cli()
142
143
144
__all__ = ["CmdNamespace", "MandosCli"]
145