1#!/usr/bin/env python3
2# pylint: disable=C0112,C0114,C0115,C0116,C0301,R0201,R0903
3# -*- Python -*- See copyright, etc below
4######################################################################
5
6import argparse
7import re
8
9#######################################################################
10
11
12class VlSphinxExtract:
13    debug = 0
14    SkipBasenames = {}
15
16    def process(self, filename):
17        with open(filename) as fhr:
18            fhw = None
19            for line in fhr:
20                # =for VL_SPHINX_EXTRACT "file_to_write_to"
21                match = re.search(r'VL_SPHINX_EXTRACT +"([^"]+)"', line)
22                if match:
23                    outname = match.group(1)
24                    print("Writing %s" % outname)
25                    fhw = open(outname, "w")
26                    fhw.write(
27                        ".. comment: generated by vl_sphinx_extract from " +
28                        filename + "\n")
29                    fhw.write(".. code-block::\n")
30                elif re.match(r'^[=a-zA-Z0-9_]', line):
31                    fhw = None
32                elif fhw:
33                    fhw.write(line)
34
35
36#######################################################################
37
38parser = argparse.ArgumentParser(
39    allow_abbrev=False,
40    formatter_class=argparse.RawDescriptionHelpFormatter,
41    description="""Read a file and extract documentation data.""",
42    epilog=
43    """ Copyright 2021-2021 by Wilson Snyder.  This package is free software;
44you can redistribute it and/or modify it under the terms of either the GNU
45Lesser General Public License Version 3 or the Perl Artistic License
46Version 2.0.
47
48SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0""")
49
50parser.add_argument('--debug',
51                    action='store_const',
52                    const=9,
53                    help='enable debug')
54parser.add_argument('path', help='path to extract from')
55Args = parser.parse_args()
56
57o = VlSphinxExtract()
58o.debug = Args.debug
59o.process(Args.path)
60
61######################################################################
62# Local Variables:
63# compile-command: "./vl_sphinx_extract --debug ../../bin/verilator"
64# End:
65