Created
May 7, 2024 13:47
-
-
Save plutov/88342bc9bf555180770ed78b3c3bfcc2 to your computer and use it in GitHub Desktop.
frames.py
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
from pathlib import Path | |
from collections.abc import Iterator | |
import sys | |
import json | |
import dataclasses | |
THREADS_SEPARATOR = "DALVIK THREADS:" | |
def parse(lines: list) -> list: | |
result = [] | |
in_threads = False | |
headers = [] | |
for line in lines: | |
line = line.strip() | |
if line == THREADS_SEPARATOR: | |
in_threads = True | |
continue | |
if not in_threads: | |
continue | |
if line == "": | |
headers = [] | |
continue | |
if len(headers) == 0 or line.startswith("|"): | |
if len(headers) == 0: | |
result.append([]) | |
headers.append(line) | |
else: | |
result[-1].append(parse_line(line)) | |
return result | |
def parse_line(line: str) -> dict: | |
if not line.startswith("at"): | |
return {"raw": line} | |
line = line.lstrip("at ") | |
func = None | |
file = None | |
line_number = None | |
parts = line.split("(") | |
func = parts[0] | |
file_str = parts[1].rstrip(")") | |
if ":" in file_str: | |
file_parts = file_str.split(":") | |
file = file_parts[0] | |
line_number = file_parts[1] | |
return { | |
"func": func, | |
"file": file, | |
"line_number": line_number, | |
} | |
def main(path: Path) -> None: | |
with path.open() as f: | |
result = parse([l.rstrip("\n") for l in f]) | |
print_result(result) | |
def print_result(result: object) -> None: | |
# Note: this is just using JSON to render this out nicely, | |
# we're not interested in making it JSON serializable. | |
json.dump( | |
result, | |
sys.stdout, | |
sort_keys=True, | |
indent=2, | |
default=dataclasses.asdict, | |
) | |
if __name__ == "__main__": | |
main(Path("./input.txt")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment