Skip to content

Commit

Permalink
clean up cli doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
mcflugen committed Mar 2, 2024
1 parent a8fa93f commit afb8cf7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 82 deletions.
22 changes: 0 additions & 22 deletions src/standard_names/cmd/sndump.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,6 @@ def main(argv: tuple[str] | None = None) -> int:
----------
args : iterable of str, optional
Arguments to pass to *parse_args*. If not provided, use ``sys.argv``.
Examples
--------
>>> import os
>>> from standard_names.registry import NamesRegistry
>>> from standard_names.registry import _get_latest_names_file
>>> from standard_names.cmd.sndump import main
>>> (fname, _) = _get_latest_names_file()
>>> registry = NamesRegistry.from_path(fname)
>>> names = main(['-n', fname]).split(os.linesep)
>>> len(names) == len(registry)
True
>>> objects = main(['-o', fname]).split(os.linesep)
>>> len(objects) == len(registry.objects)
True
>>> names = main(['-n', '-o', fname]).split(os.linesep)
>>> len(names) == len(registry) + len(registry.objects)
True
"""
VALID_FIELDS = {
"op": "operators",
Expand Down
58 changes: 32 additions & 26 deletions src/standard_names/cmd/snscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,59 @@
> data/scraped.yaml
```
"""

import argparse
from collections.abc import Iterable

from standard_names.registry import NamesRegistry


def main(argv: tuple[str] | None = None) -> int:
parser = argparse.ArgumentParser("Scrape standard names from a file or URL")
parser.add_argument("file", nargs="*", metavar="FILE", help="URL or file to scrape")

args = parser.parse_args(argv)

registry = scrape_names(args.file)
print(registry.dumps(format_="text", fields=("names",)))

return 0


def scrape_names(files: Iterable[str]) -> NamesRegistry:
"""Scrape standard names from a file or URL.
Parameters
----------
files : iterable of str
Files to search for names.
Returns
-------
NamesRegistry
A registry of the names found in the files.
Examples
--------
>>> import os
>>> import tempfile
>>> import standard_names as csn
>>> from standard_names.cmd.snscrape import scrape_names
>>> contents = \"\"\"
>>> contents = '''
... A file with text and names (air__temperature) mixed in. Some names
... have double underscores (like, Water__Temperature) by are not
... valid names. Others, like water__temperature, are good.
... \"\"\"
... '''
>>> (fd, fname) = tempfile.mkstemp()
>>> os.close(fd)
>>> with open(fname, 'w') as fp:
>>> with tempfile.NamedTemporaryFile("w") as fp:
... print(contents, file=fp)
>>> names = csn.cmd.snscrape.main(
... [fp.name, '--reader=plain_text', '--no-headers'])
>>> names.split(os.linesep)
... _ = fp.seek(0)
... registry = scrape_names([fp.name])
>>> sorted(registry.names)
['air__temperature', 'water__temperature']
>>> os.remove(fname)
"""
import argparse

parser = argparse.ArgumentParser("Scrape standard names from a file or URL")
parser.add_argument("file", nargs="*", metavar="FILE", help="URL or file to scrape")

args = parser.parse_args(argv)

registry = NamesRegistry([])
for file in args.file:
for file in files:
registry |= NamesRegistry(search_file_for_names(file))
print(registry.dumps(format_="text", fields=("names",)))

return 0
return registry


def find_all_names(lines: Iterable[str], engine: str = "peg") -> set[str]:
Expand Down
77 changes: 43 additions & 34 deletions src/standard_names/cmd/snvalidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,14 @@

import argparse
import os
import sys
from collections.abc import Iterator

from standard_names.error import BadRegistryError
from standard_names.registry import NamesRegistry


def main(argv: tuple[str] | None = None) -> int:
"""Validate a list of names.
Examples
--------
>>> import os
>>> from standard_names.registry import _get_latest_names_file
>>> from standard_names.cmd.snvalidate import main
>>> (fname, _) = _get_latest_names_file()
>>> main([fname])
0
>>> import tempfile
>>> (fd, fname) = tempfile.mkstemp()
>>> os.close(fd)
>>> with open(fname, 'w') as fp:
... print('air__temperature', file=fp)
... print('Water__temperature', file=fp)
... print('water_temperature', file=fp)
>>> main([fp.name])
2
>>> os.remove(fname)
"""
"""Validate a list of names."""
parser = argparse.ArgumentParser("Validate a list of standard names")

parser.add_argument(
Expand All @@ -47,14 +22,48 @@ def main(argv: tuple[str] | None = None) -> int:

args = parser.parse_args(argv)

error_count = 0
invalid_names = set()
for file in args.file:
try:
NamesRegistry(file)
except BadRegistryError as err:
print(os.linesep.join(err.names), file=sys.stderr)
error_count += len(err.names)
return error_count
invalid_names |= validate_names(file)

print(os.linesep.join(invalid_names))

return len(invalid_names)


def validate_names(names: Iterator[str]) -> set[str]:
"""Find invalid names.
Examples
--------
>>> import os
>>> import tempfile
>>> from standard_names.registry import _get_latest_names_file
>>> from standard_names.cmd.snvalidate import validate_names
>>> (fname, _) = _get_latest_names_file()
>>> with open(fname) as fp:
... invalid_names = validate_names(fp)
>>> len(invalid_names)
0
>>> names = [
... "air__temperature",
... "Water__temperature",
... "water_temperature",
... ]
>>> invalid_names = validate_names(names)
>>> sorted(invalid_names)
['Water__temperature', 'water_temperature']
"""
try:
NamesRegistry(names)
except BadRegistryError as err:
invalid_names = set(err.names)
else:
invalid_names = set()

return invalid_names


if __name__ == "__main__":
Expand Down

0 comments on commit afb8cf7

Please sign in to comment.