Created
July 8, 2024 08:42
-
-
Save usr-ein/e7c5f0a5346e8d976f3866db99627f61 to your computer and use it in GitHub Desktop.
Simple script to format a garbled python Traceback where all new lines and tabs have been removed, and to add them back.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Simple script to format a garbled python Traceback | |
where all new lines and tabs have been removed, and | |
to add them back. | |
""" | |
import sys | |
import os | |
import re | |
def main(): | |
argv = sys.argv | |
if len(argv) < 2: | |
print(f"Usage: {argv[0]} unformatted_traceback.txt", file=sys.stderr) | |
exit(1) | |
if not os.path.isfile(argv[1]): | |
print(f"{argv[1]} is not a file", file=sys.stderr) | |
exit(1) | |
with open(argv[1], 'r') as f: | |
tb_txt = f.read() | |
tb_header = "Traceback (most recent call last):" | |
s = f"{os.linesep}{os.linesep}{tb_header}\n".join(format_one_tb(t) for t in tb_txt.split(tb_header)) | |
print(s, file=sys.stdout) | |
file_re = re.compile(r' File "[^"]+", line [0-9]+, in [^ ]+ ') | |
def format_one_tb(tb: str) -> str: | |
file_lines = file_re.split(tb)[1:] | |
file_expr = file_re.findall(tb) | |
return os.linesep.join(e + "\n\t" + l for e,l in zip(file_expr, file_lines)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment