-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from csdms/mcflugen/update-list
Update standard names list
- Loading branch information
Showing
25 changed files
with
7,406 additions
and
1,150 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,6 @@ | ||
from __future__ import annotations | ||
|
||
from standard_names.cli.main import main | ||
|
||
if __name__ == "__main__": | ||
raise SystemExit(main()) |
This file contains 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,117 @@ | ||
from collections.abc import Iterable | ||
|
||
|
||
def as_wiki_list( | ||
items: Iterable[str], heading: str | None = None, level: int = 1 | ||
) -> str: | ||
""" | ||
Examples | ||
-------- | ||
>>> from standard_names._format import as_wiki_list | ||
>>> print(as_wiki_list(["line 1", "line 2"], heading="Lines")) | ||
= Lines = | ||
<tt> | ||
line 1<br/> | ||
line 2<br/> | ||
</tt> | ||
""" | ||
newline = "\n" | ||
|
||
if heading is not None: | ||
formatted_lines = [f"{'=' * level} {heading} {'=' * level}"] | ||
else: | ||
formatted_lines = [] | ||
|
||
formatted_lines += ["<tt>"] + [item.strip() + "<br/>" for item in items] + ["</tt>"] | ||
|
||
return newline.join(formatted_lines) | ||
|
||
|
||
def as_yaml_list( | ||
items: Iterable[str], heading: str | None = None, level: int = 1 | ||
) -> str: | ||
""" | ||
Examples | ||
-------- | ||
>>> from standard_names._format import as_yaml_list | ||
>>> print(as_yaml_list(["line 1", "line 2"], heading="Lines")) | ||
Lines: | ||
- line 1 | ||
- line 2 | ||
""" | ||
newline = "\n" | ||
indent = 2 if heading else 0 | ||
formatted_lines = [f"{heading}:"] if heading else [] | ||
|
||
if heading is None: | ||
formatted_lines = [] | ||
indent = 0 | ||
else: | ||
formatted_lines = [f"{heading}:"] | ||
indent = 2 | ||
|
||
stripped_items = [stripped for item in items if (stripped := item.strip())] | ||
|
||
if stripped_items: | ||
formatted_lines += [f"{' ' * indent}- {item}" for item in stripped_items] | ||
else: | ||
formatted_lines += [f"{' ' * indent}[]"] | ||
|
||
return newline.join(formatted_lines) | ||
|
||
|
||
def as_myst_list( | ||
items: Iterable[str], heading: str | None = None, level: int = 1 | ||
) -> str: | ||
""" | ||
Examples | ||
-------- | ||
>>> from standard_names._format import as_myst_list | ||
>>> print(as_myst_list(["line 1", "line 2"], heading="Lines")) | ||
# Lines | ||
* line 1 | ||
* line 2 | ||
""" | ||
newline = "\n" | ||
|
||
formatted_lines = ([f"# {heading}"] if heading else []) + [ | ||
f"* {stripped}" for item in items if (stripped := item.strip()) | ||
] | ||
|
||
return newline.join(formatted_lines) | ||
|
||
|
||
def as_text_list( | ||
items: Iterable[str], heading: str | None = None, level: int = 1 | ||
) -> str: | ||
""" | ||
Examples | ||
-------- | ||
>>> from standard_names._format import as_text_list | ||
>>> print(as_text_list(["line 1", "line 2"], heading="# Lines")) | ||
# Lines | ||
line 1 | ||
line 2 | ||
""" | ||
newline = "\n" | ||
|
||
formatted_lines = ([heading] if heading else []) + [ | ||
stripped for item in items if (stripped := item.strip()) | ||
] | ||
|
||
return newline.join(formatted_lines) | ||
|
||
|
||
FORMATTERS = { | ||
"wiki": as_wiki_list, | ||
"yaml": as_yaml_list, | ||
"text": as_text_list, | ||
"myst": as_myst_list, | ||
} |
File renamed without changes.
This file contains 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,82 @@ | ||
#! /usr/bin/env python | ||
""" | ||
Example usage: | ||
```bash | ||
snscrape http://csdms.colorado.edu/wiki/CSN_Quantity_Templates \ | ||
http://csdms.colorado.edu/wiki/CSN_Object_Templates \ | ||
http://csdms.colorado.edu/wiki/CSN_Operation_Templates \ | ||
> data/scraped.yaml | ||
``` | ||
""" | ||
from __future__ import annotations | ||
|
||
from collections.abc import Iterable | ||
from urllib.request import urlopen | ||
|
||
from standard_names.registry import NamesRegistry | ||
|
||
|
||
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. | ||
""" | ||
registry = NamesRegistry([]) | ||
for file in files: | ||
registry |= NamesRegistry(search_file_for_names(file)) | ||
return registry | ||
|
||
|
||
def find_all_names(lines: Iterable[str], engine: str = "regex") -> set[str]: | ||
"""Find standard names. | ||
Examples | ||
-------- | ||
>>> from standard_names.cli._scrape import find_all_names | ||
>>> 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, or "wind__speed" are good. | ||
... ''' | ||
>>> sorted(find_all_names(contents.splitlines(), engine="regex")) | ||
['air__temperature', 'water__temperature', 'wind__speed'] | ||
>>> sorted(find_all_names(contents.splitlines(), engine="peg")) | ||
['air__temperature', 'water__temperature', 'wind__speed'] | ||
""" | ||
if engine == "regex": | ||
from standard_names.regex import findall | ||
elif engine == "peg": | ||
from standard_names.peg import findall | ||
else: | ||
raise ValueError( | ||
"engine not understood: {engine!r} is not one of 'regex', 'peg'" | ||
) | ||
|
||
names = set() | ||
for line in lines: | ||
names |= set(findall(line.strip())) | ||
|
||
return names | ||
|
||
|
||
def search_file_for_names(path: str) -> set[str]: | ||
names = set() | ||
if path.startswith(("http://", "https://")): | ||
with urlopen(path) as response: | ||
names = find_all_names(line.decode("utf-8") for line in response) | ||
else: | ||
with open(path) as fp: | ||
names = find_all_names(fp) | ||
|
||
return names |
This file contains 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.