forked from datajoint/datajoint-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
77 lines (70 loc) · 2.02 KB
/
cli.py
File metadata and controls
77 lines (70 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import argparse
from code import interact
from collections import ChainMap
import datajoint as dj
def cli(args: list = None):
"""
Console interface for DataJoint Python
:param args: List of arguments to be passed in, defaults to reading stdin
:type args: list, optional
"""
parser = argparse.ArgumentParser(
prog="datajoint",
description="DataJoint console interface.",
conflict_handler="resolve",
)
parser.add_argument(
"-V", "--version", action="version", version=f"{dj.__name__} {dj.__version__}"
)
parser.add_argument(
"-u",
"--user",
type=str,
default=dj.config["database.user"],
required=False,
help="Datajoint username",
)
parser.add_argument(
"-p",
"--password",
type=str,
default=dj.config["database.password"],
required=False,
help="Datajoint password",
)
parser.add_argument(
"-h",
"--host",
type=str,
default=dj.config["database.host"],
required=False,
help="Datajoint host",
)
parser.add_argument(
"-s",
"--schemas",
nargs="+",
type=str,
required=False,
help="A list of virtual module mappings in `db:schema ...` format",
)
kwargs = vars(parser.parse_args(args))
mods = {}
if kwargs["user"]:
dj.config["database.user"] = kwargs["user"]
if kwargs["password"]:
dj.config["database.password"] = kwargs["password"]
if kwargs["host"]:
dj.config["database.host"] = kwargs["host"]
if kwargs["schemas"]:
for vm in kwargs["schemas"]:
d, m = vm.split(":")
mods[m] = dj.create_virtual_module(m, d)
banner = "dj repl\n"
if mods:
modstr = "\n".join(" - {}".format(m) for m in mods)
banner += "\nschema modules:\n\n" + modstr + "\n"
interact(banner, local=dict(ChainMap(mods, locals(), globals())))
raise SystemExit
if __name__ == "__main__":
cli()