Skip to content

Commit

Permalink
feat: add username and password parameters to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
jjjermiah committed Feb 3, 2024
1 parent fc40dec commit 83421a3
Showing 1 changed file with 41 additions and 19 deletions.
60 changes: 41 additions & 19 deletions src/nbiatoolkit/nbia_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,30 @@ def version():

return

def _initialize_parser(description: str) -> argparse.ArgumentParser:
p = argparse.ArgumentParser(description=description)

Check warning on line 50 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L50

Added line #L50 was not covered by tests

# create an argparse group called "Credentials" to hold the username and password arguments
credentials = p.add_argument_group(

Check warning on line 53 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L53

Added line #L53 was not covered by tests
title="authentication parameters",
description="The username and password for the NBIA API if querying restricted datasets, defaults to the NBIA Guest Account")

credentials.add_argument(

Check warning on line 57 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L57

Added line #L57 was not covered by tests
"-u", "--username", action="store", type=str, default= "nbia_guest", #help="Username for the NBIA API (default: nbia_guest)"
)

credentials.add_argument(

Check warning on line 61 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L61

Added line #L61 was not covered by tests
"-p", "--password", action="store", type=str, default= "", #help="Password for the NBIA API (default: '')"
)

# make the credentials group show up first
p._action_groups.insert(0, p._action_groups.pop())

Check warning on line 66 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L66

Added line #L66 was not covered by tests

return p

Check warning on line 68 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L68

Added line #L68 was not covered by tests

def _add_extra_args(parser: argparse.ArgumentParser) -> argparse.Namespace:


def general_parser(parser: argparse.ArgumentParser) -> argparse.Namespace:
parser.add_argument(
"-o",
"--output",
Expand Down Expand Up @@ -176,7 +198,7 @@ def cli_wrapper(func, **kwargs) -> List[str] | None:
def getPatients_cli() -> None:
global query
query = "patients"
p = argparse.ArgumentParser(description=f"NBIAToolkit: {query} ")
p = _initialize_parser(description=f"NBIAToolkit: {query} ")

Check warning on line 201 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L201

Added line #L201 was not covered by tests

p.add_argument(
"-c",
Expand All @@ -186,15 +208,15 @@ def getPatients_cli() -> None:
type=str,
)

args = general_parser(p)
args = _add_extra_args(p)

Check warning on line 211 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L211

Added line #L211 was not covered by tests

return getResults_cli(func=NBIAClient().getPatients, Collection=args.collection)
return getResults_cli(func=NBIAClient(args.username, args.password).getPatients, Collection=args.collection)

Check warning on line 213 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L213

Added line #L213 was not covered by tests


def getCollections_cli() -> None:
global query
query = "collections"
p = argparse.ArgumentParser(description=f"NBIAToolkit: {query} ")
p = _initialize_parser(description=f"NBIAToolkit: {query} ")

Check warning on line 219 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L219

Added line #L219 was not covered by tests

p.add_argument(
"-p",
Expand All @@ -205,14 +227,14 @@ def getCollections_cli() -> None:
help="The prefix to filter collections by, i.e 'TCGA', 'LIDC', 'NSCLC'",
)

args = general_parser(p)
args = _add_extra_args(p)

Check warning on line 230 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L230

Added line #L230 was not covered by tests

return getResults_cli(func=NBIAClient().getCollections, prefix=args.prefix)
return getResults_cli(func=NBIAClient(args.username, args.password).getCollections, prefix=args.prefix)

Check warning on line 232 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L232

Added line #L232 was not covered by tests

def getNewPatients_cli() -> None:
global query
query = "newPatients"
p = argparse.ArgumentParser(description=f"NBIAToolkit: {query}. Get new patients from a collection since a given date.")
p = _initialize_parser(description=f"NBIAToolkit: {query}. Get new patients from a collection since a given date.")

Check warning on line 237 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L236-L237

Added lines #L236 - L237 were not covered by tests

p.add_argument(

Check warning on line 239 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L239

Added line #L239 was not covered by tests
"-c",
Expand All @@ -230,16 +252,16 @@ def getNewPatients_cli() -> None:
help="The date to filter by, i.e '2021-01-01' or '2019/12/31",
)

args = general_parser(p)
args = _add_extra_args(p)

Check warning on line 255 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L255

Added line #L255 was not covered by tests

return getResults_cli(func=NBIAClient().getNewPatients, Collection=args.collection, Date=args.date)
return getResults_cli(func=NBIAClient(args.username, args.password).getNewPatients, Collection=args.collection, Date=args.date)

Check warning on line 257 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L257

Added line #L257 was not covered by tests

def getBodyPartCounts_cli() -> None:
global query
global output
query = f"BodyPartCounts"

p = argparse.ArgumentParser(description=f"NBIAToolkit: {query} ")
p = _initialize_parser(description=f"NBIAToolkit: {query} ")

Check warning on line 264 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L264

Added line #L264 was not covered by tests

p.add_argument(
"-c",
Expand All @@ -250,10 +272,10 @@ def getBodyPartCounts_cli() -> None:
type=str,
)

args = general_parser(p)
args = _add_extra_args(p)

Check warning on line 275 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L275

Added line #L275 was not covered by tests

return getResults_cli(
func=NBIAClient().getBodyPartCounts, Collection=args.collection
func=NBIAClient(args.username, args.password).getBodyPartCounts, Collection=args.collection
)


Expand All @@ -262,7 +284,7 @@ def getSeries_cli() -> None:
global output
query = f"series"

p = argparse.ArgumentParser(description=f"NBIAToolkit: {query} ")
p = _initialize_parser(description=f"NBIAToolkit: {query} ")

Check warning on line 287 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L287

Added line #L287 was not covered by tests

p.add_argument(
"-c",
Expand Down Expand Up @@ -332,9 +354,9 @@ def getSeries_cli() -> None:
type=str,
)

args = general_parser(p)
args = _add_extra_args(p)

Check warning on line 357 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L357

Added line #L357 was not covered by tests
return getResults_cli(
func=NBIAClient().getSeries,
func=NBIAClient(args.username, args.password).getSeries,
Collection=args.collection,
PatientID=args.patientID,
StudyInstanceUID=args.studyInstanceUID,
Expand All @@ -351,7 +373,7 @@ def downloadSingleSeries_cli() -> None:
query = f"series"
# use the NBIAClient._downloadSingleSeries function to download a single series

p = argparse.ArgumentParser(description="NBIAToolkit: download a single series")
p = _initialize_parser(description="NBIAToolkit: download a single series")

Check warning on line 376 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L376

Added line #L376 was not covered by tests

p.add_argument(
"--seriesUID",
Expand Down Expand Up @@ -389,7 +411,7 @@ def downloadSingleSeries_cli() -> None:
args = p.parse_args()

return getResults_cli(
func=NBIAClient()._downloadSingleSeries,
func=NBIAClient(args.username, args.password)._downloadSingleSeries,
SeriesInstanceUID=args.seriesUID,
downloadDir=args.downloadDir,
filePattern=args.filePattern,
Expand All @@ -404,7 +426,7 @@ def downloadSingleSeries_cli() -> None:


def DICOMSorter_cli():
parser = argparse.ArgumentParser(
parser = _initialize_parser(

Check warning on line 429 in src/nbiatoolkit/nbia_cli.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/nbia_cli.py#L429

Added line #L429 was not covered by tests
description="NBIAToolkit: Sort DICOM files into destination directory according to target pattern."
)

Expand Down

0 comments on commit 83421a3

Please sign in to comment.