-
Notifications
You must be signed in to change notification settings - Fork 1
/
json_log_formatter.py
executable file
·61 lines (48 loc) · 1.88 KB
/
json_log_formatter.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vi: set ft=python :
"""
Parses the json formatted output of a process and format it in a more readable way
usage:
json_log_formatter.py <command>
e.g.
json_log_formatter.py "uxfp -f <stream> --json" # no filter
json_log_formatter.py "uxfp -f <stream> --json" --filter LOG # only LOG json messages
json_log_formatter.py "uxfp -f <stream> --json" --filter LOG REPORT # only LOG and REPORT
"""
import sys
import subprocess
import json
import argparse
def execute(cmd):
""" execute command """
popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
for stdout_line in iter(popen.stdout.readline, ""):
yield stdout_line
popen.stdout.close()
return_code = popen.wait()
if return_code:
raise subprocess.CalledProcessError(return_code, cmd)
def sanitize(string):
""" sanitize a string before JSON-ize it """
return string.strip().replace("\\", "") # slash replace is only needed on Windows
if __name__ == "__main__":
PARSER = argparse.ArgumentParser(usage=__doc__)
PARSER.add_argument("command", help="the command to execute (enclosed in quotes)")
PARSER.add_argument(
"-f", "--filter", default=[], nargs="*", help="filter by json type (LOG, PROGRESS, REPORT)"
)
OPTS = PARSER.parse_args()
if len(sys.argv) == 1:
print(__doc__)
sys.exit(1)
FILTERS = ["LOG", "REPORT", "PROGRESS"]
for f in OPTS.filter:
if f not in FILTERS:
print("ERR: unrecognized filter name '{}'. Valid values {}".format(f, FILTERS))
sys.exit(1)
for p in execute(sys.argv[1].split()):
json_object = json.loads(sanitize(p))
if not OPTS.filter or json_object["type"] in OPTS.filter:
formatted_json = json.dumps(json_object, indent=2)
print(formatted_json)