Skip to content

Commit

Permalink
Parametrized error level
Browse files Browse the repository at this point in the history
  • Loading branch information
HenningTimm committed Dec 19, 2023
1 parent d8cd738 commit 025d719
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions yml2block/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def __str__(self):
return self.__repr__()


def unique_names(yaml_chunk, tsv_keyword):
def unique_names(yaml_chunk, tsv_keyword, level=Level.ERROR):
"""Make sure that each name in the block is only used once.
block content level lint
Expand All @@ -116,15 +116,15 @@ def unique_names(yaml_chunk, tsv_keyword):
if count > 1:
errors.append(
LintViolation(
Level.ERROR,
level,
"unique_names",
f"Name '{name}' occurs {count} times. Names have to be unique.",
)
)
return errors


def block_content_is_list(yaml_chunk):
def block_content_is_list(yaml_chunk, level=Level.ERROR):
"""Make sure that the yaml chunk is a list.
block content level lint
Expand All @@ -134,14 +134,14 @@ def block_content_is_list(yaml_chunk):
else:
return [
LintViolation(
Level.ERROR,
level,
"block_content_is_list",
"Entry is not a list",
)
]


def top_level_keywords_valid(keywords):
def top_level_keywords_valid(keywords, level=Level.ERROR):
"""Ensure top-level keywords are spelled correctly and no additonal ones are present.
top-level keyword level lint
Expand All @@ -154,14 +154,14 @@ def top_level_keywords_valid(keywords):
else:
return [
LintViolation(
Level.ERROR,
level,
"top_level_keywords_valid",
f"Keyword list '{keywords}' differs from '{PERMISSIBLE_KEYWORDS}' or '{REQUIRED_TOP_LEVEL_KEYWORDS}'",
)
]


def top_level_keywords_unique(keywords):
def top_level_keywords_unique(keywords, level=Level.ERROR):
"""Make sure no keyword is specified twice.
NOTE: This is most likely also enforced by the YAML parser,
Expand All @@ -175,14 +175,14 @@ def top_level_keywords_unique(keywords):
else:
return [
LintViolation(
Level.ERROR,
level,
"top_level_keywords_unique",
f"Keyword list '{keywords}' contains duplicate keys.",
)
]


def required_keys_present(list_item, tsv_keyword):
def required_keys_present(list_item, tsv_keyword, level=Level.ERROR):
"""Make sure the keywords required for the current top-level item are present.
second-level list entry lint
Expand All @@ -193,7 +193,7 @@ def required_keys_present(list_item, tsv_keyword):
except KeyError:
return [
LintViolation(
Level.ERROR,
level,
"required_keys_present",
f"Cannot check entry for invalid keyword '{tsv_keyword}'. Skipping entry.",
)
Expand All @@ -204,14 +204,14 @@ def required_keys_present(list_item, tsv_keyword):
else:
return [
LintViolation(
Level.ERROR,
level,
"required_keys_present",
f"List of required keys '{required}' and found keys '{found_keys}' differ.",
)
]


def no_invalid_keys_present(list_item, tsv_keyword):
def no_invalid_keys_present(list_item, tsv_keyword, level=Level.ERROR):
"""Make sure no invalid keys are present.
second-level list entry lint
Expand All @@ -221,7 +221,7 @@ def no_invalid_keys_present(list_item, tsv_keyword):
except KeyError:
return [
LintViolation(
Level.ERROR,
level,
"no_invalid_keys_present",
f"Cannot check entry for invalid keyword '{tsv_keyword}'. Skipping entry.",
)
Expand All @@ -232,15 +232,15 @@ def no_invalid_keys_present(list_item, tsv_keyword):
if key not in permissible:
violations.append(
LintViolation(
Level.ERROR,
level,
"no_invalid_keys_present",
f"Invalid key {key} present for {list_item} in block {tsv_keyword}.",
)
)
return violations


def no_substructures_present(list_item, tsv_keyword):
def no_substructures_present(list_item, tsv_keyword, level=Level.ERROR):
"""Make sure list items do not contain dicts, tuples lists etc.
second-level list entry lint
Expand All @@ -250,15 +250,15 @@ def no_substructures_present(list_item, tsv_keyword):
if type(value) in (dict, tuple, list):
violations.append(
LintViolation(
Level.ERROR,
level,
"no_substructures_present",
f"Key {key} in block {tsv_keyword} has a subtructure of type {type(value)}. Only strings, booleans, an numericals are allowed here.",
)
)
return violations


def no_trailing_white_spaces(list_item, tsv_keyword):
def no_trailing_white_spaces(list_item, tsv_keyword, level=Level.ERROR):
"""Make sure the entries do not contain trailing white spaces.
second-level list entry lint
Expand Down Expand Up @@ -294,7 +294,7 @@ def no_trailing_white_spaces(list_item, tsv_keyword):
# Regex matches one or more spaces at the end of strings
violations.append(
LintViolation(
Level.ERROR,
level,
"no_trailing_white_spaces",
f"The entry '{value}' has one or more trailing spaces.",
)
Expand Down

0 comments on commit 025d719

Please sign in to comment.