-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocstring_extractor.py
75 lines (56 loc) · 2.15 KB
/
docstring_extractor.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
import ast
from os.path import basename, splitext
from docstring_parser import parse
NODE_TYPES = {
ast.ClassDef: "Class",
ast.FunctionDef: "Function",
ast.Module: "Module",
}
def parse_docstrings(source):
"""Parse Python source code and yield a tuple of ast node instance, name,
line number and docstring for each function/method, class and module.
The line number refers to the first line of the docstring. If there is
no docstring, it gives the first line of the class, funcion or method
block, and docstring is None.
"""
return process_node(ast.parse(source))
def process_node(node):
"""Recursive function to obtain ast nodes"""
node_type = NODE_TYPES.get(type(node))
docstring_text = ast.get_docstring(node)
lineno = getattr(node, "lineno", 0)
if docstring_text:
docstring = parse(docstring_text)
else:
docstring = None
# Recursion with supported node types
children = [
process_node(n) for n in node.body if isinstance(n, tuple(NODE_TYPES))
]
return {
"type": node_type,
"name": getattr(node, "name", None),
"line": lineno,
"docstring": docstring,
"docstring_text": docstring_text if docstring_text else "",
"content": children,
}
def get_docstrings(source, module_name=None):
"""Parse Python source code from string and print docstrings.
For each class, method or function and the module, prints a heading with
the type, name and line number and then the docstring with normalized
indentation.
The module name is determined from the filename, or, if the source is
passed as a string, from the optional `module` argument.
The line number refers to the first line of the docstring, if present,
or the first line of the class, funcion or method block, if there is none.
"""
if hasattr(source, "read"):
filename = getattr(source, "name")
source = source.read()
if not module_name:
module_name = splitext(basename(filename))[0]
docstrings = parse_docstrings(source)
if module_name:
docstrings["name"] = module_name
return docstrings