diff options
author | Campbell Barton <ideasman42@gmail.com> | 2012-11-21 11:40:46 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2012-11-21 11:40:46 +0400 |
commit | e6f9ef8f7d3e3a8cb0404bfbddedc10694bbbba5 (patch) | |
tree | 979ed7ccef756d879631138b01c40e10ebbb604e /source/tests/rst_to_doctree_mini.py | |
parent | 36d2d090f65785fb1e948aed72b14a888a8ef55f (diff) |
scripts to report missing py reference api docs. (simple RST parse and compare with python)
Diffstat (limited to 'source/tests/rst_to_doctree_mini.py')
-rw-r--r-- | source/tests/rst_to_doctree_mini.py | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/source/tests/rst_to_doctree_mini.py b/source/tests/rst_to_doctree_mini.py new file mode 100644 index 00000000000..19c20ccc006 --- /dev/null +++ b/source/tests/rst_to_doctree_mini.py @@ -0,0 +1,91 @@ +# ##### BEGIN GPL LICENSE BLOCK ##### +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# ##### END GPL LICENSE BLOCK ##### + +# <pep8 compliant> + +# Module with function to extract a doctree from an reStructuredText file. +# Named 'Mini' because we only parse the minimum data needed to check +# Python classes, methods and attributes match up to those in existing modules. +# (To test for documentation completeness) + +# note: literalinclude's are not followed. +# could be nice to add but not really needed either right now. + +import collections + +Directive = collections.namedtuple('Directive', + ("type", + "value", + "value_strip", + "line", + "indent", + "members")) + + +def parse_rst_py(filepath): + import re + + # Get the prefix assuming the line is lstrip()'d + # ..foo:: bar + # --> + # ("foo", "bar") + re_prefix = re.compile(r"^\.\.\s([a-zA-Z09\-]+)::\s*(.*)\s*$") + + tree = collections.defaultdict(list) + + f = open(filepath, encoding="utf-8") + indent_lists = [] + for i, line in enumerate(f): + line_strip = line.lstrip() + # ^\.\.\s[a-zA-Z09\-]+::.*$ + #if line.startswith(".. "): + march = re_prefix.match(line_strip) + + if march: + directive, value = march.group(1, 2) + indent = len(line) - len(line_strip) + value_strip = value.replace("(", " ").split() + value_strip = value_strip[0] if value_strip else "" + + item = Directive(type=directive, + value=value, + value_strip=value_strip, + line=i, + indent=indent, + members=[]) + + tree[indent].append(item) + if indent > 0: + # get the previous indent, ok this isn't fast but no matter. + keys = list(sorted(tree.keys())) + key_index = keys.index(indent) + parent_indent = keys[key_index - 1] + tree[parent_indent][-1].members.append(item) + f.close() + + return tree[0] + + +if __name__ == "__main__": + # not intended use, but may as well print rst files passed as a test. + import sys + for arg in sys.argv: + if arg.lower().endswith((".txt", ".rst")): + items = parse_rst_py(arg) + for i in items: + print(i) |