2963
|
1 |
#!/usr/bin/env python
|
3617
|
2 |
#Copyright ReportLab Europe Ltd. 2000-2012
|
2963
|
3 |
#see license.txt for license details
|
|
4 |
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/tools/docco/docpy.py
|
|
5 |
|
|
6 |
"""Generate documentation from live Python objects.
|
|
7 |
|
|
8 |
This is an evolving module that allows to generate documentation
|
|
9 |
for python modules in an automated fashion. The idea is to take
|
|
10 |
live Python objects and inspect them in order to use as much mean-
|
|
11 |
ingful information as possible to write in some formatted way into
|
|
12 |
different types of documents.
|
|
13 |
|
|
14 |
In principle a skeleton captures the gathered information and
|
|
15 |
makes it available via a certain API to formatters that use it
|
|
16 |
in whatever way they like to produce something of interest. The
|
|
17 |
API allows for adding behaviour in subclasses of these formatters,
|
|
18 |
such that, e.g. for certain classes it is possible to trigger
|
|
19 |
special actions like displaying a sample image of a class that
|
|
20 |
represents some graphical widget, say.
|
|
21 |
|
|
22 |
Type the following for usage info:
|
|
23 |
|
|
24 |
python docpy.py -h
|
|
25 |
"""
|
|
26 |
|
|
27 |
# Much inspired by Ka-Ping Yee's htmldoc.py.
|
|
28 |
# Needs the inspect module.
|
|
29 |
|
|
30 |
# Dinu Gherman
|
|
31 |
|
|
32 |
|
|
33 |
__version__ = '0.8'
|
|
34 |
|
|
35 |
|
3794
|
36 |
import sys, os, re, types, getopt, copy, time
|
2963
|
37 |
from reportlab.pdfgen import canvas
|
|
38 |
from reportlab.lib import colors
|
|
39 |
from reportlab.lib.units import inch, cm
|
|
40 |
from reportlab.lib.pagesizes import A4
|
|
41 |
from reportlab.lib import enums
|
|
42 |
from reportlab.lib.enums import TA_CENTER, TA_LEFT
|
|
43 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
|
44 |
from reportlab.platypus.flowables import Flowable, Spacer
|
|
45 |
from reportlab.platypus.paragraph import Paragraph
|
|
46 |
from reportlab.platypus.flowables \
|
|
47 |
import Flowable, Preformatted,Spacer, Image, KeepTogether, PageBreak
|
|
48 |
from reportlab.platypus.tableofcontents import TableOfContents
|
|
49 |
from reportlab.platypus.xpreformatted import XPreformatted
|
|
50 |
from reportlab.platypus.frames import Frame
|
|
51 |
from reportlab.platypus.doctemplate \
|
|
52 |
import PageTemplate, BaseDocTemplate
|
|
53 |
from reportlab.platypus.tables import TableStyle, Table
|
|
54 |
import inspect
|
|
55 |
|
|
56 |
####################################################################
|
|
57 |
#
|
|
58 |
# Stuff needed for building PDF docs.
|
|
59 |
#
|
|
60 |
####################################################################
|
|
61 |
|
|
62 |
|
|
63 |
def mainPageFrame(canvas, doc):
|
|
64 |
"The page frame used for all PDF documents."
|
|
65 |
|
|
66 |
canvas.saveState()
|
|
67 |
|
|
68 |
pageNumber = canvas.getPageNumber()
|
|
69 |
canvas.line(2*cm, A4[1]-2*cm, A4[0]-2*cm, A4[1]-2*cm)
|
|
70 |
canvas.line(2*cm, 2*cm, A4[0]-2*cm, 2*cm)
|
|
71 |
if pageNumber > 1:
|
|
72 |
canvas.setFont('Times-Roman', 12)
|
|
73 |
canvas.drawString(4 * inch, cm, "%d" % pageNumber)
|
|
74 |
if hasattr(canvas, 'headerLine'): # hackish
|
3794
|
75 |
headerline = , ' \215 '.join(canvas.headerLine) # bullet
|
2963
|
76 |
canvas.drawString(2*cm, A4[1]-1.75*cm, headerline)
|
|
77 |
|
|
78 |
canvas.setFont('Times-Roman', 8)
|
|
79 |
msg = "Generated with reportlab.lib.docpy. See http://www.reportlab.com!"
|
|
80 |
canvas.drawString(2*cm, 1.65*cm, msg)
|
|
81 |
|
|
82 |
canvas.restoreState()
|
|
83 |
|
|
84 |
|
|
85 |
class MyTemplate(BaseDocTemplate):
|
|
86 |
"The document template used for all PDF documents."
|
|
87 |
|
|
88 |
_invalidInitArgs = ('pageTemplates',)
|
|
89 |
|
|
90 |
def __init__(self, filename, **kw):
|
|
91 |
frame1 = Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm, id='F1')
|
|
92 |
self.allowSplitting = 0
|
3326
|
93 |
BaseDocTemplate.__init__(self, filename, **kw)
|
2963
|
94 |
self.addPageTemplates(PageTemplate('normal', [frame1], mainPageFrame))
|
|
95 |
|
|
96 |
|
|
97 |
def afterFlowable(self, flowable):
|
|
98 |
"Takes care of header line, TOC and outline entries."
|
|
99 |
|
|
100 |
if flowable.__class__.__name__ == 'Paragraph':
|
|
101 |
f = flowable
|
|
102 |
name7 = f.style.name[:7]
|
|
103 |
name8 = f.style.name[:8]
|
|
104 |
|
|
105 |
# Build a list of heading parts.
|
|
106 |
# So far, this is the *last* item on the *previous* page...
|
|
107 |
if name7 == 'Heading' and not hasattr(self.canv, 'headerLine'):
|
|
108 |
self.canv.headerLine = []
|
|
109 |
|
|
110 |
if name8 == 'Heading0':
|
|
111 |
self.canv.headerLine = [f.text] # hackish
|
|
112 |
elif name8 == 'Heading1':
|
|
113 |
if len(self.canv.headerLine) == 2:
|
|
114 |
del self.canv.headerLine[-1]
|
|
115 |
elif len(self.canv.headerLine) == 3:
|
|
116 |
del self.canv.headerLine[-1]
|
|
117 |
del self.canv.headerLine[-1]
|
|
118 |
self.canv.headerLine.append(f.text)
|
|
119 |
elif name8 == 'Heading2':
|
|
120 |
if len(self.canv.headerLine) == 3:
|
|
121 |
del self.canv.headerLine[-1]
|
|
122 |
self.canv.headerLine.append(f.text)
|
|
123 |
|
|
124 |
if name7 == 'Heading':
|
|
125 |
# Register TOC entries.
|
|
126 |
headLevel = int(f.style.name[7:])
|
|
127 |
self.notify('TOCEntry', (headLevel, flowable.getPlainText(), self.page))
|
|
128 |
|
|
129 |
# Add PDF outline entries.
|
|
130 |
c = self.canv
|
|
131 |
title = f.text
|
|
132 |
key = str(hash(f))
|
|
133 |
|
|
134 |
try:
|
|
135 |
if headLevel == 0:
|
|
136 |
isClosed = 0
|
|
137 |
else:
|
|
138 |
isClosed = 1
|
|
139 |
|
|
140 |
c.bookmarkPage(key)
|
|
141 |
c.addOutlineEntry(title, key, level=headLevel,
|
|
142 |
closed=isClosed)
|
|
143 |
except ValueError:
|
|
144 |
pass
|
|
145 |
|
|
146 |
|
|
147 |
####################################################################
|
|
148 |
#
|
|
149 |
# Utility functions (Ka-Ping Yee).
|
|
150 |
#
|
|
151 |
####################################################################
|
|
152 |
|
|
153 |
def htmlescape(text):
|
|
154 |
"Escape special HTML characters, namely &, <, >."
|
3794
|
155 |
return text.replace('&','&').replace('<','<').replace('>','>')
|
2963
|
156 |
|
|
157 |
def htmlrepr(object):
|
|
158 |
return htmlescape(repr(object))
|
|
159 |
|
|
160 |
|
|
161 |
def defaultformat(object):
|
|
162 |
return '=' + htmlrepr(object)
|
|
163 |
|
|
164 |
|
|
165 |
def getdoc(object):
|
|
166 |
result = inspect.getdoc(object)
|
|
167 |
if not result:
|
|
168 |
try:
|
|
169 |
result = inspect.getcomments(object)
|
|
170 |
except:
|
|
171 |
pass
|
3794
|
172 |
return result and result.rstrip() + '\n' or ''
|
2963
|
173 |
|
|
174 |
|
|
175 |
def reduceDocStringLength(docStr):
|
|
176 |
"Return first line of a multiline string."
|
|
177 |
|
3794
|
178 |
return docStr.split('\n')[0]
|
2963
|
179 |
|
|
180 |
|
|
181 |
####################################################################
|
|
182 |
#
|
|
183 |
# More utility functions
|
|
184 |
#
|
|
185 |
####################################################################
|
|
186 |
|
|
187 |
def makeHtmlSection(text, bgcolor='#FFA0FF'):
|
|
188 |
"""Create HTML code for a section.
|
|
189 |
|
|
190 |
This is usually a header for all classes or functions.
|
|
191 |
u """
|
3794
|
192 |
text = htmlescape(text.expandtabs())
|
2963
|
193 |
result = []
|
|
194 |
result.append("""<TABLE WIDTH="100\%" BORDER="0">""")
|
|
195 |
result.append("""<TR><TD BGCOLOR="%s" VALIGN="CENTER">""" % bgcolor)
|
|
196 |
result.append("""<H2>%s</H2>""" % text)
|
|
197 |
result.append("""</TD></TR></TABLE>""")
|
|
198 |
result.append('')
|
|
199 |
|
3794
|
200 |
return '\n'.join(result)
|
2963
|
201 |
|
|
202 |
|
|
203 |
def makeHtmlSubSection(text, bgcolor='#AAA0FF'):
|
|
204 |
"""Create HTML code for a subsection.
|
|
205 |
|
|
206 |
This is usually a class or function name.
|
|
207 |
"""
|
3794
|
208 |
text = htmlescape(text.expandtabs())
|
2963
|
209 |
result = []
|
|
210 |
result.append("""<TABLE WIDTH="100\%" BORDER="0">""")
|
|
211 |
result.append("""<TR><TD BGCOLOR="%s" VALIGN="CENTER">""" % bgcolor)
|
|
212 |
result.append("""<H3><TT><FONT SIZE="+2">%s</FONT></TT></H3>""" % text)
|
|
213 |
result.append("""</TD></TR></TABLE>""")
|
|
214 |
result.append('')
|
|
215 |
|
3794
|
216 |
return '\n'.join(result)
|
2963
|
217 |
|
|
218 |
|
|
219 |
def makeHtmlInlineImage(text):
|
|
220 |
"""Create HTML code for an inline image.
|
|
221 |
"""
|
|
222 |
|
|
223 |
return """<IMG SRC="%s" ALT="%s">""" % (text, text)
|
|
224 |
|
|
225 |
|
|
226 |
####################################################################
|
|
227 |
#
|
|
228 |
# Core "standard" docpy classes
|
|
229 |
#
|
|
230 |
####################################################################
|
|
231 |
|
|
232 |
class PackageSkeleton0:
|
|
233 |
"""A class collecting 'interesting' information about a package."""
|
|
234 |
pass # Not yet!
|
|
235 |
|
|
236 |
|
|
237 |
class ModuleSkeleton0:
|
|
238 |
"""A class collecting 'interesting' information about a module."""
|
|
239 |
|
|
240 |
def __init__(self):
|
|
241 |
# This is an ad-hoc, somewhat questionable 'data structure',
|
|
242 |
# but for the time being it serves its purpose and is fairly
|
|
243 |
# self-contained.
|
|
244 |
self.module = {}
|
|
245 |
self.functions = {}
|
|
246 |
self.classes = {}
|
|
247 |
|
|
248 |
|
|
249 |
# Might need more like this, later.
|
|
250 |
def getModuleName(self):
|
|
251 |
"""Return the name of the module being treated."""
|
|
252 |
|
|
253 |
return self.module['name']
|
|
254 |
|
|
255 |
|
|
256 |
# These inspect methods all rely on the inspect module.
|
|
257 |
def inspect(self, object):
|
|
258 |
"""Collect information about a given object."""
|
|
259 |
|
|
260 |
self.moduleSpace = object
|
|
261 |
|
|
262 |
# Very non-OO, left for later...
|
|
263 |
if inspect.ismodule(object):
|
|
264 |
self._inspectModule(object)
|
|
265 |
elif inspect.isclass(object):
|
|
266 |
self._inspectClass(object)
|
|
267 |
elif inspect.ismethod(object):
|
|
268 |
self._inspectMethod(object)
|
|
269 |
elif inspect.isfunction(object):
|
|
270 |
self._inspectFunction(object)
|
|
271 |
elif inspect.isbuiltin(object):
|
|
272 |
self._inspectBuiltin(object)
|
|
273 |
else:
|
|
274 |
msg = "Don't know how to document this kind of object."
|
3721
|
275 |
raise TypeError(msg)
|
2963
|
276 |
|
|
277 |
|
|
278 |
def _inspectModule(self, object):
|
|
279 |
"""Collect information about a given module object."""
|
|
280 |
name = object.__name__
|
|
281 |
|
|
282 |
self.module['name'] = name
|
|
283 |
if hasattr(object, '__version__'):
|
|
284 |
self.module['version'] = object.__version__
|
|
285 |
|
|
286 |
cadr = lambda list: list[1]
|
3721
|
287 |
modules = list(map(cadr, inspect.getmembers(object, inspect.ismodule)))
|
2963
|
288 |
|
|
289 |
classes, cdict = [], {}
|
|
290 |
for key, value in inspect.getmembers(object, inspect.isclass):
|
|
291 |
if (inspect.getmodule(value) or object) is object:
|
|
292 |
classes.append(value)
|
|
293 |
cdict[key] = cdict[value] = '#' + key
|
|
294 |
|
|
295 |
functions, fdict = [], {}
|
|
296 |
for key, value in inspect.getmembers(object, inspect.isroutine):
|
|
297 |
#if inspect.isbuiltin(value) or inspect.getmodule(value) is object:
|
|
298 |
functions.append(value)
|
|
299 |
fdict[key] = '#-' + key
|
|
300 |
if inspect.isfunction(value): fdict[value] = fdict[key]
|
|
301 |
|
|
302 |
for c in classes:
|
|
303 |
for base in c.__bases__:
|
|
304 |
key, modname = base.__name__, base.__module__
|
3326
|
305 |
if modname != name and modname in sys.modules:
|
2963
|
306 |
module = sys.modules[modname]
|
|
307 |
if hasattr(module, key) and getattr(module, key) is base:
|
3326
|
308 |
if key not in cdict:
|
2963
|
309 |
cdict[key] = cdict[base] = modname + '.txt#' + key
|
|
310 |
|
|
311 |
## doc = getdoc(object) or 'No doc string.'
|
|
312 |
doc = getdoc(object)
|
|
313 |
self.module['doc'] = doc
|
|
314 |
|
|
315 |
if modules:
|
3721
|
316 |
self.module['importedModules'] = [m.__name__ for m in modules]
|
2963
|
317 |
|
|
318 |
if classes:
|
|
319 |
for item in classes:
|
|
320 |
self._inspectClass(item, fdict, cdict)
|
|
321 |
|
|
322 |
if functions:
|
|
323 |
for item in functions:
|
|
324 |
self._inspectFunction(item, fdict, cdict)
|
|
325 |
|
|
326 |
|
|
327 |
def _inspectClass(self, object, functions={}, classes={}):
|
|
328 |
"""Collect information about a given class object."""
|
|
329 |
|
|
330 |
name = object.__name__
|
|
331 |
bases = object.__bases__
|
|
332 |
results = []
|
|
333 |
|
|
334 |
if bases:
|
|
335 |
parents = []
|
|
336 |
for base in bases:
|
|
337 |
parents.append(base)
|
|
338 |
|
|
339 |
self.classes[name] = {}
|
|
340 |
if bases:
|
|
341 |
self.classes[name]['bases'] = parents
|
|
342 |
|
|
343 |
methods, mdict = [], {}
|
|
344 |
for key, value in inspect.getmembers(object, inspect.ismethod):
|
|
345 |
methods.append(value)
|
|
346 |
mdict[key] = mdict[value] = '#' + name + '-' + key
|
|
347 |
|
|
348 |
if methods:
|
3326
|
349 |
if 'methods' not in self.classes[name]:
|
2963
|
350 |
self.classes[name]['methods'] = {}
|
|
351 |
for item in methods:
|
|
352 |
self._inspectMethod(item, functions, classes, mdict, name)
|
|
353 |
|
|
354 |
## doc = getdoc(object) or 'No doc string.'
|
|
355 |
doc = getdoc(object)
|
|
356 |
self.classes[name]['doc'] = doc
|
|
357 |
|
|
358 |
|
|
359 |
def _inspectMethod(self, object, functions={}, classes={}, methods={}, clname=''):
|
|
360 |
"""Collect information about a given method object."""
|
|
361 |
|
3721
|
362 |
self._inspectFunction(object.__func__, functions, classes, methods, clname)
|
2963
|
363 |
|
|
364 |
|
|
365 |
def _inspectFunction(self, object, functions={}, classes={}, methods={}, clname=''):
|
|
366 |
"""Collect information about a given function object."""
|
|
367 |
try:
|
|
368 |
args, varargs, varkw, defaults = inspect.getargspec(object)
|
|
369 |
argspec = inspect.formatargspec(
|
|
370 |
args, varargs, varkw, defaults,
|
|
371 |
defaultformat=defaultformat)
|
|
372 |
except TypeError:
|
|
373 |
argspec = '( ... )'
|
|
374 |
|
|
375 |
## doc = getdoc(object) or 'No doc string.'
|
|
376 |
doc = getdoc(object)
|
|
377 |
|
|
378 |
if object.__name__ == '<lambda>':
|
|
379 |
decl = [' lambda ', argspec[1:-1]]
|
|
380 |
# print ' %s' % decl
|
|
381 |
# Do something with lambda functions as well...
|
|
382 |
# ...
|
|
383 |
else:
|
|
384 |
decl = object.__name__
|
|
385 |
if not clname:
|
|
386 |
self.functions[object.__name__] = {'signature':argspec, 'doc':doc}
|
|
387 |
else:
|
|
388 |
theMethods = self.classes[clname]['methods']
|
3326
|
389 |
if object.__name__ not in theMethods:
|
2963
|
390 |
theMethods[object.__name__] = {}
|
|
391 |
|
|
392 |
theMethod = theMethods[object.__name__]
|
|
393 |
theMethod['signature'] = argspec
|
|
394 |
theMethod['doc'] = doc
|
|
395 |
|
|
396 |
|
|
397 |
def _inspectBuiltin(self, object):
|
|
398 |
"""Collect information about a given built-in."""
|
|
399 |
|
3721
|
400 |
print(object.__name__ + '( ... )')
|
2963
|
401 |
|
|
402 |
|
|
403 |
def walk(self, formatter):
|
|
404 |
"""Call event methods in a visiting formatter."""
|
|
405 |
|
|
406 |
s = self
|
|
407 |
f = formatter
|
|
408 |
|
|
409 |
# The order is fixed, but could be made flexible
|
|
410 |
# with one more template method...
|
|
411 |
|
|
412 |
# Module
|
|
413 |
modName = s.module['name']
|
|
414 |
modDoc = s.module['doc']
|
|
415 |
imported = s.module.get('importedModules', [])
|
|
416 |
imported.sort()
|
|
417 |
# f.indentLevel = f.indentLevel + 1
|
|
418 |
f.beginModule(modName, modDoc, imported)
|
|
419 |
|
|
420 |
# Classes
|
|
421 |
f.indentLevel = f.indentLevel + 1
|
3721
|
422 |
f.beginClasses(list(s.classes.keys()))
|
|
423 |
items = list(s.classes.items())
|
2963
|
424 |
items.sort()
|
|
425 |
for k, v in items:
|
|
426 |
cDoc = s.classes[k]['doc']
|
|
427 |
bases = s.classes[k].get('bases', [])
|
|
428 |
f.indentLevel = f.indentLevel + 1
|
|
429 |
f.beginClass(k, cDoc, bases)
|
|
430 |
|
|
431 |
# This if should move out of this method.
|
3326
|
432 |
if 'methods' not in s.classes[k]:
|
2963
|
433 |
s.classes[k]['methods'] = {}
|
|
434 |
|
|
435 |
# Methods
|
|
436 |
#f.indentLevel = f.indentLevel + 1
|
3721
|
437 |
f.beginMethods(list(s.classes[k]['methods'].keys()))
|
|
438 |
items = list(s.classes[k]['methods'].items())
|
2963
|
439 |
items.sort()
|
|
440 |
for m, v in items:
|
|
441 |
mDoc = v['doc']
|
|
442 |
sig = v['signature']
|
|
443 |
f.indentLevel = f.indentLevel + 1
|
|
444 |
f.beginMethod(m, mDoc, sig)
|
|
445 |
f.indentLevel = f.indentLevel - 1
|
|
446 |
f.endMethod(m, mDoc, sig)
|
|
447 |
|
|
448 |
#f.indentLevel = f.indentLevel - 1
|
3721
|
449 |
f.endMethods(list(s.classes[k]['methods'].keys()))
|
2963
|
450 |
|
|
451 |
f.indentLevel = f.indentLevel - 1
|
|
452 |
f.endClass(k, cDoc, bases)
|
|
453 |
|
|
454 |
# And what about attributes?!
|
|
455 |
|
|
456 |
f.indentLevel = f.indentLevel - 1
|
3721
|
457 |
f.endClasses(list(s.classes.keys()))
|
2963
|
458 |
|
|
459 |
# Functions
|
|
460 |
f.indentLevel = f.indentLevel + 1
|
3721
|
461 |
f.beginFunctions(list(s.functions.keys()))
|
|
462 |
items = list(s.functions.items())
|
2963
|
463 |
items.sort()
|
|
464 |
for k, v in items:
|
|
465 |
doc = v['doc']
|
|
466 |
sig = v['signature']
|
|
467 |
f.indentLevel = f.indentLevel + 1
|
|
468 |
f.beginFunction(k, doc, sig)
|
|
469 |
f.indentLevel = f.indentLevel - 1
|
|
470 |
f.endFunction(k, doc, sig)
|
|
471 |
f.indentLevel = f.indentLevel - 1
|
3721
|
472 |
f.endFunctions(list(s.functions.keys()))
|
2963
|
473 |
|
|
474 |
#f.indentLevel = f.indentLevel - 1
|
|
475 |
f.endModule(modName, modDoc, imported)
|
|
476 |
|
|
477 |
# Constants?!
|
|
478 |
|
|
479 |
|
|
480 |
####################################################################
|
|
481 |
#
|
|
482 |
# Core "standard" docpy document builders
|
|
483 |
#
|
|
484 |
####################################################################
|
|
485 |
|
|
486 |
class DocBuilder0:
|
|
487 |
"""An abstract class to document the skeleton of a Python module.
|
|
488 |
|
|
489 |
Instances take a skeleton instance s and call their s.walk()
|
|
490 |
method. The skeleton, in turn, will walk over its tree structure
|
|
491 |
while generating events and calling equivalent methods from a
|
|
492 |
specific interface (begin/end methods).
|
|
493 |
"""
|
|
494 |
|
|
495 |
fileSuffix = None
|
|
496 |
|
|
497 |
def __init__(self, skeleton=None):
|
|
498 |
self.skeleton = skeleton
|
|
499 |
self.packageName = None
|
|
500 |
self.indentLevel = 0
|
|
501 |
|
|
502 |
|
|
503 |
def write(self, skeleton=None):
|
|
504 |
if skeleton:
|
|
505 |
self.skeleton = skeleton
|
|
506 |
self.skeleton.walk(self)
|
|
507 |
|
|
508 |
|
|
509 |
# Event-method API, called by associated skeleton instances.
|
|
510 |
# In fact, these should raise a NotImplementedError, but for now we
|
|
511 |
# just don't do anything here.
|
|
512 |
|
|
513 |
# The following four methods are *not* called by skeletons!
|
|
514 |
def begin(self, name='', typ=''): pass
|
|
515 |
def end(self): pass
|
|
516 |
|
|
517 |
# Methods for packaging should move into a future PackageSkeleton...
|
|
518 |
def beginPackage(self, name):
|
|
519 |
self.packageName = name
|
|
520 |
|
|
521 |
def endPackage(self, name):
|
|
522 |
pass
|
|
523 |
|
|
524 |
# Only this subset is really called by associated skeleton instances.
|
|
525 |
|
|
526 |
def beginModule(self, name, doc, imported): pass
|
|
527 |
def endModule(self, name, doc, imported): pass
|
|
528 |
|
|
529 |
def beginClasses(self, names): pass
|
|
530 |
def endClasses(self, names): pass
|
|
531 |
|
|
532 |
def beginClass(self, name, doc, bases): pass
|
|
533 |
def endClass(self, name, doc, bases): pass
|
|
534 |
|
|
535 |
def beginMethods(self, names): pass
|
|
536 |
def endMethods(self, names): pass
|
|
537 |
|
|
538 |
def beginMethod(self, name, doc, sig): pass
|
|
539 |
def endMethod(self, name, doc, sig): pass
|
|
540 |
|
|
541 |
def beginFunctions(self, names): pass
|
|
542 |
def endFunctions(self, names): pass
|
|
543 |
|
|
544 |
def beginFunction(self, name, doc, sig): pass
|
|
545 |
def endFunction(self, name, doc, sig): pass
|
|
546 |
|
|
547 |
|
|
548 |
class AsciiDocBuilder0(DocBuilder0):
|
|
549 |
"""Document the skeleton of a Python module in ASCII format.
|
|
550 |
|
|
551 |
The output will be an ASCII file with nested lines representing
|
|
552 |
the hiearchical module structure.
|
|
553 |
|
|
554 |
Currently, no doc strings are listed.
|
|
555 |
"""
|
|
556 |
|
|
557 |
fileSuffix = '.txt'
|
|
558 |
outLines = []
|
|
559 |
indentLabel = ' '
|
|
560 |
|
|
561 |
def end(self):
|
|
562 |
# This if should move into DocBuilder0...
|
|
563 |
if self.packageName:
|
|
564 |
self.outPath = self.packageName + self.fileSuffix
|
|
565 |
elif self.skeleton:
|
|
566 |
self.outPath = self.skeleton.getModuleName() + self.fileSuffix
|
|
567 |
else:
|
|
568 |
self.outPath = ''
|
|
569 |
|
|
570 |
if self.outPath:
|
|
571 |
file = open(self.outPath, 'w')
|
|
572 |
for line in self.outLines:
|
|
573 |
file.write(line + '\n')
|
|
574 |
file.close()
|
|
575 |
|
|
576 |
|
|
577 |
def beginPackage(self, name):
|
|
578 |
DocBuilder0.beginPackage(self, name)
|
|
579 |
lev, label = self.indentLevel, self.indentLabel
|
|
580 |
self.outLines.append('%sPackage: %s' % (lev*label, name))
|
|
581 |
self.outLines.append('')
|
|
582 |
|
|
583 |
|
|
584 |
def beginModule(self, name, doc, imported):
|
|
585 |
append = self.outLines.append
|
|
586 |
lev, label = self.indentLevel, self.indentLabel
|
|
587 |
self.outLines.append('%sModule: %s' % (lev*label, name))
|
|
588 |
## self.outLines.append('%s%s' % ((lev+1)*label, reduceDocStringLength(doc)))
|
|
589 |
append('')
|
|
590 |
|
|
591 |
if imported:
|
|
592 |
self.outLines.append('%sImported' % ((lev+1)*label))
|
|
593 |
append('')
|
|
594 |
for m in imported:
|
|
595 |
self.outLines.append('%s%s' % ((lev+2)*label, m))
|
|
596 |
append('')
|
|
597 |
|
|
598 |
|
|
599 |
def beginClasses(self, names):
|
|
600 |
if names:
|
|
601 |
lev, label = self.indentLevel, self.indentLabel
|
|
602 |
self.outLines.append('%sClasses' % (lev*label))
|
|
603 |
self.outLines.append('')
|
|
604 |
|
|
605 |
|
|
606 |
def beginClass(self, name, doc, bases):
|
|
607 |
append = self.outLines.append
|
|
608 |
lev, label = self.indentLevel, self.indentLabel
|
|
609 |
|
|
610 |
if bases:
|
3721
|
611 |
bases = [b.__name__ for b in bases] # hack
|
3794
|
612 |
append('%s%s(%s)' % (lev*label, name, ', '.join(bases)))
|
2963
|
613 |
else:
|
|
614 |
append('%s%s' % (lev*label, name))
|
|
615 |
return
|
|
616 |
|
|
617 |
## append('%s%s' % ((lev+1)*label, reduceDocStringLength(doc)))
|
|
618 |
self.outLines.append('')
|
|
619 |
|
|
620 |
|
|
621 |
def endClass(self, name, doc, bases):
|
|
622 |
self.outLines.append('')
|
|
623 |
|
|
624 |
|
|
625 |
def beginMethod(self, name, doc, sig):
|
|
626 |
append = self.outLines.append
|
|
627 |
lev, label = self.indentLevel, self.indentLabel
|
|
628 |
append('%s%s%s' % (lev*label, name, sig))
|
|
629 |
## append('%s%s' % ((lev+1)*label, reduceDocStringLength(doc)))
|
|
630 |
## append('')
|
|
631 |
|
|
632 |
|
|
633 |
def beginFunctions(self, names):
|
|
634 |
if names:
|
|
635 |
lev, label = self.indentLevel, self.indentLabel
|
|
636 |
self.outLines.append('%sFunctions' % (lev*label))
|
|
637 |
self.outLines.append('')
|
|
638 |
|
|
639 |
|
|
640 |
def endFunctions(self, names):
|
|
641 |
self.outLines.append('')
|
|
642 |
|
|
643 |
|
|
644 |
def beginFunction(self, name, doc, sig):
|
|
645 |
append = self.outLines.append
|
|
646 |
lev, label = self.indentLevel, self.indentLabel
|
|
647 |
self.outLines.append('%s%s%s' % (lev*label, name, sig))
|
|
648 |
## append('%s%s' % ((lev+1)*label, reduceDocStringLength(doc)))
|
|
649 |
## append('')
|
|
650 |
|
|
651 |
|
|
652 |
class HtmlDocBuilder0(DocBuilder0):
|
|
653 |
"A class to write the skeleton of a Python source in HTML format."
|
|
654 |
|
|
655 |
fileSuffix = '.html'
|
|
656 |
outLines = []
|
|
657 |
|
|
658 |
def begin(self, name='', typ=''):
|
|
659 |
self.outLines.append("""<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">""")
|
|
660 |
self.outLines.append("""<html>""")
|
|
661 |
|
|
662 |
|
|
663 |
def end(self):
|
|
664 |
if self.packageName:
|
|
665 |
self.outPath = self.packageName + self.fileSuffix
|
|
666 |
elif self.skeleton:
|
|
667 |
self.outPath = self.skeleton.getModuleName() + self.fileSuffix
|
|
668 |
else:
|
|
669 |
self.outPath = ''
|
|
670 |
|
|
671 |
if self.outPath:
|
|
672 |
file = open(self.outPath, 'w')
|
|
673 |
self.outLines.append('</body></html>')
|
|
674 |
for line in self.outLines:
|
|
675 |
file.write(line + '\n')
|
|
676 |
file.close()
|
|
677 |
|
|
678 |
|
|
679 |
def beginPackage(self, name):
|
|
680 |
DocBuilder0.beginPackage(self, name)
|
|
681 |
|
|
682 |
self.outLines.append("""<title>%s</title>""" % name)
|
|
683 |
self.outLines.append("""<body bgcolor="#ffffff">""")
|
|
684 |
self.outLines.append("""<H1>%s</H1>""" % name)
|
|
685 |
self.outLines.append('')
|
|
686 |
|
|
687 |
|
|
688 |
def beginModule(self, name, doc, imported):
|
|
689 |
if not self.packageName:
|
|
690 |
self.outLines.append("""<title>%s</title>""" % name)
|
|
691 |
self.outLines.append("""<body bgcolor="#ffffff">""")
|
|
692 |
|
|
693 |
self.outLines.append("""<H1>%s</H1>""" % name)
|
|
694 |
self.outLines.append('')
|
3794
|
695 |
for line in doc.split('\n'):
|
2963
|
696 |
self.outLines.append("""<FONT SIZE="-1">%s</FONT>""" % htmlescape(line))
|
|
697 |
self.outLines.append('<BR>')
|
|
698 |
self.outLines.append('')
|
|
699 |
|
|
700 |
if imported:
|
|
701 |
self.outLines.append(makeHtmlSection('Imported Modules'))
|
|
702 |
self.outLines.append("""<ul>""")
|
|
703 |
for m in imported:
|
|
704 |
self.outLines.append("""<li>%s</li>""" % m)
|
|
705 |
self.outLines.append("""</ul>""")
|
|
706 |
|
|
707 |
|
|
708 |
def beginClasses(self, names):
|
|
709 |
self.outLines.append(makeHtmlSection('Classes'))
|
|
710 |
|
|
711 |
|
|
712 |
def beginClass(self, name, doc, bases):
|
|
713 |
DocBuilder0.beginClass(self, name, doc, bases)
|
|
714 |
|
|
715 |
## # Keep an eye on the base classes.
|
|
716 |
## self.currentBaseClasses = bases
|
|
717 |
|
|
718 |
if bases:
|
3721
|
719 |
bases = [b.__name__ for b in bases] # hack
|
3794
|
720 |
self.outLines.append(makeHtmlSubSection('%s(%s)' % (name, ', '.join(bases))))
|
2963
|
721 |
else:
|
|
722 |
self.outLines.append(makeHtmlSubSection('%s' % name))
|
3794
|
723 |
for line in doc.split('\n'):
|
2963
|
724 |
self.outLines.append("""<FONT SIZE="-1">%s</FONT>""" % htmlescape(line))
|
|
725 |
self.outLines.append('<BR>')
|
|
726 |
|
|
727 |
self.outLines.append('')
|
|
728 |
|
|
729 |
|
|
730 |
def beginMethods(self, names):
|
|
731 |
pass
|
|
732 |
## if names:
|
|
733 |
## self.outLines.append('<H3>Method Interface</H3>')
|
|
734 |
## self.outLines.append('')
|
|
735 |
|
|
736 |
|
|
737 |
def beginMethod(self, name, doc, sig):
|
|
738 |
self.beginFunction(name, doc, sig)
|
|
739 |
|
|
740 |
|
|
741 |
def beginFunctions(self, names):
|
|
742 |
self.outLines.append(makeHtmlSection('Functions'))
|
|
743 |
|
|
744 |
|
|
745 |
def beginFunction(self, name, doc, sig):
|
|
746 |
append = self.outLines.append
|
|
747 |
append("""<DL><DL><DT><TT><STRONG>%s</STRONG>%s</TT></DT>""" % (name, sig))
|
|
748 |
append('')
|
3794
|
749 |
for line in doc.split('\n'):
|
2963
|
750 |
append("""<DD><FONT SIZE="-1">%s</FONT></DD>""" % htmlescape(line))
|
|
751 |
append('<BR>')
|
|
752 |
append('</DL></DL>')
|
|
753 |
append('')
|
|
754 |
|
|
755 |
|
|
756 |
class PdfDocBuilder0(DocBuilder0):
|
|
757 |
"Document the skeleton of a Python module in PDF format."
|
|
758 |
|
|
759 |
fileSuffix = '.pdf'
|
|
760 |
|
|
761 |
def makeHeadingStyle(self, level, typ=None, doc=''):
|
|
762 |
"Make a heading style for different types of module content."
|
|
763 |
|
|
764 |
if typ in ('package', 'module', 'class'):
|
|
765 |
style = ParagraphStyle(name='Heading'+str(level),
|
|
766 |
fontName = 'Courier-Bold',
|
|
767 |
fontSize=14,
|
|
768 |
leading=18,
|
|
769 |
spaceBefore=12,
|
|
770 |
spaceAfter=6)
|
|
771 |
elif typ in ('method', 'function'):
|
|
772 |
if doc:
|
|
773 |
style = ParagraphStyle(name='Heading'+str(level),
|
|
774 |
fontName = 'Courier-Bold',
|
|
775 |
fontSize=12,
|
|
776 |
leading=18,
|
|
777 |
firstLineIndent=-18,
|
|
778 |
leftIndent=36,
|
|
779 |
spaceBefore=0,
|
|
780 |
spaceAfter=-3)
|
|
781 |
else:
|
|
782 |
style = ParagraphStyle(name='Heading'+str(level),
|
|
783 |
fontName = 'Courier-Bold',
|
|
784 |
fontSize=12,
|
|
785 |
leading=18,
|
|
786 |
firstLineIndent=-18,
|
|
787 |
leftIndent=36,
|
|
788 |
spaceBefore=0,
|
|
789 |
spaceAfter=0)
|
|
790 |
|
|
791 |
else:
|
|
792 |
style = ParagraphStyle(name='Heading'+str(level),
|
|
793 |
fontName = 'Times-Bold',
|
|
794 |
fontSize=14,
|
|
795 |
leading=18,
|
|
796 |
spaceBefore=12,
|
|
797 |
spaceAfter=6)
|
|
798 |
|
|
799 |
return style
|
|
800 |
|
|
801 |
|
|
802 |
def begin(self, name='', typ=''):
|
|
803 |
styleSheet = getSampleStyleSheet()
|
|
804 |
self.code = styleSheet['Code']
|
|
805 |
self.bt = styleSheet['BodyText']
|
|
806 |
self.story = []
|
|
807 |
|
|
808 |
# Cover page
|
|
809 |
t = time.gmtime(time.time())
|
|
810 |
timeString = time.strftime("%Y-%m-%d %H:%M", t)
|
|
811 |
self.story.append(Paragraph('<font size=18>Documentation for %s "%s"</font>' % (typ, name), self.bt))
|
|
812 |
self.story.append(Paragraph('<font size=18>Generated by: docpy.py version %s</font>' % __version__, self.bt))
|
|
813 |
self.story.append(Paragraph('<font size=18>Date generated: %s</font>' % timeString, self.bt))
|
|
814 |
self.story.append(Paragraph('<font size=18>Format: PDF</font>', self.bt))
|
|
815 |
self.story.append(PageBreak())
|
|
816 |
|
|
817 |
# Table of contents
|
|
818 |
toc = TableOfContents()
|
|
819 |
self.story.append(toc)
|
|
820 |
self.story.append(PageBreak())
|
|
821 |
|
|
822 |
|
|
823 |
def end(self):
|
|
824 |
if self.outPath is not None:
|
|
825 |
pass
|
|
826 |
elif self.packageName:
|
|
827 |
self.outPath = self.packageName + self.fileSuffix
|
|
828 |
elif self.skeleton:
|
|
829 |
self.outPath = self.skeleton.getModuleName() + self.fileSuffix
|
|
830 |
else:
|
|
831 |
self.outPath = ''
|
3721
|
832 |
print('output path is %s' % self.outPath)
|
2963
|
833 |
if self.outPath:
|
|
834 |
doc = MyTemplate(self.outPath)
|
|
835 |
doc.multiBuild(self.story)
|
|
836 |
|
|
837 |
|
|
838 |
def beginPackage(self, name):
|
|
839 |
DocBuilder0.beginPackage(self, name)
|
|
840 |
story = self.story
|
|
841 |
story.append(Paragraph(name, self.makeHeadingStyle(self.indentLevel, 'package')))
|
|
842 |
|
|
843 |
|
|
844 |
def beginModule(self, name, doc, imported):
|
|
845 |
story = self.story
|
|
846 |
bt = self.bt
|
|
847 |
story.append(Paragraph(name, self.makeHeadingStyle(self.indentLevel, 'module')))
|
|
848 |
if doc:
|
|
849 |
story.append(XPreformatted(htmlescape(doc), bt))
|
|
850 |
story.append(XPreformatted('', bt))
|
|
851 |
|
|
852 |
if imported:
|
|
853 |
story.append(Paragraph('Imported modules', self.makeHeadingStyle(self.indentLevel + 1)))
|
|
854 |
for m in imported:
|
|
855 |
p = Paragraph('<bullet>\201</bullet> %s' % m, bt)
|
|
856 |
p.style.bulletIndent = 10
|
|
857 |
p.style.leftIndent = 18
|
|
858 |
story.append(p)
|
|
859 |
|
|
860 |
|
|
861 |
def endModule(self, name, doc, imported):
|
|
862 |
DocBuilder0.endModule(self, name, doc, imported)
|
|
863 |
self.story.append(PageBreak())
|
|
864 |
|
|
865 |
|
|
866 |
def beginClasses(self, names):
|
|
867 |
self.story.append(Paragraph('Classes', self.makeHeadingStyle(self.indentLevel)))
|
|
868 |
|
|
869 |
|
|
870 |
def beginClass(self, name, doc, bases):
|
|
871 |
bt = self.bt
|
|
872 |
story = self.story
|
|
873 |
if bases:
|
3721
|
874 |
bases = [b.__name__ for b in bases] # hack
|
3794
|
875 |
story.append(Paragraph('%s(%s)' % (name,', '.join(bases)), self.makeHeadingStyle(self.indentLevel, 'class')))
|
2963
|
876 |
else:
|
|
877 |
story.append(Paragraph(name, self.makeHeadingStyle(self.indentLevel, 'class')))
|
|
878 |
|
|
879 |
if doc:
|
|
880 |
story.append(XPreformatted(htmlescape(doc), bt))
|
|
881 |
story.append(XPreformatted('', bt))
|
|
882 |
|
|
883 |
|
|
884 |
def beginMethod(self, name, doc, sig):
|
|
885 |
bt = self.bt
|
|
886 |
story = self.story
|
|
887 |
story.append(Paragraph(name+sig, self.makeHeadingStyle(self.indentLevel, 'method', doc)))
|
|
888 |
if doc:
|
|
889 |
story.append(XPreformatted(htmlescape(doc), bt))
|
|
890 |
story.append(XPreformatted('', bt))
|
|
891 |
|
|
892 |
|
|
893 |
def beginFunctions(self, names):
|
|
894 |
if names:
|
|
895 |
self.story.append(Paragraph('Functions', self.makeHeadingStyle(self.indentLevel)))
|
|
896 |
|
|
897 |
|
|
898 |
def beginFunction(self, name, doc, sig):
|
|
899 |
bt = self.bt
|
|
900 |
story = self.story
|
|
901 |
story.append(Paragraph(name+sig, self.makeHeadingStyle(self.indentLevel, 'function')))
|
|
902 |
if doc:
|
|
903 |
story.append(XPreformatted(htmlescape(doc), bt))
|
|
904 |
story.append(XPreformatted('', bt))
|
|
905 |
|
|
906 |
|
|
907 |
class UmlPdfDocBuilder0(PdfDocBuilder0):
|
|
908 |
"Document the skeleton of a Python module with UML class diagrams."
|
|
909 |
|
|
910 |
fileSuffix = '.pdf'
|
|
911 |
|
|
912 |
def begin(self, name='', typ=''):
|
|
913 |
styleSheet = getSampleStyleSheet()
|
|
914 |
self.h1 = styleSheet['Heading1']
|
|
915 |
self.h2 = styleSheet['Heading2']
|
|
916 |
self.h3 = styleSheet['Heading3']
|
|
917 |
self.code = styleSheet['Code']
|
|
918 |
self.bt = styleSheet['BodyText']
|
|
919 |
self.story = []
|
|
920 |
self.classCompartment = ''
|
|
921 |
self.methodCompartment = []
|
|
922 |
|
|
923 |
|
|
924 |
def beginModule(self, name, doc, imported):
|
|
925 |
story = self.story
|
|
926 |
h1, h2, h3, bt = self.h1, self.h2, self.h3, self.bt
|
|
927 |
styleSheet = getSampleStyleSheet()
|
|
928 |
bt1 = styleSheet['BodyText']
|
|
929 |
|
|
930 |
story.append(Paragraph(name, h1))
|
|
931 |
story.append(XPreformatted(doc, bt1))
|
|
932 |
|
|
933 |
if imported:
|
|
934 |
story.append(Paragraph('Imported modules', self.makeHeadingStyle(self.indentLevel + 1)))
|
|
935 |
for m in imported:
|
|
936 |
p = Paragraph('<bullet>\201</bullet> %s' % m, bt1)
|
|
937 |
p.style.bulletIndent = 10
|
|
938 |
p.style.leftIndent = 18
|
|
939 |
story.append(p)
|
|
940 |
|
|
941 |
|
|
942 |
def endModule(self, name, doc, imported):
|
|
943 |
self.story.append(PageBreak())
|
|
944 |
PdfDocBuilder0.endModule(self, name, doc, imported)
|
|
945 |
|
|
946 |
|
|
947 |
def beginClasses(self, names):
|
|
948 |
h1, h2, h3, bt = self.h1, self.h2, self.h3, self.bt
|
|
949 |
if names:
|
|
950 |
self.story.append(Paragraph('Classes', h2))
|
|
951 |
|
|
952 |
|
|
953 |
def beginClass(self, name, doc, bases):
|
|
954 |
self.classCompartment = ''
|
|
955 |
self.methodCompartment = []
|
|
956 |
|
|
957 |
if bases:
|
3721
|
958 |
bases = [b.__name__ for b in bases] # hack
|
3794
|
959 |
self.classCompartment = '%s(%s)' % (name, ', '.join(bases))
|
2963
|
960 |
else:
|
|
961 |
self.classCompartment = name
|
|
962 |
|
|
963 |
|
|
964 |
def endClass(self, name, doc, bases):
|
|
965 |
h1, h2, h3, bt, code = self.h1, self.h2, self.h3, self.bt, self.code
|
|
966 |
styleSheet = getSampleStyleSheet()
|
|
967 |
bt1 = styleSheet['BodyText']
|
|
968 |
story = self.story
|
|
969 |
|
|
970 |
# Use only the first line of the class' doc string --
|
|
971 |
# no matter how long! (Do the same later for methods)
|
|
972 |
classDoc = reduceDocStringLength(doc)
|
|
973 |
|
|
974 |
tsa = tableStyleAttributes = []
|
|
975 |
|
|
976 |
# Make table with class and method rows
|
|
977 |
# and add it to the story.
|
|
978 |
p = Paragraph('<b>%s</b>' % self.classCompartment, bt)
|
|
979 |
p.style.alignment = TA_CENTER
|
|
980 |
rows = [(p,)]
|
|
981 |
# No doc strings, now...
|
|
982 |
# rows = rows + [(Paragraph('<i>%s</i>' % classDoc, bt1),)]
|
|
983 |
lenRows = len(rows)
|
|
984 |
tsa.append(('BOX', (0,0), (-1,lenRows-1), 0.25, colors.black))
|
|
985 |
for name, doc, sig in self.methodCompartment:
|
|
986 |
nameAndSig = Paragraph('<b>%s</b>%s' % (name, sig), bt1)
|
|
987 |
rows.append((nameAndSig,))
|
|
988 |
# No doc strings, now...
|
|
989 |
# docStr = Paragraph('<i>%s</i>' % reduceDocStringLength(doc), bt1)
|
|
990 |
# rows.append((docStr,))
|
|
991 |
tsa.append(('BOX', (0,lenRows), (-1,-1), 0.25, colors.black))
|
|
992 |
t = Table(rows, (12*cm,))
|
|
993 |
tableStyle = TableStyle(tableStyleAttributes)
|
|
994 |
t.setStyle(tableStyle)
|
|
995 |
self.story.append(t)
|
|
996 |
self.story.append(Spacer(1*cm, 1*cm))
|
|
997 |
|
|
998 |
|
|
999 |
def beginMethod(self, name, doc, sig):
|
|
1000 |
self.methodCompartment.append((name, doc, sig))
|
|
1001 |
|
|
1002 |
|
|
1003 |
def beginFunctions(self, names):
|
|
1004 |
h1, h2, h3, bt = self.h1, self.h2, self.h3, self.bt
|
|
1005 |
if names:
|
|
1006 |
self.story.append(Paragraph('Functions', h2))
|
|
1007 |
self.classCompartment = chr(171) + ' Module-Level Functions ' + chr(187)
|
|
1008 |
self.methodCompartment = []
|
|
1009 |
|
|
1010 |
|
|
1011 |
def beginFunction(self, name, doc, sig):
|
|
1012 |
self.methodCompartment.append((name, doc, sig))
|
|
1013 |
|
|
1014 |
|
|
1015 |
def endFunctions(self, names):
|
|
1016 |
h1, h2, h3, bt, code = self.h1, self.h2, self.h3, self.bt, self.code
|
|
1017 |
styleSheet = getSampleStyleSheet()
|
|
1018 |
bt1 = styleSheet['BodyText']
|
|
1019 |
story = self.story
|
|
1020 |
if not names:
|
|
1021 |
return
|
|
1022 |
|
|
1023 |
tsa = tableStyleAttributes = []
|
|
1024 |
|
|
1025 |
# Make table with class and method rows
|
|
1026 |
# and add it to the story.
|
|
1027 |
p = Paragraph('<b>%s</b>' % self.classCompartment, bt)
|
|
1028 |
p.style.alignment = TA_CENTER
|
|
1029 |
rows = [(p,)]
|
|
1030 |
lenRows = len(rows)
|
|
1031 |
tsa.append(('BOX', (0,0), (-1,lenRows-1), 0.25, colors.black))
|
|
1032 |
for name, doc, sig in self.methodCompartment:
|
|
1033 |
nameAndSig = Paragraph('<b>%s</b>%s' % (name, sig), bt1)
|
|
1034 |
rows.append((nameAndSig,))
|
|
1035 |
# No doc strings, now...
|
|
1036 |
# docStr = Paragraph('<i>%s</i>' % reduceDocStringLength(doc), bt1)
|
|
1037 |
# rows.append((docStr,))
|
|
1038 |
tsa.append(('BOX', (0,lenRows), (-1,-1), 0.25, colors.black))
|
|
1039 |
t = Table(rows, (12*cm,))
|
|
1040 |
tableStyle = TableStyle(tableStyleAttributes)
|
|
1041 |
t.setStyle(tableStyle)
|
|
1042 |
self.story.append(t)
|
|
1043 |
self.story.append(Spacer(1*cm, 1*cm))
|
|
1044 |
|
|
1045 |
|
|
1046 |
####################################################################
|
|
1047 |
#
|
|
1048 |
# Main
|
|
1049 |
#
|
|
1050 |
####################################################################
|
|
1051 |
|
|
1052 |
def printUsage():
|
|
1053 |
"""docpy.py - Automated documentation for Python source code.
|
|
1054 |
|
|
1055 |
Usage: python docpy.py [options]
|
|
1056 |
|
|
1057 |
[options]
|
|
1058 |
-h Print this help message.
|
|
1059 |
|
|
1060 |
-f name Use the document builder indicated by 'name',
|
|
1061 |
e.g. Ascii, Html, Pdf (default), UmlPdf.
|
|
1062 |
|
|
1063 |
-m module Generate document for module named 'module'
|
|
1064 |
(default is 'docpy').
|
|
1065 |
'module' may follow any of these forms:
|
|
1066 |
- docpy.py
|
|
1067 |
- docpy
|
|
1068 |
- c:\\test\\docpy
|
|
1069 |
and can be any of these:
|
|
1070 |
- standard Python modules
|
|
1071 |
- modules in the Python search path
|
|
1072 |
- modules in the current directory
|
|
1073 |
|
|
1074 |
-p package Generate document for package named 'package'.
|
|
1075 |
'package' may follow any of these forms:
|
|
1076 |
- reportlab
|
|
1077 |
- reportlab.platypus
|
|
1078 |
- c:\\test\\reportlab
|
|
1079 |
and can be any of these:
|
|
1080 |
- standard Python packages (?)
|
|
1081 |
- packages in the Python search path
|
|
1082 |
- packages in the current directory
|
|
1083 |
|
|
1084 |
-s Silent mode (default is unset).
|
|
1085 |
|
|
1086 |
Examples:
|
|
1087 |
|
|
1088 |
python docpy.py -h
|
|
1089 |
python docpy.py -m docpy.py -f Ascii
|
|
1090 |
python docpy.py -m string -f Html
|
|
1091 |
python docpy.py -m signsandsymbols.py -f Pdf
|
|
1092 |
python docpy.py -p reportlab.platypus -f UmlPdf
|
|
1093 |
python docpy.py -p reportlab.lib -s -f UmlPdf
|
|
1094 |
"""
|
|
1095 |
|
|
1096 |
|
|
1097 |
def documentModule0(pathOrName, builder, opts={}):
|
|
1098 |
"""Generate documentation for one Python file in some format.
|
|
1099 |
|
|
1100 |
This handles Python standard modules like string, custom modules
|
|
1101 |
on the Python search path like e.g. docpy as well as modules
|
|
1102 |
specified with their full path like C:/tmp/junk.py.
|
|
1103 |
|
|
1104 |
The doc file will always be saved in the current directory with
|
|
1105 |
a basename equal to that of the module, e.g. docpy.
|
|
1106 |
"""
|
|
1107 |
|
|
1108 |
cwd = os.getcwd()
|
|
1109 |
|
|
1110 |
# Append directory to Python search path if we get one.
|
|
1111 |
dirName = os.path.dirname(pathOrName)
|
|
1112 |
if dirName:
|
|
1113 |
sys.path.append(dirName)
|
|
1114 |
|
|
1115 |
# Remove .py extension from module name.
|
|
1116 |
if pathOrName[-3:] == '.py':
|
|
1117 |
modname = pathOrName[:-3]
|
|
1118 |
else:
|
|
1119 |
modname = pathOrName
|
|
1120 |
|
|
1121 |
# Remove directory paths from module name.
|
|
1122 |
if dirName:
|
|
1123 |
modname = os.path.basename(modname)
|
|
1124 |
|
|
1125 |
# Load the module.
|
|
1126 |
try:
|
|
1127 |
module = __import__(modname)
|
|
1128 |
except:
|
3721
|
1129 |
print('Failed to import %s.' % modname)
|
2963
|
1130 |
os.chdir(cwd)
|
|
1131 |
return
|
|
1132 |
|
|
1133 |
# Do the real documentation work.
|
|
1134 |
s = ModuleSkeleton0()
|
|
1135 |
s.inspect(module)
|
|
1136 |
builder.write(s)
|
|
1137 |
|
|
1138 |
# Remove appended directory from Python search path if we got one.
|
|
1139 |
if dirName:
|
|
1140 |
del sys.path[-1]
|
|
1141 |
|
|
1142 |
os.chdir(cwd)
|
|
1143 |
|
|
1144 |
|
3721
|
1145 |
def _packageWalkCallback(xxx_todo_changeme, dirPath, files):
|
2963
|
1146 |
"A callback function used when waking over a package tree."
|
3721
|
1147 |
(builder, opts) = xxx_todo_changeme
|
|
1148 |
files = [f for f in files if f != '__init__.py']
|
2963
|
1149 |
|
3721
|
1150 |
files = [f for f in files if f[-3:] == '.py']
|
2963
|
1151 |
for f in files:
|
|
1152 |
path = os.path.join(dirPath, f)
|
|
1153 |
if not opts.get('isSilent', 0):
|
3721
|
1154 |
print(path)
|
2963
|
1155 |
builder.indentLevel = builder.indentLevel + 1
|
|
1156 |
documentModule0(path, builder)
|
|
1157 |
builder.indentLevel = builder.indentLevel - 1
|
|
1158 |
|
|
1159 |
|
|
1160 |
def documentPackage0(pathOrName, builder, opts={}):
|
|
1161 |
"""Generate documentation for one Python package in some format.
|
|
1162 |
|
|
1163 |
'pathOrName' can be either a filesystem path leading to a Python
|
|
1164 |
package or package name whose path will be resolved by importing
|
|
1165 |
the top-level module.
|
|
1166 |
|
|
1167 |
The doc file will always be saved in the current directory with
|
|
1168 |
a basename equal to that of the package, e.g. reportlab.lib.
|
|
1169 |
"""
|
|
1170 |
|
|
1171 |
# Did we get a package path with OS-dependant seperators...?
|
|
1172 |
if os.sep in pathOrName:
|
|
1173 |
path = pathOrName
|
|
1174 |
name = os.path.splitext(os.path.basename(path))[0]
|
|
1175 |
# ... or rather a package name?
|
|
1176 |
else:
|
|
1177 |
name = pathOrName
|
|
1178 |
package = __import__(name)
|
|
1179 |
# Some special care needed for dotted names.
|
|
1180 |
if '.' in name:
|
3794
|
1181 |
subname = 'package' + name[name.find('.'):]
|
2963
|
1182 |
package = eval(subname)
|
|
1183 |
path = os.path.dirname(package.__file__)
|
|
1184 |
|
|
1185 |
cwd = os.getcwd()
|
|
1186 |
builder.beginPackage(name)
|
|
1187 |
os.path.walk(path, _packageWalkCallback, (builder, opts))
|
|
1188 |
builder.endPackage(name)
|
|
1189 |
os.chdir(cwd)
|
|
1190 |
|
|
1191 |
|
|
1192 |
def main():
|
|
1193 |
"Handle command-line options and trigger corresponding action."
|
|
1194 |
|
|
1195 |
opts, args = getopt.getopt(sys.argv[1:], 'hsf:m:p:')
|
|
1196 |
|
|
1197 |
# Make an options dictionary that is easier to use.
|
|
1198 |
optsDict = {}
|
|
1199 |
for k, v in opts:
|
|
1200 |
optsDict[k] = v
|
3326
|
1201 |
hasOpt = optsDict.__contains__
|
2963
|
1202 |
|
|
1203 |
# On -h print usage and exit immediately.
|
|
1204 |
if hasOpt('-h'):
|
3721
|
1205 |
print(printUsage.__doc__)
|
2963
|
1206 |
sys.exit(0)
|
|
1207 |
|
|
1208 |
# On -s set silent mode.
|
|
1209 |
isSilent = hasOpt('-s')
|
|
1210 |
|
|
1211 |
# On -f set the appropriate DocBuilder to use or a default one.
|
|
1212 |
builderClassName = optsDict.get('-f', 'Pdf') + 'DocBuilder0'
|
|
1213 |
builder = eval(builderClassName + '()')
|
|
1214 |
|
|
1215 |
# Set default module or package to document.
|
|
1216 |
if not hasOpt('-p') and not hasOpt('-m'):
|
|
1217 |
optsDict['-m'] = 'docpy'
|
|
1218 |
|
|
1219 |
# Save a few options for further use.
|
|
1220 |
options = {'isSilent':isSilent}
|
|
1221 |
|
|
1222 |
# Now call the real documentation functions.
|
|
1223 |
if hasOpt('-m'):
|
|
1224 |
nameOrPath = optsDict['-m']
|
|
1225 |
if not isSilent:
|
3721
|
1226 |
print("Generating documentation for module %s..." % nameOrPath)
|
2963
|
1227 |
builder.begin(name=nameOrPath, typ='module')
|
|
1228 |
documentModule0(nameOrPath, builder, options)
|
|
1229 |
elif hasOpt('-p'):
|
|
1230 |
nameOrPath = optsDict['-p']
|
|
1231 |
if not isSilent:
|
3721
|
1232 |
print("Generating documentation for package %s..." % nameOrPath)
|
2963
|
1233 |
builder.begin(name=nameOrPath, typ='package')
|
|
1234 |
documentPackage0(nameOrPath, builder, options)
|
|
1235 |
builder.end()
|
|
1236 |
|
|
1237 |
if not isSilent:
|
3721
|
1238 |
print("Saved %s." % builder.outPath)
|
2963
|
1239 |
|
|
1240 |
|
|
1241 |
if __name__ == '__main__':
|
|
1242 |
main()
|