1
|
|
|
""" |
2
|
|
|
Command-line interface for mandos. |
3
|
|
|
""" |
4
|
|
|
|
5
|
|
|
from __future__ import annotations |
6
|
|
|
|
7
|
|
|
from typing import Type |
8
|
|
|
|
9
|
|
|
import typer |
|
|
|
|
10
|
|
|
from pocketutils.core import DictNamespace |
|
|
|
|
11
|
|
|
from typer.models import CommandInfo |
|
|
|
|
12
|
|
|
|
13
|
|
|
from mandos.model.utils.globals import Globals |
14
|
|
|
|
15
|
|
|
cli = typer.Typer() |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class CmdNamespace(DictNamespace): |
|
|
|
|
19
|
|
|
@classmethod |
20
|
|
|
def make(cls) -> CmdNamespace: |
|
|
|
|
21
|
|
|
from mandos.entry.calc_commands import CalcCommands |
|
|
|
|
22
|
|
|
from mandos.entry.entry_commands import Entries |
|
|
|
|
23
|
|
|
from mandos.entry.misc_commands import ( |
|
|
|
|
24
|
|
|
MiscCommands, |
25
|
|
|
_InsertedCommandListSingleton, |
26
|
|
|
) |
27
|
|
|
from mandos.entry.plot_commands import PlotCommands |
|
|
|
|
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 |
|
|
|
|
81
|
|
|
commands = None |
82
|
|
|
|
83
|
|
|
@classmethod |
84
|
|
|
def as_library(cls) -> Type[MandosCli]: |
|
|
|
|
85
|
|
|
from mandos.model.utils.setup import LOG_SETUP |
|
|
|
|
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]: |
|
|
|
|
103
|
|
|
from mandos.model.utils.setup import LOG_SETUP |
|
|
|
|
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): |
|
|
|
|
125
|
|
|
from mandos.entry.api_singletons import Apis |
|
|
|
|
126
|
|
|
|
127
|
|
|
Apis.set_default() |
128
|
|
|
|
129
|
|
|
@classmethod |
130
|
|
|
def start(cls): |
|
|
|
|
131
|
|
|
from mandos import MandosMetadata |
|
|
|
|
132
|
|
|
from mandos.model.utils.setup import logger |
|
|
|
|
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
|
|
|
|