-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
datajoint python CLI #1095
Closed
Closed
datajoint python CLI #1095
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c35c754
cli and setup script
A-Baji 088c52e
working cli
A-Baji 2645a37
cli tests
A-Baji 98b5c55
clean up tests
A-Baji eed9eba
bump nginx
A-Baji ec0af58
formatting
A-Baji 748914f
move to new tests
A-Baji b4f110d
remove schema
A-Baji c7776ed
changelog
A-Baji 219d6d6
Merge branch 'master' into dj-cli
dimitri-yatsenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
""" | ||
Collection of test cases to test the dj cli | ||
""" | ||
|
||
import json | ||
import subprocess | ||
import pytest | ||
import datajoint as dj | ||
from . import CONN_INFO_ROOT, PREFIX | ||
|
||
|
||
def test_cli_version(capsys): | ||
with pytest.raises(SystemExit) as pytest_wrapped_e: | ||
dj.cli(args=["-V"]) | ||
assert pytest_wrapped_e.type == SystemExit | ||
assert pytest_wrapped_e.value.code == 0 | ||
|
||
captured_output = capsys.readouterr().out | ||
assert captured_output == f"{dj.__name__} {dj.__version__}\n" | ||
|
||
|
||
def test_cli_help(capsys): | ||
with pytest.raises(SystemExit) as pytest_wrapped_e: | ||
dj.cli(args=["--help"]) | ||
assert pytest_wrapped_e.type == SystemExit | ||
assert pytest_wrapped_e.value.code == 0 | ||
|
||
captured_output = capsys.readouterr().out | ||
|
||
assert ( | ||
"\ | ||
usage: datajoint [--help] [-V] [-u USER] [-p PASSWORD] [-h HOST]\n\ | ||
[-s SCHEMAS [SCHEMAS ...]]\n\n\ | ||
\ | ||
DataJoint console interface.\n\n\ | ||
\ | ||
optional arguments:\n\ | ||
--help show this help message and exit\n\ | ||
-V, --version show program's version number and exit\n\ | ||
-u USER, --user USER Datajoint username\n\ | ||
-p PASSWORD, --password PASSWORD\n\ | ||
Datajoint password\n\ | ||
-h HOST, --host HOST Datajoint host\n\ | ||
-s SCHEMAS [SCHEMAS ...], --schemas SCHEMAS [SCHEMAS ...]\n\ | ||
A list of virtual module mappings in `db:schema ...`\n\ | ||
format\n" | ||
== captured_output | ||
) | ||
|
||
|
||
def test_cli_config(): | ||
process = subprocess.Popen( | ||
["dj"], | ||
stdin=subprocess.PIPE, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
text=True, | ||
) | ||
|
||
process.stdin.write("dj.config\n") | ||
process.stdin.flush() | ||
|
||
stdout, stderr = process.communicate() | ||
|
||
assert dj.config == json.loads( | ||
stdout[4:519] | ||
.replace("'", '"') | ||
.replace("None", "null") | ||
.replace("True", "true") | ||
.replace("False", "false") | ||
) | ||
|
||
|
||
def test_cli_args(): | ||
process = subprocess.Popen( | ||
["dj", "-utest_user", "-ptest_pass", "-htest_host"], | ||
stdin=subprocess.PIPE, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
text=True, | ||
) | ||
|
||
process.stdin.write("dj.config['database.user']\n") | ||
process.stdin.write("dj.config['database.password']\n") | ||
process.stdin.write("dj.config['database.host']\n") | ||
process.stdin.flush() | ||
|
||
stdout, stderr = process.communicate() | ||
assert "test_user" == stdout[5:14] | ||
assert "test_pass" == stdout[21:30] | ||
assert "test_host" == stdout[37:46] | ||
|
||
|
||
def test_cli_schemas(): | ||
schema = dj.Schema(PREFIX + "_cli", locals(), connection=dj.conn(**CONN_INFO_ROOT)) | ||
|
||
@schema | ||
class IJ(dj.Lookup): | ||
definition = """ # tests restrictions | ||
i : int | ||
j : int | ||
""" | ||
contents = list(dict(i=i, j=j + 2) for i in range(3) for j in range(3)) | ||
|
||
process = subprocess.Popen( | ||
["dj", "-s", "djtest_cli:test_schema"], | ||
stdin=subprocess.PIPE, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
text=True, | ||
) | ||
|
||
process.stdin.write("test_schema.__dict__['__name__']\n") | ||
process.stdin.write("test_schema.__dict__['schema']\n") | ||
process.stdin.write("test_schema.IJ.fetch(as_dict=True)\n") | ||
process.stdin.flush() | ||
|
||
stdout, stderr = process.communicate() | ||
fetch_res = [ | ||
{"i": 0, "j": 2}, | ||
{"i": 0, "j": 3}, | ||
{"i": 0, "j": 4}, | ||
{"i": 1, "j": 2}, | ||
{"i": 1, "j": 3}, | ||
{"i": 1, "j": 4}, | ||
{"i": 2, "j": 2}, | ||
{"i": 2, "j": 3}, | ||
{"i": 2, "j": 4}, | ||
] | ||
assert ( | ||
"\ | ||
dj repl\n\n\ | ||
\ | ||
schema modules:\n\n\ | ||
- test_schema" | ||
== stderr[159:200] | ||
) | ||
assert "'test_schema'" == stdout[4:17] | ||
assert "Schema `djtest_cli`" == stdout[22:41] | ||
assert fetch_res == json.loads(stdout[47:209].replace("'", '"')) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pytests not working in Dev Container with new pytest framework. Will need to migrate dependencies like
CONN_INFO_ROOT
to the respectivepytest.fixture
s.