-
Notifications
You must be signed in to change notification settings - Fork 0
/
hltflow.py
executable file
·57 lines (48 loc) · 1.62 KB
/
hltflow.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
#!/usr/bin/env python
import argparse
import json
import sys
from hltflow.core import StreamerFlowchart
from hltflow import latex
DESC = '''
Turn an Hlt1 streamer into a nice TikZ-based flowchart.
This program will read the streamer's code from stdin.
Thus it can be used with pipes as for example:
{} Test < tests/data/code.txt
'''.format(sys.argv[0])
EPILOG = '''
When supplying a configuration in a JSON file make sure that
the syntax is correct. Example:
[{
"name": "Your streamer's name here.",
"code": "Your streamer's code here. Separate newlines by \\n"
}, {
"name": "Another streamer's name, you can add as many as you like.",
"code": "Its code."
}]
'''
def main():
"""
Run a command line interface that exposes the functionality in a simple
way.
"""
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=DESC,
epilog=EPILOG
)
parser.add_argument('name', help='the streamer\'s name')
parser.add_argument('-c', '--config', metavar='configfile',
help='Path of a JSON file containing a valid config.')
args = parser.parse_args()
if args.config is None: # process single file read from stdin
streamers = [{'name': args.name, 'code': sys.stdin.read()}]
else: # process one or more files from config
with open(args.config) as cf:
streamers = json.load(cf)
flowcharts = (StreamerFlowchart(**s) for s in streamers)
figures = (latex.make_figure(f) for f in flowcharts)
document = latex.make_document(figures)
print(document)
if __name__ == '__main__':
main()