Skip to content

Commit

Permalink
Added cli flag to skip spellchecking
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-hampton committed Apr 19, 2023
1 parent 5cdcc9d commit 51f7f8e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 28 deletions.
15 changes: 10 additions & 5 deletions checksit/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
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", ignore_warnings=False):
auto_cache=False, verbose=False, log_mode="standard", ignore_warnings=False,
skip_spellcheck=False):
self.template = template
self.mappings = mappings or {}
self.extra_rules = extra_rules or {}
Expand All @@ -30,6 +31,7 @@ def __init__(self, template="auto", mappings=None, extra_rules=None, specs=None,
self.ignore_warnings = ignore_warnings
self.verbose = verbose
self.log_mode = log_mode
self.skip_spellcheck = skip_spellcheck
self._check_context = {}

def _update_check_context(self, file_path, template):
Expand Down Expand Up @@ -124,7 +126,8 @@ 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_warnings=False):
ignore_attrs=None, log_mode="standard", fmt_errors=None,
ignore_warnings=False, skip_spellcheck=False):

if hasattr(file_content, "to_dict"):
record = file_content.to_dict()
Expand All @@ -144,7 +147,7 @@ def _check_file(self, file_content, template, mappings=None, extra_rules=None, s

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

Expand Down Expand Up @@ -186,7 +189,8 @@ def _check_file(self, file_content, template, mappings=None, extra_rules=None, s


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_warnings=False):
ignore_attrs=None, auto_cache=False, verbose=False, log_mode="standard",
ignore_warnings=False, skip_spellcheck=False):

try:
fp = FileParser()
Expand Down Expand Up @@ -250,7 +254,8 @@ 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, ignore_warnings=ignore_warnings)
specs=specs, ignore_attrs=ignore_attrs, log_mode=log_mode,
ignore_warnings=ignore_warnings, skip_spellcheck=skip_spellcheck)


class TemplateManager:
Expand Down
6 changes: 4 additions & 2 deletions checksit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ def main():
@click.option("-v", "--verbose/--no-verbose", default=False)
@click.option("-t", "--template", default="auto")
@click.option("-w", "--ignore-warnings", is_flag=True)
@click.option("-p", "--skip-spellcheck", 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", ignore_warnings=False):
auto_cache=False, log_mode="standard", verbose=False, template="auto", ignore_warnings=False,
skip_spellcheck=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 @@ -56,7 +58,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, ignore_warnings=ignore_warnings)
log_mode=log_mode, ignore_warnings=ignore_warnings, skip_spellcheck=skip_spellcheck)


@main.command()
Expand Down
36 changes: 18 additions & 18 deletions checksit/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def search_close_match(search_for, search_in):
return ""


