ncasuk / amf-check-writer

Library to write AMF compliance checks
BSD 3-Clause "New" or "Revised" License
0 stars 4 forks source link

Integer global attr check fails due to regex check against a non-string type #56

Closed agstephens closed 3 years ago

agstephens commented 3 years ago

The spreadsheets define a possible type as: "Integer"

However, the regex checks expect everything to be a string. This causes a warning when running the test because the check tries to perform a regular expression match against a non-string type.

The simplest fix is just to patch compliance-check-lib so that it will convert all global attrs to strings if doing a regex check:

(amf)$ cd compliance-check-lib/
(amf)$ git diff
diff --git a/checklib/code/nc_util.py b/checklib/code/nc_util.py
index 66553b2..41672e6 100644
--- a/checklib/code/nc_util.py
+++ b/checklib/code/nc_util.py
@@ -77,7 +77,9 @@ def check_global_attr_against_regex(ds, attr, regex):
     """
     if attr not in ds.ncattrs():
         return 0
-    if not re.match("^{}$".format(regex), getattr(ds, attr), re.DOTALL):
+
+    # Always coerce the attribute to a string to do the regex check
+    if not re.match("^{}$".format(regex), str(getattr(ds, attr)), re.DOTALL):
         return 1
     # Success
     return 2

NOTE: The test code for seeing if you get a warning is:

export DATA_DIR=check-data-2021-09-15
VERSION=v2.0
export PYESSV_ARCHIVE_HOME=$DATA_DIR/$VERSION/pyessv-vocabs
TEST_FILE=../NCAS-Data-Project-Training-Data/Data/ncas-anemometer-1_ral_29001225_mean-winds_v0.1.nc
amf-checker --yaml-dir $DATA_DIR/$VERSION/checks $TEST_FILE --version $VERSION
agstephens commented 3 years ago

Fix this in a PR in the compliance-check-lib.

agstephens commented 3 years ago

Fixed.