Skip to content

Instantly share code, notes, and snippets.

@cxs-kge
Last active June 15, 2021 09:38
Show Gist options
  • Save cxs-kge/689597fd216b0a64b5bf0c35354cd0b4 to your computer and use it in GitHub Desktop.
Save cxs-kge/689597fd216b0a64b5bf0c35354cd0b4 to your computer and use it in GitHub Desktop.
A really simple way to fold long pysnooper logs
from argparse import ArgumentParser
from html import escape
def convert(input_path: str, output_path: str) -> None:
"""Convert a pysnooper log file to an html page."""
with open(output_path, "w") as output:
output.write("""\
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>pysnooper</title>
<style>
details {
padding-left: 4rem;
}
</style>
</head>
<body>
""")
with open(input_path, "r") as pysnoop_log:
indent_level = 0
for line in pysnoop_log:
line_indent_level = (len(line) - len(line.lstrip(" "))) // 4
line = line.lstrip(" ")
if line_indent_level > indent_level:
output.write("<details>")
elif line_indent_level < indent_level:
output.write("</details>")
output.write("<pre>"+escape(line)+"</pre>")
indent_level = line_indent_level
if indent_level == 0 and line.startswith("Elapsed time: "):
output.write("<hr>")
output.write("""
</body>
</html>
""")
def main():
parser = ArgumentParser()
parser.add_argument("input_file", type=str, help="The input log file")
parser.add_argument("output_file", type=str, help="The html output file")
namespace = parser.parse_args()
convert(namespace.input_file, namespace.output_file)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment