forked from lowRISC/opentitan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2csvg.py
executable file
·144 lines (129 loc) · 4.95 KB
/
i2csvg.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
r"""Command-line tool to convert i2c to svg
"""
import argparse
import logging as log
import sys
from pathlib import PurePath
import pkg_resources # part of setuptools
from i2csvg import convert
from reggen import version
ep = """defaults or the filename - can be used for stdin/stdout
By default all input files are concatenated into a single output file
this can be changed with the --multiout flag.
"""
def main():
done_stdin = False
parser = argparse.ArgumentParser(
prog="i2csvg.py",
formatter_class=argparse.RawDescriptionHelpFormatter,
description=__doc__,
epilog=ep)
parser.add_argument('--version',
action='store_true',
help='Show version and exit')
parser.add_argument('-v',
'--verbose',
action='store_true',
help='Verbose output during processing')
parser.add_argument('-d',
'--debug',
action='store_true',
help='Include internal representation in output file')
parser.add_argument('-t',
'--text',
action='store_true',
help='Include text output in output file')
parser.add_argument('-n',
'--nosvg',
action='store_true',
help="Don't include svg in output")
parser.add_argument('-f',
'--fifodata',
action='store_true',
help='Data is hexdump of writes to FDATA fifo')
parser.add_argument(
'-p',
'--prefix',
action='store',
help='Only process lines with this prefix (the prefix is removed)')
parser.add_argument(
'-m',
'--multiout',
action='store_true',
help='Generate separate output file with .svg extension from inputs')
parser.add_argument('-o',
'--output',
type=argparse.FileType('w'),
default=sys.stdout,
metavar='file',
help='Output file (default stdout)')
parser.add_argument('srcfile',
nargs='*',
metavar='input',
default='-',
help='source i2c file (default stdin)')
args = parser.parse_args()
if args.version:
version.show_and_exit(__file__, ["Hjson"])
if (args.verbose):
log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG)
else:
log.basicConfig(format="%(levelname)s: %(message)s")
outfile = args.output
# output will be:
# .svg if a single input file and no additional details (debug/text)
# or --multiout and no additional details
# .html if combining multiple files (not --multiout) or additional details
# .txt if --nosvg
combiningfiles = (len(args.srcfile) > 1) and not args.multiout
extrainfo = args.debug or args.text or combiningfiles
if args.nosvg:
makehtml = False
outext = '.txt'
elif extrainfo:
makehtml = True
outext = '.html'
else:
makehtml = False
outext = '.svg'
with outfile:
for filename in args.srcfile:
if (filename == '-'):
if (done_stdin):
log.warn("Ignore stdin after first use\n")
continue
done_stdin = True
infile = sys.stdin
else:
infile = open(filename, 'r', encoding='UTF-8')
with infile:
log.info("\nFile now " + filename)
if args.multiout:
outfname = str(PurePath(filename).with_suffix('.svg'))
outf = open(outfname, 'w', encoding='UTF-8')
else:
outf = outfile
if makehtml:
outf.write("<H2>" + filename + "</H2>")
tr = convert.parse_file(infile, args.fifodata, args.prefix)
if args.debug:
convert.output_debug(outf, tr,
'<br>\n' if makehtml else '\n')
outf.write('<br>\n' if makehtml else '\n')
if args.text:
if makehtml:
outf.write("<pre>\n")
convert.output_text(outf, tr,
'<br>\n' if makehtml else '\n')
if makehtml:
outf.write("</pre>\n")
if not args.nosvg:
convert.output_svg(outf, tr, makehtml)
if args.multiout:
outf.close()
if __name__ == '__main__':
main()