def check_var_attrs(dct, defined_attrs, ignore_bounds=True):
def check_var_attrs(dct, defined_attrs, ignore_bounds=True, skip_spellcheck=False):
"""
Check that variable attributes are defined.
Expand All @@ -61,7 +61,7 @@ def check_var_attrs(dct, defined_attrs, ignore_bounds=True):
return errors, warnings


def check_global_attrs(dct, defined_attrs=None, vocab_attrs=None, regex_attrs=None, rules_attrs=None):
def check_global_attrs(dct, defined_attrs=None, vocab_attrs=None, regex_attrs=None, rules_attrs=None, skip_spellcheck=False):
"""
Check that required global attributes are correct.
Expand All @@ -78,21 +78,21 @@ def check_global_attrs(dct, defined_attrs=None, vocab_attrs=None, regex_attrs=No

for attr in defined_attrs:
if attr not in dct['global_attributes']:
errors.append(f"[global-attributes:**************:{attr}]: Attribute '{attr}' does not exist. {search_close_match(attr, dct['global_attributes'].keys())}")
errors.append(f"[global-attributes:**************:{attr}]: Attribute '{attr}' does not exist. {search_close_match(attr, dct['global_attributes'].keys()) if not skip_spellcheck else ''}")
elif is_undefined(dct['global_attributes'].get(attr)):
errors.append(f"[global-attributes:**************:{attr}]: No value defined for attribute '{attr}'.")

for attr in vocab_attrs:
if attr not in dct['global_attributes']:
errors.append(f"[global-attributes:**************:{attr}]: Attribute '{attr}' does not exist. {search_close_match(attr, dct['global_attributes'].keys())}")
errors.append(f"[global-attributes:**************:{attr}]: Attribute '{attr}' does not exist. {search_close_match(attr, dct['global_attributes'].keys()) if not skip_spellcheck else ''}")
elif is_undefined(dct['global_attributes'].get(attr)):
errors.append(f"[global-attributes:**************:{attr}]: No value defined for attribute '{attr}'.")
else:
errors.extend(vocabs.check(vocab_attrs[attr], dct["global_attributes"].get(attr), label=f"[global-attributes:******:{attr}]***"))

for attr in regex_attrs:
if attr not in dct['global_attributes']:
errors.append(f"[global-attributes:**************:{attr}]: Attribute '{attr}' does not exist. {search_close_match(attr, dct['global_attributes'].keys())}")
errors.append(f"[global-attributes:**************:{attr}]: Attribute '{attr}' does not exist. {search_close_match(attr, dct['global_attributes'].keys()) if not skip_spellcheck else ''}")
elif is_undefined(dct['global_attributes'].get(attr)):
errors.append(f"[global-attributes:**************:{attr}]: No value defined for attribute '{attr}'.")
elif not re.match(regex_attrs[attr], dct['global_attributes'].get(attr)):
Expand All @@ -101,7 +101,7 @@ def check_global_attrs(dct, defined_attrs=None, vocab_attrs=None, regex_attrs=No
for attr in rules_attrs:
#errors.extend(rules.check(rules_attrs[attr], dct['global_attributes'].get(attr, UNDEFINED), label=f"[global-attributes:******:{attr}]***"))
if attr not in dct['global_attributes']:
errors.append(f"[global-attributes:**************:{attr}]: Attribute '{attr}' does not exist. {search_close_match(attr, dct['global_attributes'].keys())}")
errors.append(f"[global-attributes:**************:{attr}]: Attribute '{attr}' does not exist. {search_close_match(attr, dct['global_attributes'].keys()) if not skip_spellcheck else ''}")
elif is_undefined(dct['global_attributes'].get(attr)):
errors.append(f"[global-attributes:**************:{attr}]: No value defined for attribute '{attr}'.")
else:
Expand All @@ -111,7 +111,7 @@ def check_global_attrs(dct, defined_attrs=None, vocab_attrs=None, regex_attrs=No
return errors, warnings


def check_var_exists(dct, variables):
def check_var_exists(dct, variables, skip_spellcheck=False):
"""
Check that variables exist
Expand All @@ -124,15 +124,15 @@ def check_var_exists(dct, variables):
if ':__OPTIONAL__' in var:
var = var.split(':')[0]
if var not in dct["variables"].keys():
warnings.append(f"[variable**************:{var}]: Optional variable does not exist in file. {search_close_match(var, dct['variables'].keys())}")
warnings.append(f"[variable**************:{var}]: Optional variable does not exist in file. {search_close_match(var, dct['variables'].keys()) if not skip_spellcheck else ''}")
else:
if var not in dct["variables"].keys():
errors.append(f"[variable**************:{var}]: Does not exist in file. {search_close_match(var, dct['variables'].keys())}")
errors.append(f"[variable**************:{var}]: Does not exist in file. {search_close_match(var, dct['variables'].keys()) if not skip_spellcheck else ''}")

return errors, warnings


def check_dim_exists(dct, dimensions):
def check_dim_exists(dct, dimensions, skip_spellcheck=False):
"""
Check that variables exist
Expand All @@ -145,15 +145,15 @@ def check_dim_exists(dct, dimensions):
if ':__OPTIONAL__' in dim:
dim = dim.split(':')[0]
if dim not in dct["dimensions"].keys():
warnings.append(f"[dimension**************:{dim}]: Optional dimension does not exist in file. {search_close_match(dim, dct['dimensions'].keys())}")
warnings.append(f"[dimension**************:{dim}]: Optional dimension does not exist in file. {search_close_match(dim, dct['dimensions'].keys()) if not skip_spellcheck else ''}")
else:
if dim not in dct["dimensions"].keys():
errors.append(f"[dimension**************:{dim}]: Does not exist in file. {search_close_match(dim, dct['dimensions'].keys())}")
errors.append(f"[dimension**************:{dim}]: Does not exist in file. {search_close_match(dim, dct['dimensions'].keys()) if not skip_spellcheck else ''}")

return errors, warnings


def check_var(dct, variable, defined_attrs):
def check_var(dct, variable, defined_attrs, skip_spellcheck=False):
"""
Check variable exists and has attributes defined.
"""
Expand All @@ -165,7 +165,7 @@ def check_var(dct, variable, defined_attrs):
if ':__OPTIONAL__' in variable:
variable = variable.split(':')[0]
if variable not in dct["variables"].keys():
warnings.append(f"[variable**************:{variable}]: Optional variable does not exist in file. {search_close_match(variable, dct['variables'].keys())}")
warnings.append(f"[variable**************:{variable}]: Optional variable does not exist in file. {search_close_match(variable, dct['variables'].keys()) if not skip_spellcheck else ''}")
else:
for attr in defined_attrs:
if isinstance(attr, dict) and len(attr.keys()) == 1:
Expand All @@ -174,7 +174,7 @@ def check_var(dct, variable, defined_attrs):
attr_key = attr.split(':')[0]
attr_value = ':'.join(attr.split(':')[1:])
if attr_key not in dct["variables"][variable]:
errors.append(f"[variable**************:{variable}]: Attribute '{attr_key}' does not exist. {search_close_match(attr_key, dct['variables'][variable])}")
errors.append(f"[variable**************:{variable}]: Attribute '{attr_key}' does not exist. {search_close_match(attr_key, dct['variables'][variable]) if not skip_spellcheck else ''}")
elif '<derived from file>' in attr_value:
# work this out
pass
Expand All @@ -183,21 +183,21 @@ def check_var(dct, variable, defined_attrs):
attr_value = [ int(i.strip('b')) for i in attr_value.split(',') ]
attr_value = np.array(attr_value, dtype=np.int8)
if not np.all(dct["variables"][variable].get(attr_key) == attr_value):
errors.append(f"[variable**************:{variable}]: Attribute '{attr_key}' must have definition {attr_value}, not {dct['variables'][variable].get(attr_key)}.")
errors.append(f"[variable**************:{variable}]: Attribute '{attr_key}' must have definition {attr_value}, not {dct['variables'][variable].get(attr_key) if skip_spellcheck else ''}.")
#elif attr_key == 'flag_meanings':
# print(attr_value)
# print(dct["variables"][variable].get(attr_key))
elif not str(dct["variables"][variable].get(attr_key)) == attr_value:
errors.append(f"[variable**************:{variable}]: Attribute '{attr_key}' must have definition {attr_value}, not {dct['variables'][variable].get(attr_key).encode('unicode_escape').decode('utf-8')}.")
else:
if variable not in dct["variables"].keys():
errors.append(f"[variable**************:{variable}]: Optional variable does not exist in file. {search_close_match(variable, dct['variables'].keys())}")
errors.append(f"[variable**************:{variable}]: Optional variable does not exist in file. {search_close_match(variable, dct['variables'].keys()) if not skip_spellcheck else ''}")
else:
for attr in defined_attrs:
attr_key = attr.split(':')[0]
attr_value = ':'.join(attr.split(':')[1:])
if attr_key not in dct["variables"][variable]:
errors.append(f"[variable**************:{variable}]: Attribute '{attr_key}' does not exist. {search_close_match(attr_key, dct['variables'][variable])}")
errors.append(f"[variable**************:{variable}]: Attribute '{attr_key}' does not exist. {search_close_match(attr_key, dct['variables'][variable]) if not skip_spellcheck else ''}")
elif '<' in attr_value:
# work this out
pass
Expand Down
9 changes: 6 additions & 3 deletions checksit/specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,25 @@ def _setup(self, spec_id):
self.spec_id = spec_id
self.spec = load_specs([spec_id])[spec_id]

def _run_check(self, record, check_dict):
def _run_check(self, record, check_dict, skip_spellcheck=False):
d = check_dict
parts = d["func"].split(".")

mod_path, func = ".".join(parts[:-1]), parts[-1]
func = getattr(importlib.import_module(mod_path), func)

params = d["params"]
params["skip_spellcheck"] = skip_spellcheck
return func(record, **params)

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

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

Expand Down

0 comments on commit 51f7f8e

Please sign in to comment.