forked from OpenGeoVis/PVGeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc-include-import.py
184 lines (153 loc) · 5.93 KB
/
doc-include-import.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# -*- coding: utf-8 -*-
#
# docs-include.py
#
# Copyright 2018 Bane Sullivan <[email protected]>
# This script generates mkdocs friendly Markdown documentation from a function declaration in a python package.
# It is based on the the following blog post by Christian Medina
# https://medium.com/python-pandemonium/python-introspection-with-the-inspect-module-2c85d5aa5a48#.twcmlyack
#
#
from __future__ import print_function
import builtins
import re
import os.path
from codecs import open
from markdown.extensions import Extension
from markdown.preprocessors import Preprocessor
import os
import sys
from types import ModuleType
import importlib
import inspect
realimport = builtins.__import__
class DummyModule(ModuleType):
def __getattr__(self, key):
return None
__all__ = [] # support wildcard imports
def tryimport(name, globals={}, locals={}, fromlist=[], level=1):
try:
return realimport(name, globals, locals, fromlist, level)
except ImportError:
return DummyModule(name)
DEF_SYNTAX = re.compile(r'\{def:\s*(.+?)\s*\}')
CLASS_SYNTAX = re.compile(r'\{class:\s*(.+?)\s*\}')
def _cleandocstr(doc, rmv='\n '):
# Decrease indentation from method def
doc = doc.replace(rmv,'\n')
# add a newline before list items
doc = doc.replace('\n- ','\n\n- ')
lines = doc.split('\n')
dels = []
for i in range(len(lines)):
ln = lines[i]
if len(ln)>2 and ln[0] == '-' and ln[1] == '-':
dels.append(i)
lines[i-1] = '**' + lines[i-1] + '**\n'
# Delete the underlines
for i in range(len(dels)):
del(lines[dels[i]-i])
# Join the lines
doc = "\n".join((str(x) for x in lines))
doc = doc.replace('\n','\n ')
return doc
################
def _getDefMarkdown(method, module, rmv='\n '):
sig = inspect.signature(method)
output = ['\n??? abstract "%s.%s"'% (module.__name__, method.__name__)]
output.append(' `:::py %s.%s%s`'% (module.__name__, method.__name__, sig))
if method.__doc__:
output.append(_cleandocstr(method.__doc__, rmv=rmv))
return "\n".join((str(x) for x in output))
####
def _getClassMarkdown(cla, module):
rmv = '\n '
output = ['### Class `%s.%s`' % (module.__name__, cla.__name__)]
if cla.__doc__:
output.append(_cleandocstr(cla.__doc__).replace('\n ','\n'))
# Now handle member function just like normal functions
members = inspect.getmembers(cla)
for mem in members:
if mem[0][0] != '_':
output.append(_getDefMarkdown(mem[1], cla, rmv=rmv))
return "\n".join((str(x) for x in output))
################
def generateDefDocs(modulename):
sys.path.append(os.getcwd())
# Attempt import
p, m = modulename.rsplit('.', 1)
mod = importlib.import_module(p)
met = getattr(mod, m)
if mod is None:
raise Exception("Module not found")
# Module imported correctly, let's create the docs
return _getDefMarkdown(met, mod)
def generateClassDocs(classname):
sys.path.append(os.getcwd())
# Attempt import
p, c = classname.rsplit('.', 1)
mod = importlib.import_module(p)
cla = getattr(mod, c)
if cla is None:
raise Exception("Class not found")
if not inspect.isclass(cla):
raise Exception("Not a class: %s" % cla.__name__)
# Module imported correctly, let's create the docs
return _getClassMarkdown(cla, mod)
##############
class MethodInclude(Extension):
def __init__(self, configs={}):
self.config = {
'base_path': ['.', 'Default location from which to evaluate ' \
'relative paths for the include statement.'],
'encoding': ['utf-8', 'Encoding of the files used by the include ' \
'statement.']
}
for key, value in configs.items():
self.setConfig(key, value)
def extendMarkdown(self, md, md_globals):
md.preprocessors.add(
'docs-include', MethodIncludePreprocessor(md,self.getConfigs()),'_begin'
)
class MethodIncludePreprocessor(Preprocessor):
'''
This provides an "include" function for pyhton module definitions for Markdown. The syntax is {def:module.method}, which will be replaced by the contents of doc string for that method. This replacement is done prior to any other Markdown processing. A safe import is used so that the module can be inported into any python environment.
'''
def __init__(self, md, config):
super(MethodIncludePreprocessor, self).__init__(md)
self.base_path = config['base_path']
self.encoding = config['encoding']
def run(self, lines):
done = False
builtins.__import__ = tryimport
while not done:
for line in lines:
loc = lines.index(line)
m = DEF_SYNTAX.search(line)
c = CLASS_SYNTAX.search(line)
if m:
modname = m.group(1)
text = generateDefDocs(modname).split('\n')
line_split = DEF_SYNTAX.split(line,maxsplit=0)
if len(text) == 0:
text.append('')
text[0] = line_split[0] + text[0]
text[-1] = text[-1] + line_split[2]
lines = lines[:loc] + text + lines[loc+1:]
break
elif c:
classname = c.group(1)
text = generateClassDocs(classname).split('\n')
line_split = CLASS_SYNTAX.split(line,maxsplit=0)
if len(text) == 0:
text.append('')
text[0] = line_split[0] + text[0]
text[-1] = text[-1] + line_split[2]
lines = lines[:loc] + text + lines[loc+1:]
break
else:
done = True
builtins.__import__ = realimport
return lines
def makeExtension(*args,**kwargs):
return MethodInclude(kwargs)