1
|
|
|
""" |
2
|
|
|
Command-line interface for mandos. |
3
|
|
|
""" |
4
|
|
|
|
5
|
|
|
from __future__ import annotations |
6
|
|
|
|
7
|
|
|
import time |
8
|
|
|
from typing import Type |
9
|
|
|
|
10
|
|
|
import typer |
|
|
|
|
11
|
|
|
from loguru import logger |
|
|
|
|
12
|
|
|
from pocketutils.core import DictNamespace |
|
|
|
|
13
|
|
|
from pocketutils.misc.loguru_utils import FancyLoguru |
|
|
|
|
14
|
|
|
from pocketutils.tools.filesys_tools import FilesysTools |
|
|
|
|
15
|
|
|
from pocketutils.tools.sys_tools import SystemTools |
|
|
|
|
16
|
|
|
from typer.models import CommandInfo |
|
|
|
|
17
|
|
|
|
18
|
|
|
from mandos.model.utils.globals import Globals |
19
|
|
|
|
20
|
|
|
cli = typer.Typer() |
21
|
|
|
# .disable("chembl_webresource_client", "requests_cache", "urllib3", "numba") |
22
|
|
|
_filter = {"": "WARNING", "mandos": "TRACE"} |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
def _msg(msg: str): |
26
|
|
|
typer.echo(msg) |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
def _err(msg: str): |
30
|
|
|
msg = typer.style(msg, fg=typer.colors.RED) |
31
|
|
|
typer.echo(msg, err=True) |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
class CmdNamespace(DictNamespace): |
|
|
|
|
35
|
|
|
@classmethod |
36
|
|
|
def make(cls) -> CmdNamespace: |
|
|
|
|
37
|
|
|
from mandos.entry.calc_commands import CalcCommands |
|
|
|
|
38
|
|
|
from mandos.entry.entry_commands import Entries |
|
|
|
|
39
|
|
|
from mandos.entry.misc_commands import ( |
|
|
|
|
40
|
|
|
MiscCommands, |
41
|
|
|
_InsertedCommandListSingleton, |
42
|
|
|
) |
43
|
|
|
from mandos.entry.plot_commands import PlotCommands |
|
|
|
|
44
|
|
|
|
45
|
|
|
cli.registered_commands += [ |
46
|
|
|
CommandInfo(":document", callback=MiscCommands.document), |
47
|
|
|
CommandInfo(":search", callback=MiscCommands.search), |
48
|
|
|
CommandInfo(":init", callback=MiscCommands.init, hidden=True), |
49
|
|
|
CommandInfo(":settings", callback=MiscCommands.list_settings, hidden=True), |
50
|
|
|
CommandInfo(":fill", callback=MiscCommands.fill), |
51
|
|
|
CommandInfo(":cache:data", callback=MiscCommands.cache_data), |
52
|
|
|
CommandInfo(":cache:taxa", callback=MiscCommands.cache_taxa), |
53
|
|
|
CommandInfo(":cache:g2p", callback=MiscCommands.cache_g2p), |
54
|
|
|
CommandInfo(":cache:clear", callback=MiscCommands.cache_clear), |
55
|
|
|
CommandInfo(":export:taxa", callback=MiscCommands.export_taxa), |
56
|
|
|
CommandInfo(":concat", callback=MiscCommands.concat), |
57
|
|
|
CommandInfo(":filter", callback=MiscCommands.filter), |
58
|
|
|
CommandInfo(":export:copy", callback=MiscCommands.export_copy), |
59
|
|
|
CommandInfo(":export:state", callback=MiscCommands.export_state), |
60
|
|
|
CommandInfo(":export:reify", callback=MiscCommands.export_reify), |
61
|
|
|
CommandInfo(":export:db", callback=MiscCommands.export_db, hidden=True), |
62
|
|
|
CommandInfo(":init-db", callback=MiscCommands.init_db, hidden=True), |
63
|
|
|
CommandInfo(":serve", callback=MiscCommands.serve, hidden=True), |
64
|
|
|
CommandInfo(":calc:enrichment", callback=CalcCommands.calc_enrichment), |
65
|
|
|
CommandInfo(":calc:phi", callback=CalcCommands.calc_phi), |
66
|
|
|
CommandInfo(":calc:psi", callback=CalcCommands.calc_psi), |
67
|
|
|
CommandInfo(":calc:ecfp", callback=CalcCommands.calc_ecfp, hidden=True), |
68
|
|
|
CommandInfo(":calc:psi-projection", callback=CalcCommands.calc_projection), |
69
|
|
|
CommandInfo(":calc:tau", callback=CalcCommands.calc_tau), |
70
|
|
|
CommandInfo(":plot:enrichment", callback=PlotCommands.plot_enrichment), |
71
|
|
|
CommandInfo(":plot:psi-projection", callback=PlotCommands.plot_projection), |
72
|
|
|
CommandInfo(":plot:psi-heatmap", callback=PlotCommands.plot_heatmap), |
73
|
|
|
CommandInfo(":plot:phi-vs-psi", callback=PlotCommands.plot_phi_psi), |
74
|
|
|
CommandInfo(":plot:tau", callback=PlotCommands.plot_tau), |
75
|
|
|
] |
76
|
|
|
commands = {c.name: c for c in cli.registered_commands} |
77
|
|
|
# Oh dear this is a nightmare |
78
|
|
|
# it's really hard to create typer commands with dynamically configured params -- |
79
|
|
|
# we really need to rely on its inferring of params |
80
|
|
|
# that makes this really hard to do well |
81
|
|
|
for entry in Entries: |
82
|
|
|
cmd = entry.cmd() |
83
|
|
|
info = CommandInfo(cmd, callback=entry.run) |
84
|
|
|
cli.registered_commands.append(info) |
85
|
|
|
# print(f"Registered {entry.cmd()} to {entry}") |
86
|
|
|
commands[cmd] = entry.run |
87
|
|
|
_InsertedCommandListSingleton.commands = cli.registered_commands |
88
|
|
|
return cls(**commands) |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
class MandosCli: |
92
|
|
|
""" |
93
|
|
|
Global entry point for various stuff. |
94
|
|
|
""" |
95
|
|
|
|
96
|
|
|
cli = cli |
|
|
|
|
97
|
|
|
commands = None |
98
|
|
|
log_setup: FancyLoguru = None |
99
|
|
|
|
100
|
|
|
@classmethod |
101
|
|
|
def as_library(cls) -> Type[MandosCli]: |
|
|
|
|
102
|
|
|
from mandos.model.utils.setup import LOG_SETUP |
|
|
|
|
103
|
|
|
|
104
|
|
|
Globals.is_cli = False |
105
|
|
|
cls.log_setup = ( |
106
|
|
|
LOG_SETUP.set_control(False) |
107
|
|
|
.config_levels( |
108
|
|
|
levels=LOG_SETUP.defaults.levels_extended, |
109
|
|
|
icons=LOG_SETUP.defaults.icons_extended, |
110
|
|
|
colors=LOG_SETUP.defaults.colors_extended, |
111
|
|
|
) |
112
|
|
|
.add_log_methods() |
113
|
|
|
) |
114
|
|
|
cls.start() |
115
|
|
|
cls.commands = CmdNamespace.make() |
116
|
|
|
return cls |
117
|
|
|
|
118
|
|
|
@classmethod |
119
|
|
|
def as_cli(cls) -> Type[MandosCli]: |
|
|
|
|
120
|
|
|
from mandos.model.utils.setup import LOG_SETUP |
|
|
|
|
121
|
|
|
|
122
|
|
|
Globals.is_cli = True |
123
|
|
|
cls.log_setup = LOG_SETUP.logger.remove(None) |
124
|
|
|
cls.log_setup = ( |
125
|
|
|
LOG_SETUP.set_control(True) |
126
|
|
|
.config_levels( |
127
|
|
|
levels=LOG_SETUP.defaults.levels_extended, |
128
|
|
|
icons=LOG_SETUP.defaults.icons_extended, |
129
|
|
|
colors=LOG_SETUP.defaults.colors_red_green_safe, |
130
|
|
|
) |
131
|
|
|
.add_log_methods() |
132
|
|
|
.config_main(fmt=LOG_SETUP.defaults.fmt_simplified, filter=_filter) |
133
|
|
|
.intercept_std() |
134
|
|
|
) |
135
|
|
|
cls.start() |
136
|
|
|
cls.commands = CmdNamespace.make() |
137
|
|
|
cls.init_apis() |
138
|
|
|
return cls |
139
|
|
|
|
140
|
|
|
@classmethod |
141
|
|
|
def init_apis(cls): |
|
|
|
|
142
|
|
|
from mandos.entry.api_singletons import Apis |
|
|
|
|
143
|
|
|
|
144
|
|
|
Apis.set_default() |
145
|
|
|
|
146
|
|
|
@classmethod |
147
|
|
|
def start(cls): |
|
|
|
|
148
|
|
|
from mandos import MandosMetadata |
|
|
|
|
149
|
|
|
from mandos.model.utils.setup import logger |
|
|
|
|
150
|
|
|
|
151
|
|
|
if MandosMetadata.version is None: |
152
|
|
|
logger.error("Could not load package metadata for mandos. Is it installed?") |
153
|
|
|
else: |
154
|
|
|
logger.info(f"Mandos v{MandosMetadata.version}") |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
class MandosTyperCli: |
|
|
|
|
158
|
|
|
def __init__(self): |
159
|
|
|
self._mandos = None |
160
|
|
|
|
161
|
|
|
def main(self) -> None: |
|
|
|
|
162
|
|
|
try: |
163
|
|
|
self._mandos = MandosCli.as_cli() |
164
|
|
|
self._mandos.cli() |
165
|
|
|
except (KeyboardInterrupt, typer.Abort) as e: |
|
|
|
|
166
|
|
|
self._fail(e, abort=True) |
167
|
|
|
quit(1) |
|
|
|
|
168
|
|
|
except (SystemExit, typer.Exit) as e: |
|
|
|
|
169
|
|
|
_err("Abnormal system exit.") |
170
|
|
|
self._fail(e, abort=True) |
171
|
|
|
quit(2) |
|
|
|
|
172
|
|
|
except BaseException as e: |
|
|
|
|
173
|
|
|
_err("Error.") |
174
|
|
|
self._fail(e, abort=False) |
175
|
|
|
quit(-1) |
|
|
|
|
176
|
|
|
|
177
|
|
|
def _fail(self, e: BaseException, *, abort: bool) -> None: |
|
|
|
|
178
|
|
|
if not abort or True: |
179
|
|
|
msg = ( |
180
|
|
|
"\n".join(SystemTools.serialize_exception_msg(e)) |
181
|
|
|
.replace("[ exc_info True ]", "") |
182
|
|
|
.strip() |
183
|
|
|
) |
184
|
|
|
if len(msg) > 0: |
185
|
|
|
_err(f"< Command failed: {msg} >") |
186
|
|
|
logger.opt(exception=True).critical(f"Command failed: {msg}") |
187
|
|
|
self._dump_error(e) |
188
|
|
|
if self._mandos and self._mandos.log_setup and self._mandos.log_setup.only_path: |
189
|
|
|
log_path = self._mandos.log_setup.only_path |
190
|
|
|
_err(f"See the log file: {log_path.resolve()}") |
191
|
|
|
time.sleep(0.5) |
192
|
|
|
|
193
|
|
|
def _dump_error(self, e: BaseException) -> None: |
|
|
|
|
194
|
|
|
try: |
195
|
|
|
dmp_path = FilesysTools.dump_error(e) |
196
|
|
|
_err(f"Wrote error and system info to: {dmp_path.resolve()}") |
197
|
|
|
except BaseException: |
|
|
|
|
198
|
|
|
_err("Note: Failed to write an error dump") |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
if __name__ == "__main__": |
202
|
|
|
MandosTyperCli().main() |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
__all__ = ["CmdNamespace", "MandosCli"] |
206
|
|
|
|