Commit 29582220 authored by Aidan Richard Wiederhold's avatar Aidan Richard Wiederhold
Browse files

Merge branch 'print_summary' into 'aiwieder/api'

Moved the print_summary to command.py

See merge request !5
parents 3d5585d4 4b6b6d6f
Pipeline #3288468 passed with stage
in 2 minutes and 4 seconds
......@@ -43,6 +43,7 @@ console_scripts =
apd-cache = apd.command:cmd_cache_ap_info
apd-list-pfns = apd.command:cmd_list_pfns
apd-list-samples = apd.command:cmd_list_samples
apd-summary = apd.command:cmd_summary
###############################################################################
# Linting
......
......@@ -21,7 +21,6 @@ from apd.ap_info import (
load_ap_info,
safe_casefold,
)
from apd.rich_console import console, error_console
logger = logging.getLogger("apd")
......@@ -291,16 +290,16 @@ class AnalysisData:
def summary(self, tags: list = None) -> dict:
summary = {}
if tags is not None:
if tags:
for tag in tags:
if tag in self.available_tags:
try:
values = sorted(self.available_tags[tag])
except TypeError:
error_console.print(
f"WARNING: Could not sort the values for tag ({tag}). Please check that the values are sensible.\n"
)
values = list(self.available_tags[tag])
except TypeError as exc:
raise ValueError(
f"Could not sort the values for tag ({tag}). Please check that the values are sensible.\n"
) from exc
values = list(self.available_tags[tag])
summary[tag] = values
else:
raise ValueError(
......@@ -309,11 +308,3 @@ class AnalysisData:
else:
summary = self.available_tags
return summary
def print_summary(self, summary: dict = None, tags: list = None) -> None:
if summary is None:
console.print(self.summary(tags))
elif isinstance(summary, dict):
console.print(summary)
else:
raise ValueError(f"{summary} is not a valid dataset summary!")
......@@ -20,6 +20,7 @@ import click_log
from .analysis_data import AnalysisData
from .ap_info import cache_ap_info
from .rich_console import console
logger = logging.getLogger("apd")
click_log.basic_config(logger)
......@@ -190,3 +191,42 @@ def cmd_list_samples(
filter_tags |= dict(zip(tag, value))
matching = datasets(check_data=False, return_pfns=False, **filter_tags)
click.echo(matching)
@click.command()
@click.argument("working_group")
@click.argument("analysis")
@click.option(
"--cache_directory",
default=os.environ.get("APD_METADATA_CACHE_DIR", None),
help="Specify location of the cached analysis data files",
)
@click.option(
"--tag",
default=None,
help="Tag for which the values should be listed",
multiple=True,
)
@click_log.simple_verbosity_option(logger)
def cmd_summary(
working_group,
analysis,
cache_directory,
tag,
):
# Dealing with the cache
if not cache_directory:
cache_directory = "/tmp/apd_cache"
logger.debug("Cache directory not set, using %s", cache_directory)
if not os.path.exists(cache_directory):
logger.debug(
"Caching information for %s/%s to %s",
working_group,
analysis,
cache_directory,
)
cache_ap_info(cache_directory, working_group, analysis)
# Loading the dataset and displaying its summary
datasets = AnalysisData(working_group, analysis, metadata_cache=cache_directory)
console.print(datasets.summary(tag))
......@@ -85,3 +85,10 @@ def test_sample_check_load_dataset_error(apinfo_multipleversions):
polarity="magup",
)
datasets()
def test_summary(apinfo_multipleversions):
datasets = AnalysisData("SL", "RDs", metadata_cache=apinfo_multipleversions)
tagname = "datatype"
dt = set(datasets.summary([tagname])[tagname])
assert dt == set(["2011", "2018", "2016", "2017", "2012"])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment