Skip to content

Commit

Permalink
Add function to check dimensions against regex
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-hampton committed Apr 10, 2024
1 parent e5170b2 commit 68cb8d5
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion checksit/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def check_var_exists(dct, variables, skip_spellcheck=False):

def check_dim_exists(dct, dimensions, skip_spellcheck=False):
"""
Check that variables exist
Check that dimensions exist
E.g. check-dim-exists:dimensions:time|latitude
"""
Expand All @@ -185,6 +185,35 @@ def check_dim_exists(dct, dimensions, skip_spellcheck=False):
return errors, warnings


def check_dim_regex(dct, regex_dims, skip_spellcheck=False):
"""
Check dimension exists matching regex
E.g. check-dime-regex:regex-dims:^string_length[^,]*$
"""
errors = []
warnings = []
for regex_dim in regex_dims:
if regex_dim.endswith(":__OPTIONAL__"):
regex_dim = ":".join(regex_dim.split(":")[:-1])
r = re.compile(regex_dim)
matches = list(filter(r.match, dct["dimensions"].keys()))
#if not re.match(regex_dim, dct["dimensions"].keys()):
if len(matches) == 0:
warnings.append(
f"[dimension**************:{regex_dim}]: No dimension matching optional regex check in file. "
)
else:
r = re.compile(regex_dim)
matches = list(filter(r.match, dct["dimensions"].keys()))
#if not re.match(regex_dim, dct["dimensions"].keys()):
if len(matches) == 0:
errors.append(
f"[dimension**************:{regex_dim}]: No dimension matching regex check in file. "
)
return errors, warnings


def check_var(dct, variable, defined_attrs, rules_attrs=None, skip_spellcheck=False):
"""
Check variable exists and has attributes defined.
Expand Down

0 comments on commit 68cb8d5

Please sign in to comment.