Skip to content
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
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Release notes

### Upcoming
- Added - Datajoint python CLI ([#940](https://github.com/datajoint/datajoint-python/issues/940)) PR [#1095](https://github.com/datajoint/datajoint-python/pull/1095)

### 0.14.1 -- Jun 02, 2023
- Fixed - Fix altering a part table that uses the "master" keyword - PR [#991](https://github.com/datajoint/datajoint-python/pull/991)
- Fixed - `.ipynb` output in tutorials is not visible in dark mode ([#1078](https://github.com/datajoint/datajoint-python/issues/1078)) PR [#1080](https://github.com/datajoint/datajoint-python/pull/1080)
Expand Down
2 changes: 1 addition & 1 deletion LNX-docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ services:
interval: 15s
fakeservices.datajoint.io:
<<: *net
image: datajoint/nginx:v0.2.5
image: datajoint/nginx:v0.2.6
environment:
- ADD_db_TYPE=DATABASE
- ADD_db_ENDPOINT=db:3306
Expand Down
2 changes: 2 additions & 0 deletions datajoint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"key",
"key_hash",
"logger",
"cli",
]

from .logging import logger
Expand All @@ -70,6 +71,7 @@
from .attribute_adapter import AttributeAdapter
from . import errors
from .errors import DataJointError
from .cli import cli

ERD = Di = Diagram # Aliases for Diagram
schema = Schema # Aliases for Schema
Expand Down
77 changes: 77 additions & 0 deletions datajoint/cli.py
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()
2 changes: 1 addition & 1 deletion local-docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ services:
interval: 15s
fakeservices.datajoint.io:
<<: *net
image: datajoint/nginx:v0.2.5
image: datajoint/nginx:v0.2.6
environment:
- ADD_db_TYPE=DATABASE
- ADD_db_ENDPOINT=db:3306
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
"automated research workflows",
],
packages=find_packages(exclude=["contrib", "docs", "tests*"]),
entry_points={
"console_scripts": ["dj=datajoint.cli:cli", "datajoint=datajoint.cli:cli"],
},
install_requires=requirements,
python_requires="~={}.{}".format(*min_py_version),
setup_requires=["otumat"], # maybe remove due to conflicts?
Expand Down
140 changes: 140 additions & 0 deletions tests/test_cli.py
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
Copy link
Contributor

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 respective pytest.fixtures.

vscode ➜ /workspaces/datajoint-python (hidden-1091-continued) $ pytest tests/test_cli.py
========================================================= test session starts ==========================================================
platform linux -- Python 3.11.4, pytest-8.2.2, pluggy-1.5.0
rootdir: /workspaces/datajoint-python
plugins: Faker-28.4.1, cov-5.0.0
collected 0 items / 1 error                                                                                                            

================================================================ ERRORS ================================================================
__________________________________________________ ERROR collecting tests/test_cli.py __________________________________________________
ImportError while importing test module '/workspaces/datajoint-python/tests/test_cli.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_cli.py:9: in <module>
    from . import CONN_INFO_ROOT, PREFIX
E   ImportError: cannot import name 'CONN_INFO_ROOT' from 'tests' (/workspaces/datajoint-python/tests/__init__.py)
======================================================= short test summary info ========================================================
ERROR tests/test_cli.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=========================================================== 1 error in 0.20s ===========================================================



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("'", '"'))