1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
21 # Module with function to extract a doctree from an reStructuredText file.
22 # Named 'Mini' because we only parse the minimum data needed to check
23 # Python classes, methods and attributes match up to those in existing modules.
24 # (To test for documentation completeness)
26 # note: literalinclude's are not followed.
27 # could be nice to add but not really needed either right now.
31 Directive = collections.namedtuple(
41 def parse_rst_py(filepath):
44 # Get the prefix assuming the line is lstrip()'d
48 re_prefix = re.compile(r"^\.\.\s([a-zA-Z09\-]+)::\s*(.*)\s*$")
50 tree = collections.defaultdict(list)
53 f = open(filepath, encoding="utf-8")
54 for i, line in enumerate(f):
55 line_strip = line.lstrip()
56 # ^\.\.\s[a-zA-Z09\-]+::.*$
57 # if line.startswith(".. "):
58 march = re_prefix.match(line_strip)
61 directive, value = march.group(1, 2)
62 indent = len(line) - len(line_strip)
63 value_strip = value.replace("(", " ").split()
64 value_strip = value_strip[0] if value_strip else ""
66 item = Directive(type=directive,
68 value_strip=value_strip,
73 tree[indent].append(item)
74 if indent_prev < indent:
75 indent_map[indent] = indent_prev
77 tree[indent_map[indent]][-1].members.append(item)
85 # not intended use, but may as well print rst files passed as a test.
88 if arg.lower().endswith((".txt", ".rst")):
89 items = parse_rst_py(arg)
94 if __name__ == "__main__":