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