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 typer.models import CommandInfo |
|
|
|
|
11
|
|
|
|
12
|
|
|
from mandos import MandosLogging, logger |
13
|
|
|
from mandos.commands import MiscCommands |
14
|
|
|
from mandos.entries.api_singletons import Apis |
15
|
|
|
from mandos.entries.entries import Entries |
16
|
|
|
from mandos.model.settings import MANDOS_SETTINGS |
17
|
|
|
|
18
|
|
|
cli = typer.Typer() |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
class SearchCommands: |
22
|
|
|
""" |
23
|
|
|
Entry points for mandos. |
24
|
|
|
""" |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
def _init_commands(): |
28
|
|
|
# Oh dear this is a nightmare |
29
|
|
|
# it's really hard to create typer commands with dynamically configured params -- |
30
|
|
|
# we really need to rely on its inferring of params |
31
|
|
|
# that makes this really hard to do well |
32
|
|
|
for entry in Entries: |
33
|
|
|
info = CommandInfo(entry.cmd(), callback=entry.run) |
34
|
|
|
cli.registered_commands.append(info) |
35
|
|
|
# print(f"Registered {entry.cmd()} to {entry}") |
36
|
|
|
setattr(SearchCommands, entry.cmd(), entry.run) |
37
|
|
|
|
38
|
|
|
cli.registered_commands += [ |
39
|
|
|
CommandInfo(":search", callback=MiscCommands.search), |
40
|
|
|
CommandInfo(":export:tax-tree", callback=MiscCommands.build_taxonomy), |
41
|
|
|
CommandInfo(":tax:dl", callback=MiscCommands.dl_tax, hidden=True), |
42
|
|
|
CommandInfo(":cache", callback=MiscCommands.find), |
43
|
|
|
CommandInfo(":concat", callback=MiscCommands.concat), |
44
|
|
|
CommandInfo(":filter", callback=MiscCommands.filter), |
45
|
|
|
CommandInfo(":filter:taxa", callback=MiscCommands.filter_taxa), |
46
|
|
|
CommandInfo(":export:copy", callback=MiscCommands.copy), |
47
|
|
|
CommandInfo(":export:state", callback=MiscCommands.state), |
48
|
|
|
CommandInfo(":export:reify", callback=MiscCommands.reify), |
49
|
|
|
CommandInfo(":export:db", callback=MiscCommands.deposit), |
50
|
|
|
CommandInfo(":serve", callback=MiscCommands.serve), |
51
|
|
|
CommandInfo(":calc:scores", callback=MiscCommands.score), |
52
|
|
|
CommandInfo(":calc:matrix", callback=MiscCommands.matrix), |
53
|
|
|
CommandInfo(":calc:matrix-concordance", callback=MiscCommands.concordance), |
54
|
|
|
] |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
_init_commands() |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
class MandosCli: |
61
|
|
|
""" |
62
|
|
|
Global entry point for various stuff. For import by consumers. |
63
|
|
|
""" |
64
|
|
|
|
65
|
|
|
settings = MANDOS_SETTINGS |
66
|
|
|
logger = logger |
|
|
|
|
67
|
|
|
logging = MandosLogging |
68
|
|
|
main = cli |
69
|
|
|
search_cmds = SearchCommands |
70
|
|
|
misc_cmds = MiscCommands |
71
|
|
|
|
72
|
|
|
@classmethod |
73
|
|
|
def init(cls) -> Type[MandosCli]: |
|
|
|
|
74
|
|
|
MandosLogging.init() |
75
|
|
|
Apis.set_default() |
76
|
|
|
return cls |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
if __name__ == "__main__": |
80
|
|
|
MandosCli.init().main() |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
__all__ = ["MandosCli"] |
84
|
|
|
|