-
Notifications
You must be signed in to change notification settings - Fork 14
adds resource list script #764
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d32ddbf
added basic script which given a valid resource type checks for avail…
wilsonmr 2644d12
cleaned up script and added option to just check locally or remotely
wilsonmr cb7a0c3
update how to check available fits to include using vp-list to check …
wilsonmr af2367e
address review points - rename doc and clean info strings for vp-list
wilsonmr 757bfc9
ensure renamed docs is in index
wilsonmr 5e59908
uses argparse choices and print available options in help
wilsonmr 2b27365
update docs to reflect change to help
wilsonmr e3e6647
added basic testing suite for vp-list
wilsonmr 8e98fc9
fix vp-list test which lists datasets to actually list datasets
wilsonmr 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 was deleted.
Oops, something went wrong.
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,39 @@ | ||
| # How to list the available resources | ||
|
|
||
| ## Using `vp-list` | ||
|
|
||
| In order to check what resources are available locally and for download, use | ||
| `vp-list` which will print out the names of resources. | ||
|
|
||
| ```bash | ||
| vp-list <resource type> | ||
| ``` | ||
|
|
||
| The options for resource type can be seen with `vp-list --help`. | ||
|
|
||
| ```bash | ||
| $ vp-list --help | ||
| usage: vp-list [-h] [-r | -l] resource | ||
|
|
||
| vp-list Script which lists available resources locally and remotely | ||
|
|
||
| positional arguments: | ||
| resource The type of resource to check availability for (locally | ||
| and/or remotely). Choose from: theories, fits, pdfs, | ||
| datasets. | ||
|
|
||
| ``` | ||
|
|
||
| You can use the options `-l/--local-only` or `-r/--remote-only` to only check | ||
| for resources available locally or remotely respectively. | ||
|
|
||
| ## Manually checking server - example with fits | ||
|
|
||
| You can also check manually on the storage servers for these resources. For example, | ||
| the bulk of the available fits can be found by going to the fits folder of the | ||
| NNPDF data server. Some other fits may be found in standalone folders in the home | ||
| folder of this server, but of course finding a specific fit here may require some | ||
| digging. For help in accessing the server, | ||
| please see [here](NNPDF-server). For information on how to download fits and | ||
| other resources, | ||
| please see the [Downloading resources](download) section of the vp-guide. |
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,104 @@ | ||
| """ | ||
| vp-list | ||
|
|
||
| Script which lists available resources locally and remotely | ||
|
|
||
| """ | ||
| import argparse | ||
| from functools import partial | ||
| import re | ||
| import logging | ||
|
|
||
| from reportengine import colors | ||
|
|
||
| from validphys.loader import FallbackLoader as L | ||
|
|
||
| log = logging.getLogger() | ||
| log.setLevel(logging.INFO) | ||
| log.addHandler(colors.ColorHandler()) | ||
|
|
||
| REMOTE_TOKEN = "downloadable_" | ||
| LOCAL_TOKEN = "available_" | ||
|
|
||
|
|
||
| def atoi(text): | ||
| """convert string to integer, if possible""" | ||
| return int(text) if text.isdigit() else text | ||
|
|
||
|
|
||
| def natural_keys(text): | ||
| """ | ||
| sort a list according to natural ordering | ||
| http://nedbatchelder.com/blog/200712/human_sorting.html | ||
|
|
||
| taken directly from https://stackoverflow.com/a/5967539 | ||
|
|
||
| """ | ||
| return [atoi(c) for c in re.split(r"(\d+)", text)] | ||
|
|
||
|
|
||
| sane_order = partial(sorted, key=natural_keys) | ||
|
|
||
|
|
||
| def main(command_line=None): | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
|
|
||
| attrs = dir(L) | ||
|
|
||
| available = [ | ||
| attr.lstrip(LOCAL_TOKEN) for attr in attrs if attr.startswith(LOCAL_TOKEN) | ||
| ] | ||
| downloadable = [ | ||
| attr.lstrip(REMOTE_TOKEN) for attr in attrs if attr.startswith(REMOTE_TOKEN) | ||
| ] | ||
| # set metavar and print choices in help string - otherwise looks ugly. | ||
| parser.add_argument( | ||
| "resource", | ||
| type=str, | ||
| choices={*available, *downloadable}, | ||
| help=( | ||
| "The type of resource to check availability for (locally and/or remotely). " | ||
| + "Choose from: " | ||
| + ", ".join(list({*available, *downloadable})) | ||
| + "." | ||
| ), | ||
| metavar="resource", | ||
| ) | ||
| g = parser.add_mutually_exclusive_group() | ||
| g.add_argument( | ||
| "-r", | ||
| "--remote-only", | ||
| dest="remote", | ||
| action="store_true", | ||
| default=False, | ||
| help="Only list remote resources", | ||
| ) | ||
| g.add_argument( | ||
| "-l", | ||
| "--local-only", | ||
| dest="local", | ||
| action="store_true", | ||
| default=False, | ||
| help="Only list local resources", | ||
| ) | ||
| args = parser.parse_args(command_line) | ||
|
|
||
| l = L() | ||
| if not args.remote: | ||
| local_res = getattr(l, LOCAL_TOKEN + args.resource, None) | ||
| if args.resource in available and local_res: | ||
| log.info("The following %s are available locally:", args.resource) | ||
| print("- " + "\n- ".join(sane_order(local_res))) | ||
| else: | ||
| log.info("No %s are available locally.", args.resource) | ||
| if not args.local: | ||
| remote_res = getattr(l, REMOTE_TOKEN + args.resource, None) | ||
| if args.resource in downloadable and remote_res: | ||
| log.info("The following %s are downloadable:", args.resource) | ||
| print("- " + "\n- ".join(sane_order(remote_res))) | ||
| else: | ||
| log.info("No %s are available to download.", args.resource) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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,64 @@ | ||
| """ | ||
| test_vplistscript.py | ||
|
|
||
| Module for testing vp-list. The output of which is dynamic and so we just check | ||
| that the script runs and gives some output | ||
| """ | ||
| from contextlib import redirect_stdout | ||
| import io | ||
|
|
||
| from validphys.scripts.vp_list import main | ||
|
|
||
| def test_listfits(): | ||
| """Checks listing fits returns output""" | ||
| f = io.StringIO() | ||
| cmd = ["fits"] | ||
| with redirect_stdout(f): | ||
| main(cmd) | ||
| assert f.getvalue() | ||
|
|
||
| def test_listpdfs(): | ||
| """Checks listing pdfs returns output""" | ||
| f = io.StringIO() | ||
| cmd = ["pdfs"] | ||
| with redirect_stdout(f): | ||
| main(cmd) | ||
| assert f.getvalue() | ||
|
|
||
| def test_listtheories(): | ||
| """Checks listing theories returns output""" | ||
| f = io.StringIO() | ||
| cmd = ["theories"] | ||
| with redirect_stdout(f): | ||
| main(cmd) | ||
| assert f.getvalue() | ||
|
|
||
| def test_listdatasets(): | ||
| """Checks listing datasets returns output""" | ||
| f = io.StringIO() | ||
| cmd = ["datasets"] | ||
| with redirect_stdout(f): | ||
| main(cmd) | ||
| assert f.getvalue() | ||
|
|
||
| def test_local(): | ||
| """Check local flag""" | ||
| f = io.StringIO() | ||
| cmd = ["datasets", "-l"] | ||
| with redirect_stdout(f): | ||
| main(cmd) | ||
| assert f.getvalue() | ||
|
|
||
| def test_remote(): | ||
| """Test remote flag on both datasets (which should return empty string) and pdfs | ||
| which returns output | ||
|
|
||
| """ | ||
| f = io.StringIO() | ||
| cmd_data = ["datasets", "-r"] | ||
| cmd_pdfs = ["pdfs", "-r"] | ||
| with redirect_stdout(f): | ||
| main(cmd_data) | ||
| assert not f.getvalue() | ||
| main(cmd_pdfs) | ||
| assert f.getvalue() |
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.
Uh oh!
There was an error while loading. Please reload this page.