-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter.py
69 lines (57 loc) · 1.57 KB
/
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
62
63
64
65
66
67
68
69
'''
Create format report from json to html using jinja2
'''
import json
import sys
import argparse
from jinja2 import Template
def get_template(argument: str):
'''
get template content
'''
if argument == '-':
return Template(sys.stdin.read())
return Template(open(argument).read())
def get_entries(argument: str):
'''
get stream for reading json file
'''
if argument == '-':
return sys.stdin
return open(argument, mode='rt')
def main():
'''
Main entry point
'''
parser = argparse.ArgumentParser()
parser.add_argument(
'-i',
'--input',
help='file containing result of comparison,' +
' use \'-\' for stdin (default)',
metavar=('<name>', '<file>'),
nargs=2,
action='append'
)
parser.add_argument(
'-t',
'--template',
help='file containing template' +
', use \'-\' for stdin (default:\'metrics.jinja.txt\')',
metavar='<template file>',
default='metrics.jinja.txt'
)
args = parser.parse_args()
# Verify args
assert(args.input is not None), 'please specify at least one input'
use_stdin = len(list(filter(lambda value: value[0] == '-', args.input)))
if args.template == '-':
use_stdin += 1
assert use_stdin <= 1, 'stdin used multiple times'
template = get_template(args.template)
template_args = {}
for pair in args.input:
template_args[pair[0]] = json.load(get_entries(pair[1]))
print(template.render(**template_args))
if __name__ == '__main__':
main()