Skip to content

Commit

Permalink
Added warnings on missing optional things, and a flag to ignore the w…
Browse files Browse the repository at this point in the history
…arnings
  • Loading branch information
joshua-hampton committed Dec 7, 2022
1 parent c4fe779 commit 31a0209
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
25 changes: 20 additions & 5 deletions checksit/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
class Checker:

def __init__(self, template="auto", mappings=None, extra_rules=None, specs=None, ignore_attrs=None,
auto_cache=False, verbose=False, log_mode="standard"):
auto_cache=False, verbose=False, log_mode="standard", ignore_warnings=False):
self.template = template
self.mappings = mappings or {}
self.extra_rules = extra_rules or {}
self.specs = specs or []
self.ignore_attrs = ignore_attrs or []
self.auto_cache = auto_cache
self.ignore_warnings = ignore_warnings
self.verbose = verbose
self.log_mode = log_mode
self._check_context = {}
Expand Down Expand Up @@ -123,7 +124,7 @@ def _compare_dicts(self, record, template, label, mappings=None, ignore_attrs=No
return errors

def _check_file(self, file_content, template, mappings=None, extra_rules=None, specs=None,
ignore_attrs=None, log_mode="standard", fmt_errors=None):
ignore_attrs=None, log_mode="standard", fmt_errors=None, ignore_warnings=False):

if hasattr(file_content, "to_dict"):
record = file_content.to_dict()
Expand All @@ -136,13 +137,16 @@ def _check_file(self, file_content, template, mappings=None, extra_rules=None, s

# Create a container for collecting errors
errors = getattr(file_content, "fmt_errors", [])
warnings = []

# Use check specifications if requested
specs = specs or self.specs

for spec in specs:
sr = SpecificationChecker(spec)
errors.extend(sr.run_checks(record))
spec_errors, spec_warnings = sr.run_checks(record)
errors.extend(spec_errors)
warnings.extend(spec_warnings)

if template == "off" and log_mode == "standard":
print("[WARNING] Template checks switched off!")
Expand All @@ -166,11 +170,22 @@ def _check_file(self, file_content, template, mappings=None, extra_rules=None, s
for i, error in enumerate(errors):
count = i + 1
print(f"\t{count:02d}. {error}")
compliant = False
else:
compliant = True

if warnings and not ignore_warnings:
print(f"\n[WARNING] {len(warnings)} warnings about file:\n")
for i, warning in enumerate(warnings):
count = i + 1
print(f"\t{count:02d}. {warning}")

if compliant:
print("[INFO] File is compliant!")


def check_file(self, file_path, template="auto", mappings=None, extra_rules=None, specs=None,
ignore_attrs=None, auto_cache=False, verbose=False, log_mode="standard"):
ignore_attrs=None, auto_cache=False, verbose=False, log_mode="standard", ignore_warnings=False):

try:
fp = FileParser()
Expand Down Expand Up @@ -234,7 +249,7 @@ def check_file(self, file_path, template="auto", mappings=None, extra_rules=None
print(f"\nRunning with:\n\tTemplate: {tmpl_input}\n\tSpec Files: {specs}\n\tDatafile: {file_content.inpt}")

self._check_file(file_content, template=tmpl, mappings=mappings, extra_rules=extra_rules,
specs=specs, ignore_attrs=ignore_attrs, log_mode=log_mode)
specs=specs, ignore_attrs=ignore_attrs, log_mode=log_mode, ignore_warnings=ignore_warnings)


class TemplateManager:
Expand Down
5 changes: 3 additions & 2 deletions checksit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ def main():
@click.option("-l", "--log-mode", default="standard")
@click.option("-v", "--verbose/--no-verbose", default=False)
@click.option("-t", "--template", default="auto")
@click.option("-w", "--ignore-warnings", is_flag=True)
def check(file_path, mappings=None, rules=None, specs=None, ignore_attrs=None, ignore_all_globals=False,
ignore_all_dimensions=False, ignore_all_variables=False, ignore_all_variable_attrs=False,
auto_cache=False, log_mode="standard", verbose=False, template="auto"):
auto_cache=False, log_mode="standard", verbose=False, template="auto", ignore_warnings=False):

