From 28f9892d91037009a55b3513ed125c4b907197bb Mon Sep 17 00:00:00 2001 From: "Michael Kavulich, Jr" Date: Mon, 6 Nov 2023 11:12:40 -0700 Subject: [PATCH] Address more review comments --- tools/check_name_rules.py | 15 +++++++-------- tools/check_xml_unique.py | 12 +++++------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/tools/check_name_rules.py b/tools/check_name_rules.py index 0862615..2a67336 100755 --- a/tools/check_name_rules.py +++ b/tools/check_name_rules.py @@ -1,13 +1,12 @@ #!/usr/bin/env python3 """ -Remove duplicates from a metadata standard-name XML library file. +Check standard names database file for violations of standard name character rules """ import argparse import sys import os.path -import copy import re ################################################ @@ -50,20 +49,20 @@ def main(): validate_xml_file(stdname_file, schema_name, version, None, schema_path=schema_root, error_on_noxmllint=True) except ValueError: - raise ValueError("Invalid standard names file, {}".format(stdname_file)) + raise ValueError(f"Invalid standard names file, {stdname_file}") else: - raise ValueError( - 'Cannot find schema file, {}, for version {}'.format(schema_name, version) - ) + 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 re.sub('[a-z]', '', sname[0]): + if legal_first_char.sub('', sname[0]): violations.append('First character is not a lowercase letter') - testchars = re.sub('[a-z0-9_]', '', sname) + testchars = valid_chars.sub('', sname) if testchars: violations.append(f'Invalid characters are present: "{testchars}"') diff --git a/tools/check_xml_unique.py b/tools/check_xml_unique.py index cbe301d..008de4f 100755 --- a/tools/check_xml_unique.py +++ b/tools/check_xml_unique.py @@ -59,11 +59,9 @@ def main_func(): validate_xml_file(stdname_file, schema_name, version, None, schema_path=schema_root, error_on_noxmllint=True) except ValueError: - raise ValueError("Invalid standard names file, {}".format(stdname_file)) + raise ValueError(f"Invalid standard names file, {stdname_file}") else: - raise ValueError( - 'Cannot find schema file, {}, for version {}'.format(schema_name, version) - ) + raise ValueError(f'Cannot find schema file, {schema_name}, for {version=}') #get list of all standard names all_std_names = [] @@ -87,8 +85,8 @@ def main_func(): print('The following duplicate standard names were found:') for dup in dup_std_names: rm_elements = root.findall('./section/standard_name[@name="%s"]'%dup)[1:] - print("{0}, ({1} duplicate(s))".format(dup, len(rm_elements))) - print('Removing duplicates and overwriting {}'.format(stdname_file)) + print("{dup}, ({len(rm_elements)} duplicate(s))") + print('Removing duplicates and overwriting {stdname_file}') for dup in dup_std_names: first_use = True #Logical that indicates the first use of the duplicated name rm_parents = root.findall('./section/standard_name[@name="%s"]..'%dup) @@ -110,7 +108,7 @@ def main_func(): print('The following duplicate standard names were found:') for dup in dup_std_names: rm_elements = root.findall('./section/standard_name[@name="%s"]'%dup)[1:] - print("{0}, ({1} duplicate(s))".format(dup, len(rm_elements))) + print("{dup}, ({len(rm_elements)} duplicate(s))") sys.exit(1) else: print('No duplicate standard names were found.')