-
Notifications
You must be signed in to change notification settings - Fork 7
/
kv-extractor.py
64 lines (47 loc) · 1.86 KB
/
kv-extractor.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
#!/usr/bin/env python3
"""
An experiment in extracting key value pairs from files.
"""
__author__ = "David Morris [email protected]"
__version__ = "0.1.0"
import sys
import json
import logging
import argparse
import bundlefun as bf
logger = logging.getLogger(__name__)
def main(args):
log_level = args.log_level.upper()
logging.basicConfig(level=log_level)
logger = logging.getLogger(__name__)
report = bf.Report()
report.get_ghes_config("../metadata/github.conf")
results = {}
for (line_number, line, exception) in bf.read_file_line_by_line(args.kv_input_file):
if line is None:
report.file_stats.setdefault(args.kv_input_file, {"lines_with_read_failed": 0, "lines_total": 0, "lines_parsed": 0, "lines_with_parse_failures": 0})["lines_with_read_failed"] += 1
continue
report.file_stats.setdefault(args.kv_input_file, {"lines_with_read_failed": 0, "lines_total": 0, "lines_parsed": 0, "lines_with_parse_failures": 0})["lines_total"] += 1
try:
r = bf.parse_kv(line)
report.file_stats[args.kv_input_file]["lines_parsed"] += 1
results.update(r)
except Exception as e:
report.file_stats[args.kv_input_file]["lines_with_parse_failures"] += 1
logger.debug(f"""keys: {results.keys()}""")
report.file_stats[args.kv_input_file]["kv_pairs_extracted"] = len(results.keys())
keys_extracted = len(results.keys())
print(json.dumps(list(results.keys())))
print(report, file=sys.stderr)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('kv_input_file', help='Full path to a log file to process')
parser.add_argument(
"--log-level",
action="store",
dest="log_level",
default="info",
help="Set the log level",
)
args = parser.parse_args()
main(args)