-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix invalid standard names, add standard name checking script and git…
…hub action (#52) This PR introduces a Github Action to automatically check the validity of Standard Names against the list of criteria specified by the CF conventions: Must only contain lowercase letters, numerals, or underscores, and the first character must be a letter. This is achieved with a new script (tools/check_name_rules.py) that parses the given standard names xml file, and checks each name against these rules, returning an error if any invalid names are found. This action will run on all branches for any new commits to ensure any new names follow these rules. This new script revealed several existing invalid names; those have been corrected as part of this PR.
- Loading branch information
Showing
6 changed files
with
140 additions
and
35 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,79 @@ | ||
#!/usr/bin/env python3 | ||
|
||
""" | ||
Check standard names database file for violations of standard name character rules | ||
""" | ||
|
||
import argparse | ||
import sys | ||
import os.path | ||
import re | ||
|
||
################################################ | ||
#Add CCPP framework (lib) modules to python path | ||
################################################ | ||
|
||
_CURR_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
sys.path.append(os.path.join(_CURR_DIR, "lib")) | ||
|
||
####################################### | ||
#Import needed framework python modules | ||
####################################### | ||
|
||
from xml_tools import find_schema_file, find_schema_version, validate_xml_file, read_xml_file | ||
|
||
def main(): | ||
"""Parse the standard names database file and output a dictionary | ||
where the keys are any standard names in violation of character rules, | ||
and the values are lists of the specific rules violated | ||
""" | ||
#Parse arguments | ||
parser = argparse.ArgumentParser(description=__doc__) | ||
|
||
parser.add_argument("-s","--standard_name_file", | ||
metavar='<standard names filename>',required=True, | ||
type=str, help="XML file with standard name library") | ||
args = parser.parse_args() | ||
|
||
stdname_file = os.path.abspath(args.standard_name_file) | ||
tree, root = read_xml_file(stdname_file) | ||
|
||
# Validate the XML file | ||
version = find_schema_version(root) | ||
schema_name = os.path.basename(stdname_file)[0:-4] | ||
schema_root = os.path.dirname(stdname_file) | ||
schema_path = os.path.join(schema_root,schema_name) | ||
schema_file = find_schema_file(schema_path, version) | ||
if schema_file: | ||
try: | ||
validate_xml_file(stdname_file, schema_name, version, None, | ||
schema_path=schema_root, error_on_noxmllint=True) | ||
except ValueError: | ||
raise ValueError(f"Invalid standard names file, {stdname_file}") | ||
else: | ||
raise ValueError(f'Cannot find schema file, {schema_name}, for {version=}') | ||
|
||
#Parse list of standard names and see if any names violate one or more rules | ||
violators = {} | ||
legal_first_char = re.compile('[a-z]') | ||
valid_chars = re.compile('[a-z0-9_]') | ||
for name in root.findall('./section/standard_name'): | ||
sname = name.attrib['name'] | ||
violations = [] | ||
if legal_first_char.sub('', sname[0]): | ||
violations.append('First character is not a lowercase letter') | ||
testchars = valid_chars.sub('', sname) | ||
if testchars: | ||
violations.append(f'Invalid characters are present: "{testchars}"') | ||
|
||
# If any violations were detected, add an entry to "violators" dictionary | ||
if violations: | ||
violators[sname] = violations | ||
|
||
if violators: | ||
raise Exception(f"Violating standard names found:\n{violators}") | ||
else: | ||
print(f'Success! All standard names in {args.standard_name_file} follow the rules.') | ||
|
||
if __name__ == "__main__": | ||
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
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