Skip to content

Commit

Permalink
codelists add command
Browse files Browse the repository at this point in the history
Allow users to add OpenCodelists codelists to the codelists.txt
file by URL.
  • Loading branch information
Jongmassey committed Nov 28, 2024
1 parent c62aef9 commit 1e72d63
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
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
26 changes: 26 additions & 0 deletions tests/test_codelists.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,29 @@ 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"
)

0 comments on commit 1e72d63

Please sign in to comment.