Skip to content

Commit 025d719

Browse files
committed
Parametrized error level
1 parent d8cd738 commit 025d719

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

yml2block/rules.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def __str__(self):
101101
return self.__repr__()
102102

103103

104-
def unique_names(yaml_chunk, tsv_keyword):
104+
def unique_names(yaml_chunk, tsv_keyword, level=Level.ERROR):
105105
"""Make sure that each name in the block is only used once.
106106
107107
block content level lint
@@ -116,15 +116,15 @@ def unique_names(yaml_chunk, tsv_keyword):
116116
if count > 1:
117117
errors.append(
118118
LintViolation(
119-
Level.ERROR,
119+
level,
120120
"unique_names",
121121
f"Name '{name}' occurs {count} times. Names have to be unique.",
122122
)
123123
)
124124
return errors
125125

126126

127-
def block_content_is_list(yaml_chunk):
127+
def block_content_is_list(yaml_chunk, level=Level.ERROR):
128128
"""Make sure that the yaml chunk is a list.
129129
130130
block content level lint
@@ -134,14 +134,14 @@ def block_content_is_list(yaml_chunk):
134134
else:
135135
return [
136136
LintViolation(
137-
Level.ERROR,
137+
level,
138138
"block_content_is_list",
139139
"Entry is not a list",
140140
)
141141
]
142142

143143

144-
def top_level_keywords_valid(keywords):
144+
def top_level_keywords_valid(keywords, level=Level.ERROR):
145145
"""Ensure top-level keywords are spelled correctly and no additonal ones are present.
146146
147147
top-level keyword level lint
@@ -154,14 +154,14 @@ def top_level_keywords_valid(keywords):
154154
else:
155155
return [
156156
LintViolation(
157-
Level.ERROR,
157+
level,
158158
"top_level_keywords_valid",
159159
f"Keyword list '{keywords}' differs from '{PERMISSIBLE_KEYWORDS}' or '{REQUIRED_TOP_LEVEL_KEYWORDS}'",
160160
)
161161
]
162162

163163

164-
def top_level_keywords_unique(keywords):
164+
def top_level_keywords_unique(keywords, level=Level.ERROR):
165165
"""Make sure no keyword is specified twice.
166166
167167
NOTE: This is most likely also enforced by the YAML parser,
@@ -175,14 +175,14 @@ def top_level_keywords_unique(keywords):
175175
else:
176176
return [
177177
LintViolation(
178-
Level.ERROR,
178+
level,
179179
"top_level_keywords_unique",
180180
f"Keyword list '{keywords}' contains duplicate keys.",
181181
)
182182
]
183183

184184

185-
def required_keys_present(list_item, tsv_keyword):
185+
def required_keys_present(list_item, tsv_keyword, level=Level.ERROR):
186186
"""Make sure the keywords required for the current top-level item are present.
187187
188188
second-level list entry lint
@@ -193,7 +193,7 @@ def required_keys_present(list_item, tsv_keyword):
193193
except KeyError:
194194
return [
195195
LintViolation(
196-
Level.ERROR,
196+
level,
197197
"required_keys_present",
198198
f"Cannot check entry for invalid keyword '{tsv_keyword}'. Skipping entry.",
199199
)
@@ -204,14 +204,14 @@ def required_keys_present(list_item, tsv_keyword):
204204
else:
205205
return [
206206
LintViolation(
207-
Level.ERROR,
207+
level,
208208
"required_keys_present",
209209
f"List of required keys '{required}' and found keys '{found_keys}' differ.",
210210
)
211211
]
212212

213213

214-
def no_invalid_keys_present(list_item, tsv_keyword):
214+
def no_invalid_keys_present(list_item, tsv_keyword, level=Level.ERROR):
215215
"""Make sure no invalid keys are present.
216216
217217
second-level list entry lint
@@ -221,7 +221,7 @@ def no_invalid_keys_present(list_item, tsv_keyword):
221221
except KeyError:
222222
return [
223223
LintViolation(
224-
Level.ERROR,
224+
level,
225225
"no_invalid_keys_present",
226226
f"Cannot check entry for invalid keyword '{tsv_keyword}'. Skipping entry.",
227227
)
@@ -232,15 +232,15 @@ def no_invalid_keys_present(list_item, tsv_keyword):
232232
if key not in permissible:
233233
violations.append(
234234
LintViolation(
235-
Level.ERROR,
235+
level,
236236
"no_invalid_keys_present",
237237
f"Invalid key {key} present for {list_item} in block {tsv_keyword}.",
238238
)
239239
)
240240
return violations
241241

242242

243-
def no_substructures_present(list_item, tsv_keyword):
243+
def no_substructures_present(list_item, tsv_keyword, level=Level.ERROR):
244244
"""Make sure list items do not contain dicts, tuples lists etc.
245245
246246
second-level list entry lint
@@ -250,15 +250,15 @@ def no_substructures_present(list_item, tsv_keyword):
250250
if type(value) in (dict, tuple, list):
251251
violations.append(
252252
LintViolation(
253-
Level.ERROR,
253+
level,
254254
"no_substructures_present",
255255
f"Key {key} in block {tsv_keyword} has a subtructure of type {type(value)}. Only strings, booleans, an numericals are allowed here.",
256256
)
257257
)
258258
return violations
259259

260260

261-
def no_trailing_white_spaces(list_item, tsv_keyword):
261+
def no_trailing_white_spaces(list_item, tsv_keyword, level=Level.ERROR):
262262
"""Make sure the entries do not contain trailing white spaces.
263263
264264
second-level list entry lint
@@ -294,7 +294,7 @@ def no_trailing_white_spaces(list_item, tsv_keyword):
294294
# Regex matches one or more spaces at the end of strings
295295
violations.append(
296296
LintViolation(
297-
Level.ERROR,
297+
level,
298298
"no_trailing_white_spaces",
299299
f"The entry '{value}' has one or more trailing spaces.",
300300
)

0 commit comments

Comments
 (0)