-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_from_json.py
74 lines (61 loc) · 2.28 KB
/
generate_from_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# TODO: This should really be merged in with `generate_task_sheets.py` as a
# secondary command or something.
import gzip
import json
from pathlib import Path
import sys
from web_monitoring.db import DbJsonDecoder
from generate_task_sheets import ResultItem, filter_priority, write_sheets
def read_json_data(input: Path) -> list[ResultItem]:
with input.open('rb') as file:
start = file.read(2)
file.seek(0)
if start == b'\x1f\x8b':
file = gzip.open(file)
raw_data = json.load(file, cls=DbJsonDecoder)
return [
(
item['page'],
item['analysis'],
Exception(item['error']) if item['error'] else None,
)
for item in raw_data
]
def main(input: Path, output: Path, tags: list[str], threshold: float) -> None:
data = read_json_data(input)
if tags:
data = [
item for item in data
if any(
tag['name'] in tags
for tag in item[0]['tags']
)
]
output.mkdir(exist_ok=True)
write_sheets(output, filter_priority(data, threshold))
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Re-generate CSVs from the raw data of a previous run.')
parser.add_argument('input_json', type=Path,
help='Path to JSON data to reformat as CSV.')
parser.add_argument('--output', type=Path, required=True,
help='Output CSV files in this directory.')
parser.add_argument('--tag', action='append', help='Only anlyze pages with this tag (repeat for multiple tags).')
parser.add_argument('--threshold', type=float, default=0.0, help='Minimum priority value to include in output.')
options = parser.parse_args()
input_path: Path = options.input_json
if input_path.is_dir():
for name in ['_results.json', '_results.json.gz']:
candidate = input_path / name
if candidate.is_file():
input_path = candidate
break
if not input_path.is_file():
print(f'Cannot find input JSON file at "{input_path}"')
sys.exit(1)
main(
input=input_path,
output=options.output,
tags=options.tag,
threshold=options.threshold
)