Mark Hayter | 14336e7 | 2019-12-29 16:21:34 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright lowRISC contributors. |
| 3 | # Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | r"""Command-line tool to convert i2c to svg |
| 6 | """ |
| 7 | |
| 8 | import argparse |
| 9 | import logging as log |
| 10 | import sys |
| 11 | from pathlib import PurePath |
| 12 | |
| 13 | import pkg_resources # part of setuptools |
| 14 | |
| 15 | from i2csvg import convert |
| 16 | from reggen import version |
| 17 | |
| 18 | ep = """defaults or the filename - can be used for stdin/stdout |
| 19 | By default all input files are concatenated into a single output file |
| 20 | this can be changed with the --multiout flag. |
| 21 | """ |
| 22 | |
| 23 | |
| 24 | def main(): |
| 25 | done_stdin = False |
| 26 | parser = argparse.ArgumentParser( |
| 27 | prog="i2csvg.py", |
| 28 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 29 | description=__doc__, |
| 30 | epilog=ep) |
| 31 | parser.add_argument('--version', |
| 32 | action='store_true', |
| 33 | help='Show version and exit') |
| 34 | parser.add_argument('-v', |
| 35 | '--verbose', |
| 36 | action='store_true', |
| 37 | help='Verbose output during processing') |
| 38 | parser.add_argument('-d', |
| 39 | '--debug', |
| 40 | action='store_true', |
| 41 | help='Include internal representation in output file') |
| 42 | parser.add_argument('-t', |
| 43 | '--text', |
| 44 | action='store_true', |
| 45 | help='Include text output in output file') |
| 46 | parser.add_argument('-n', |
| 47 | '--nosvg', |
| 48 | action='store_true', |
| 49 | help="Don't include svg in output") |
| 50 | |
| 51 | parser.add_argument('-f', |
| 52 | '--fifodata', |
| 53 | action='store_true', |
| 54 | help='Data is hexdump of writes to FDATA fifo') |
| 55 | parser.add_argument( |
| 56 | '-p', |
| 57 | '--prefix', |
| 58 | action='store', |
| 59 | help='Only process lines with this prefix (the prefix is removed)') |
| 60 | parser.add_argument( |
| 61 | '-m', |
| 62 | '--multiout', |
| 63 | action='store_true', |
| 64 | help='Generate separate output file with .svg extension from inputs') |
| 65 | parser.add_argument('-o', |
| 66 | '--output', |
| 67 | type=argparse.FileType('w'), |
| 68 | default=sys.stdout, |
| 69 | metavar='file', |
| 70 | help='Output file (default stdout)') |
| 71 | parser.add_argument('srcfile', |
| 72 | nargs='*', |
| 73 | metavar='input', |
| 74 | default='-', |
| 75 | help='source i2c file (default stdin)') |
| 76 | args = parser.parse_args() |
| 77 | |
| 78 | if args.version: |
| 79 | version.show_and_exit(__file__, ["Hjson"]) |
| 80 | |
| 81 | if (args.verbose): |
| 82 | log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG) |
| 83 | else: |
| 84 | log.basicConfig(format="%(levelname)s: %(message)s") |
| 85 | |
| 86 | outfile = args.output |
| 87 | # output will be: |
| 88 | # .svg if a single input file and no additional details (debug/text) |
| 89 | # or --multiout and no additional details |
| 90 | # .html if combining multiple files (not --multiout) or additional details |
| 91 | # .txt if --nosvg |
| 92 | combiningfiles = (len(args.srcfile) > 1) and not args.multiout |
| 93 | extrainfo = args.debug or args.text or combiningfiles |
| 94 | |
| 95 | if args.nosvg: |
| 96 | makehtml = False |
| 97 | outext = '.txt' |
| 98 | elif extrainfo: |
| 99 | makehtml = True |
| 100 | outext = '.html' |
| 101 | else: |
| 102 | makehtml = False |
| 103 | outext = '.svg' |
| 104 | |
| 105 | with outfile: |
| 106 | for filename in args.srcfile: |
| 107 | if (filename == '-'): |
| 108 | if (done_stdin): |
| 109 | log.warn("Ignore stdin after first use\n") |
| 110 | continue |
| 111 | done_stdin = True |
| 112 | infile = sys.stdin |
| 113 | else: |
| 114 | infile = open(filename, 'r', encoding='UTF-8') |
| 115 | with infile: |
| 116 | log.info("\nFile now " + filename) |
| 117 | if args.multiout: |
| 118 | outfname = str(PurePath(filename).with_suffix('.svg')) |
| 119 | outf = open(outfname, 'w', encoding='UTF-8') |
| 120 | else: |
| 121 | outf = outfile |
| 122 | if makehtml: |
| 123 | outf.write("<H2>" + filename + "</H2>") |
| 124 | tr = convert.parse_file(infile, args.fifodata, args.prefix) |
| 125 | if args.debug: |
| 126 | convert.output_debug(outf, tr, |
| 127 | '<br>\n' if makehtml else '\n') |
| 128 | outf.write('<br>\n' if makehtml else '\n') |
| 129 | if args.text: |
| 130 | if makehtml: |
| 131 | outf.write("<pre>\n") |
| 132 | convert.output_text(outf, tr, |
| 133 | '<br>\n' if makehtml else '\n') |
| 134 | if makehtml: |
| 135 | outf.write("</pre>\n") |
| 136 | if not args.nosvg: |
| 137 | convert.output_svg(outf, tr, makehtml) |
| 138 | |
| 139 | if args.multiout: |
| 140 | outf.close() |
| 141 | |
| 142 | |
| 143 | if __name__ == '__main__': |
| 144 | main() |