-
Notifications
You must be signed in to change notification settings - Fork 1
Data Search Command #12
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
Open
Tyler-g-hudson
wants to merge
3
commits into
isce3-testing:develop
Choose a base branch
from
Tyler-g-hudson:search-cmd
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import argparse | ||
from pathlib import Path | ||
|
||
from ..data_commands import data_search | ||
from ..defaults import default_workflowdata_path | ||
from ._utils import help_formatter | ||
|
||
|
||
def init_data_parsers(subparsers: argparse._SubParsersAction) -> None: | ||
""" | ||
Augment an argument parser with setup commands. | ||
|
||
Parameters | ||
------- | ||
parser : argparse.ArgumentParser | ||
The parser to add setup commands to. | ||
""" | ||
search_params = argparse.ArgumentParser(add_help=False) | ||
search_params.add_argument( | ||
"--data-file", | ||
"-f", | ||
type=Path, | ||
default=default_workflowdata_path(), | ||
metavar="FILENAME", | ||
help="The filename of the repository metadata file.", | ||
) | ||
search_params.add_argument( | ||
"--tags", | ||
"-t", | ||
nargs="+", | ||
action="append", | ||
default=[], | ||
metavar="TAG", | ||
help="A set of data repository tags. Can be used multiple times.", | ||
) | ||
search_params.add_argument( | ||
"--names", | ||
"-n", | ||
nargs="+", | ||
default=[], | ||
metavar="NAME", | ||
help="A set of data repository names.", | ||
) | ||
search_params.add_argument( | ||
"--all", | ||
"-a", | ||
action="store_true", | ||
default=False, | ||
help="If used, get all repositories. Other search parameters will be ignored.", | ||
) | ||
|
||
data_parser = subparsers.add_parser( | ||
"data", help="Perform data operations.", formatter_class=help_formatter | ||
) | ||
data_subparsers = data_parser.add_subparsers(dest="data_subcommand") | ||
|
||
search_parser = data_subparsers.add_parser( | ||
"search", | ||
parents=[search_params], | ||
help="Search a file for repository metadata.", | ||
formatter_class=help_formatter, | ||
) | ||
search_parser.add_argument( | ||
"--fields", | ||
nargs="+", | ||
default=[], | ||
metavar="FIELD", | ||
help="The metadata fields to be returned.", | ||
) | ||
|
||
|
||
def run_data(args: argparse.Namespace) -> None: | ||
data_subcommand = args.data_subcommand | ||
del args.data_subcommand | ||
if data_subcommand == "search": | ||
data_search(**vars(args)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
from pathlib import Path | ||
from typing import Dict, Iterable, List | ||
|
||
from .search import filtered_file_search, names_only_search, search_file | ||
|
||
|
||
def data_search( | ||
data_file: Path, | ||
tags: Iterable[Iterable[str]], | ||
names: Iterable[str], | ||
fields: Iterable[str], | ||
all: bool = False, | ||
print_output: bool = True, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
) -> List[Dict[str, str | Dict[str, str]]]: | ||
""" | ||
Query a file database for items. | ||
|
||
Parameters | ||
---------- | ||
data_file : Path | ||
The name of the database file. | ||
tags : Iterable[Iterable[str]] | ||
A set of sets of tags - this function will return the union of items that have | ||
all of any of the sets of tags passed in. | ||
names : Iterable[str] | ||
A list of names of data items to return. | ||
fields : Iterable[str] | ||
The set of fields to be returned on the data items. This should be a strict | ||
subset of the fields present on the items. Fields not included in this parameter | ||
will be filtered from the items prior to returning them. | ||
all : bool, optional | ||
If true, return all of the items in the database. Defaults to False | ||
|
||
Returns | ||
------- | ||
List[Dict[str, str | Dict[str, str]]] | ||
The items returned by the query, in dictionary format. | ||
""" | ||
if all: | ||
if (any(True for _ in names)) or (any(True for _ in tags)): | ||
print("'all' cannot be used in conjunction with 'tags' or 'names'.") | ||
exit() | ||
|
||
if fields == []: | ||
return search_file(tags=tags, names=names, filename=data_file, all=all) | ||
|
||
filtered_search = filtered_file_search( | ||
fields=fields, names=names, tags=tags, filename=data_file, all=all | ||
) | ||
|
||
if print_output: | ||
print(json.dumps(filtered_search, indent=2)) | ||
|
||
return filtered_search | ||
|
||
|
||
def data_names( | ||
data_file: Path, | ||
tags: Iterable[Iterable[str]], | ||
names: Iterable[str], | ||
all: bool = False, | ||
) -> List[str]: | ||
""" | ||
Query a database file and return the names of all data items that match the query. | ||
|
||
Parameters | ||
---------- | ||
data_file : Path | ||
The path to the database file. | ||
tags : Iterable[Iterable[str]] | ||
A set of sets of tags - this function will return the union of items that have | ||
all of any of the sets of tags passed in. | ||
names : Iterable[str] | ||
A list of names of data items to return. | ||
all : bool, optional | ||
If true, return all of the items in the database. Defaults to False | ||
|
||
Returns | ||
------- | ||
List[str] | ||
A list of the names of items that were returned by the query. | ||
""" | ||
if all: | ||
if (any(True for _ in names)) or (any(True for _ in tags)): | ||
print("'all' cannot be used in conjunction with 'tags' or 'names'.") | ||
exit() | ||
|
||
return names_only_search(tags=tags, names=names, filename=data_file, all=all) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
Path
specifically here instead ofstr | os.PathLike
, which is more abstract. Usually we prefer more abstract types as inputs in order to avoid overly constraining users.