Skip to content

Commit 398758e

Browse files
committed
Change TSValid reporting to logging rather than stdin
This makes tsvalid more flexible for use in other toolkit. Also, previously, we have printed a filename in the beginning of the output string, which makes no sense when the input is a stream. We only print a filename now if there is one. The output is thoroughly tested and works as expected.
1 parent a256937 commit 398758e

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

tsvalid/util.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Utilities for TSValid."""
22

3+
import logging
34
import os
45
import re
56
from typing import Any, Dict
@@ -90,7 +91,15 @@ def print_validation_error(context: Dict[str, Any]):
9091
message = context[KEY_ERROR_MESSAGE]
9192
if EXCEPTION_MESSAGE in context:
9293
message = f'{message} Exception: "{context[EXCEPTION_MESSAGE]}".'
93-
print(
94-
f"{context[KEY_FILENAME]}:{context[KEY_CURRENT_LINE]}:{context[KEY_COLUMN]}: "
94+
filename_reference = ""
95+
filename = context[KEY_FILENAME]
96+
if filename != "Unknown":
97+
filename_reference = f"{filename}:"
98+
full_message = (
99+
f"{filename_reference}{context[KEY_CURRENT_LINE]}:{context[KEY_COLUMN]}: "
95100
f"{context[KEY_ERROR_CODE]}: {message}"
96101
)
102+
if context[KEY_ERROR_CODE].startswith("E"):
103+
logging.error(full_message)
104+
else:
105+
logging.warning(full_message)

0 commit comments

Comments
 (0)