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

codelists add command #294

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
25 changes: 24 additions & 1 deletion opensafely/codelists.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def show_help(**kwargs):
title="available commands", description="", metavar="COMMAND"
)

parser_update = subparsers.add_parser(
"add", help="Add an OpenCodelists codelist to your project"
)

parser_update = subparsers.add_parser(
"update",
help=(
Expand Down Expand Up @@ -63,6 +67,20 @@ def main():
pass


def add(codelist_url, codelists_dir=None):
if not codelists_dir:
codelists_dir = Path.cwd() / CODELISTS_DIR
codelists_file = get_codelist_file(codelists_dir)
if OPENCODELISTS_BASE_URL not in codelist_url:
exit_with_error(f"Unable to parse URL, {OPENCODELISTS_BASE_URL} not found")
line = codelist_url.replace(f"{OPENCODELISTS_BASE_URL}/codelist/", "")
with codelists_file.open("r+") as f:
if not f.readlines()[-1].endswith("\n"):
line = "\n" + line
f.write(line + "\n")
update(codelists_dir)


def update(codelists_dir=None):
if not codelists_dir:
codelists_dir = Path.cwd() / CODELISTS_DIR
Expand Down Expand Up @@ -247,12 +265,17 @@ class Codelist:
filename: Path


def parse_codelist_file(codelists_dir):
def get_codelist_file(codelists_dir):
if not codelists_dir.exists() or not codelists_dir.is_dir():
exit_with_error(f"No '{CODELISTS_DIR}' folder found")
codelists_file = codelists_dir / CODELISTS_FILE
if not codelists_file.exists():
exit_with_error(f"No file found at '{CODELISTS_DIR}/{CODELISTS_FILE}'")
return codelists_file


def parse_codelist_file(codelists_dir):
codelists_file = get_codelist_file(codelists_dir)
codelists = []
codelist_versions = {}
for line in codelists_file.read_text().splitlines():
Expand Down
28 changes: 28 additions & 0 deletions tests/test_codelists.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,31 @@ def test_codelists_check_with_upstream_changes_in_CI(
os.chdir(codelists_path)
# check doesn't fail in CI if there are upstream errors only
assert codelists.check()


def test_codelists_add(codelists_path, requests_mock):
codelists_path /= "codelists"
codelists_file = codelists_path / "codelists.txt"
prior_codelists = codelists_file.read_text()
for codelist in prior_codelists.split("\n"):
if codelist:
requests_mock.get(
f"https://www.opencodelists.org/codelist/{codelist.rstrip('/')}/download.csv",
text="foo",
)
requests_mock.get(
"https://www.opencodelists.org/"
"codelist/project123/codelist456/version1/download.csv",
text="foo",
)

codelists.add(
"https://www.opencodelists.org/codelist/project123/codelist456/version1",
codelists_path,
)

assert (
codelists_file.read_text()
== prior_codelists + "project123/codelist456/version1\n"
)
assert (codelists_path / "project123-codelist456.csv").read_text() == "foo"