Skip to content

Commit 5817063

Browse files
committed
Ensure a tsv file is only written if no errors are present
1 parent 666e267 commit 5817063

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

yml2block/__main__.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ def __iter__(self):
5757
for filename, violations in self.violations.items():
5858
yield (filename, violations, min(violations, key=lambda x: x.level).level)
5959

60+
def safe_conversion_possible(self, file_path, strict=False):
61+
"""Check if the file can be safely converted to tsv.
62+
"""
63+
try:
64+
max_severity = min(self.violations[file_path], key=lambda x: x.level).level
65+
if max_severity == Level.ERROR:
66+
return False
67+
elif max_severity == Level.WARNING:
68+
if strict:
69+
return False
70+
else:
71+
return True
72+
else:
73+
return True
74+
except KeyError:
75+
print(f"The file {file_path} is not present in this list of files.")
76+
raise
77+
6078

6179
def guess_input_type(input_path):
6280
"""Guess the input type from the file name."""
@@ -244,8 +262,10 @@ def convert(file_path, warn, skip, warn_ec, verbose, outfile):
244262

245263
lint_violations.extend_for(file_path, file_lint_violations)
246264

247-
if input_type == "yaml" and file_path not in lint_violations:
265+
if input_type == "yaml" and lint_violations.safe_conversion_possible(file_path):
248266
output.write_metadata_block(data, outfile, longest_row, verbose)
267+
else:
268+
print("Errors detected. No TSV file was written.")
249269

250270
return_violations(lint_violations, warn_ec, verbose)
251271

0 commit comments

Comments
 (0)