if ignore_all_globals or ignore_all_dimensions or ignore_all_variables or ignore_all_variable_attrs:
raise Exception("Options not implemented yet!!!!!")
Expand All @@ -55,7 +56,7 @@ def check(file_path, mappings=None, rules=None, specs=None, ignore_attrs=None, i
return check_file(file_path, template=template, mappings=mappings, extra_rules=rules,
specs=specs, ignore_attrs=ignore_attrs,
auto_cache=auto_cache, verbose=verbose,
log_mode=log_mode)
log_mode=log_mode, ignore_warnings=ignore_warnings)


@main.command()
Expand Down
26 changes: 15 additions & 11 deletions checksit/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def check_var_attrs(dct, defined_attrs, ignore_bounds=True):
E.g.: check-var-attrs:defined_attrs:long_name|units
"""
errors = []
warnings = []

bounds_vars = _get_bounds_var_ids(dct)

for var_id, var_dict in dct["variables"].items():
Expand All @@ -26,7 +28,7 @@ def check_var_attrs(dct, defined_attrs, ignore_bounds=True):
if is_undefined(var_dict.get(attr)):
errors.append(f"[variable**************:{var_id}]: Attribute '{attr}' must have a valid definition.")

return errors
return errors, warnings


def check_global_attrs(dct, defined_attrs=None, vocab_attrs=None, regex_attrs=None, rules_attrs=None):
Expand All @@ -42,6 +44,7 @@ def check_global_attrs(dct, defined_attrs=None, vocab_attrs=None, regex_attrs=No
rules_attrs = rules_attrs or {}

errors = []
warnings = []

for attr in defined_attrs:
if is_undefined(dct['global_attributes'].get(attr)):
Expand All @@ -58,7 +61,7 @@ def check_global_attrs(dct, defined_attrs=None, vocab_attrs=None, regex_attrs=No
errors.extend(rules.check(rules_attrs[attr], dct['global_attributes'].get(attr, UNDEFINED), label=f"[global-attributes:******:{attr}]***"))


return errors
return errors, warnings


def check_var_exists(dct, variables):
Expand All @@ -68,18 +71,18 @@ def check_var_exists(dct, variables):
E.g. check-var-exists:variables:time|altitude
"""
errors = []
warnings = []

for var in variables:
if ':__OPTIONAL__' in var:
var = var.split(':')[0]
if var not in dct["variables"].keys():
# add warning/advisory
pass
warnings.append(f"[variable**************:{var}]: Optional variable does not exist in file.")
else:
if var not in dct["variables"].keys():
errors.append(f"[variable**************:{var}]: Does not exist in file.")

return errors
return errors, warnings


def check_dim_exists(dct, dimensions):
Expand All @@ -89,31 +92,32 @@ def check_dim_exists(dct, dimensions):
E.g. check-dim-exists:dimensions:time|latitude
"""
errors = []
warnings = []

for dim in dimensions:
if ':__OPTIONAL__' in dim:
dim = dim.split(':')[0]
if dim not in dct["dimensions"].keys():
# add warning/advisory
pass
warnings.append(f"[dimension**************:{dim}]: Optional dimension does not exist in file.")
else:
if dim not in dct["dimensions"].keys():
errors.append(f"[dimension**************:{dim}]: Does not exist in file.")

return errors
return errors, warnings


def check_var(dct, variables, defined_attrs):
"""
Check variables exist and have attributes defined.
"""
errors = []
warnings = []

for var in variables:
if ':__OPTIONAL__' in var:
var = var.split(':')[0]
if var not in dct["variables"].keys():
# add warning/advisory
pass
warnings.append(f"[variable**************:{var}]: Optional variable does not exist in file.")
else:
for attr in defined_attrs:
if is_undefined(dct["variables"][var].get(attr)):
Expand All @@ -126,4 +130,4 @@ def check_var(dct, variables, defined_attrs):
if is_undefined(dct["variables"][var].get(attr)):
errors.append(f"[variable**************:{var}]: Attribute '{attr}' must have a valid definition.")

return errors
return errors, warnings
7 changes: 5 additions & 2 deletions checksit/specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ def _run_check(self, record, check_dict):

def run_checks(self, record):
errors = []
warnings = []

for check_id, check_dict in self.spec.items():
errors.extend(self._run_check(record, check_dict))
check_errors, check_warnings = self._run_check(record, check_dict)
errors.extend(check_errors)
warnings.extend(check_warnings)

return errors
return errors, warnings

0 comments on commit 31a0209

Please sign in to comment.