summaryrefslogtreecommitdiff
path: root/sandbox/dkuhlman/Extract/extract_doc.py
blob: 2ac742de826947629f2bb7c40c77cdba06ca2258 (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/usr/bin/env python

import sys
import string
import time
import getopt
import textwrap
import traceback
import inspect
import __builtin__

from pydoc import *


TARGET_rest = 1
TARGET_latex = 2
UseOverTitleAdornment = 0
TitleAdornmentChars_noover = '=-.,^+~:;_"\''
TitleAdornmentChars_over = '==--..,,^^++~~'
TitleAdornmentChars = TitleAdornmentChars_noover


## class Doc:
##     def document(self, thing, name=None, *args):
##         """Generate documentation for an object."""
##         args = (thing, name) + args
##         # 'try' clause is to attempt to handle the possibility that inspect
##         # identifies something in a way that pydoc itself has issues handling;
##         # think 'super' and how it is a descriptor (which raises the exception
##         # by lacking a __name__ attribute) and an instance.
## ##         try:
##         if inspect.ismodule(thing): 
##             result = self.docmodule(*args)
##             return result
##         if inspect.isclass(thing):
##             result = self.docclass(*args)
##             return result
##         if inspect.isroutine(thing):
##             result = self.docroutine(*args)
##             return result
## ##         except AttributeError:
## ##             traceback.print_last()
## ##             pass
##         result = self.docother(*args)
##         return result
## 
##     def fail(self, thing, name=None, *args):
##         """Raise an exception for unimplemented types."""
##         message = "don't know how to document thing%s of type %s" % (
##             name and ' ' + repr(name), type(thing).__name__)
##         raise TypeError, message
## 
##     docmodule = docclass = docroutine = docother = fail


class ReSTDoc(Doc):
    """Formatter class for reStructuredText documentation."""
    
    def __init__(self):
        self.lines = []
        self.titleLevel = 0

    _repr_instance = TextRepr()
    repr = _repr_instance.repr

    def push(self, text=''):
        """Push one line of output onto the collected output."""
        self.lines.append(text)

    def get_lines(self):
        """Get the accumulated lines of output."""
        return self.lines

    def emphasize(self, text):
        """Add emphasis to a piece of text."""
        text1 = '*%s*' % text
        return text1

    def formatvalue(self, thing):
        """Format an argument default value as text."""
        return '=' + self.repr(thing)

    def escape(self, text):
        """Escape special reStructuredText characters."""
        text1 = text.replace('*', '\\*')
        return text1

    def genTitle(self, text):
        """Generate a title according to the current title level."""
        adornment = TitleAdornmentChars[self.titleLevel]
        # If it's an even numbered title level, then generate
        # adornment above the title.
        if UseOverTitleAdornment:
            if ((self.titleLevel / 2) * 2) == self.titleLevel:
                self.push(adornment * len(text))
        self.push(text)
        self.push(adornment * len(text))
        self.push()
        
    def incTitleLevel(self):
        self.titleLevel += 1

    def decTitleLevel(self):
        self.titleLevel -= 1

    def docmodule(self, thing, name=None, mod=None):
        """Produce text documentation for a given module object."""
        # Ignore the passed-in name.
        name = thing.__name__
        synop, desc = splitdoc(getdoc(thing))
        self.genTitle('SYNOPSIS')
        if synop:
            line = '%s -- %s' % (name, synop)
            line = textwrap.fill(line, 68)
        else:
            line = name
        self.push(line)
        self.push()
        self.genTitle('DESCRIPTION')
        if desc:
            self.push(desc)
        else:
            self.push('No description for this module')
        self.push()
        classes = []
        for key, value in inspect.getmembers(thing, inspect.isclass):
            if (inspect.getmodule(value) or thing) is thing:
                if visiblename(key):
                    classes.append((key, value))
        funcs = []
        for key, value in inspect.getmembers(thing, inspect.isroutine):
            if inspect.isbuiltin(value) or inspect.getmodule(value) is thing:
                if visiblename(key):
                    funcs.append((key, value))
        data = []
        for key, value in inspect.getmembers(thing, isdata):
            if visiblename(key):
                data.append((key, value))
        self.genTitle('CLASSES')
        self.incTitleLevel()
        if classes:
            for key, value in classes:
                self.document(value, key, name)
        else:
            self.push('No classes in this module')
            self.push()
        self.decTitleLevel()
        self.genTitle('FUNCTIONS')
        self.incTitleLevel()
        if funcs:
            for key, value in funcs:
                self.document(value, key, name)
        else:
            self.push('No global functions in this module')
            self.push()
        self.decTitleLevel()
        self.genTitle('DATA')
        self.incTitleLevel()
        if data:
            for key, value in data:
                self.document(value, key, name)
        else:
            self.push('No global data in this module')
            self.push()
        self.decTitleLevel()
        if hasattr(thing, '__version__'):
            version = str(thing.__version__)
            if version[:11] == '$' + 'Revision: ' and version[-1:] == '$':
                version = strip(version[11:-1])
            self.genTitle('VERSION')
            self.push(version)
            self.push()
        if hasattr(thing, '__date__'):
            self.genTitle('DATE')
            self.push(thing.__date__)
            self.push()
        if hasattr(thing, '__author__'):
            self.genTitle('AUTHOR')
            self.push(thing.__author__)
            self.push()
        if hasattr(thing, '__credits__'):
            self.genTitle('CREDITS')
            self.push(thing.__credits__)
            self.push()

    def genlevel(self, level):
        pad = '  ' * level
        return pad
    
    def genbases(self, obj):
        level = 0
        self.genbases_aux(obj, level)
    
    def genbases_aux(self, obj, level):
        bases = obj.__bases__
        for base in bases:
            pad = self.genlevel(level)
            self.push('%s- %s' % (pad, base.__name__))
            self.push()
            self.genbases_aux(base, level + 1)

    def docclass(self, thing, name=None, mod=None):
        """Produce text documentation for a given class object."""
        realname = thing.__name__
        name = name or realname
        self.genTitle('class %s' % name)
        self.incTitleLevel()
        docstr = getdoc(thing)
        self.genTitle('Description')
        if docstr:
            self.push(docstr)
        else:
            self.push('No documentation for this class.')
        self.push()
        self.genTitle('Base Classes')
        if thing.__bases__:
            self.genbases(thing)
        else:
            self.push('No superclasses for this class.')
        self.push()
        attrs = filter(lambda (name, kind, cls, value): visiblename(name),
            inspect.classify_class_attrs(thing))
        self.genTitle('Methods')
        self.incTitleLevel()
        found = 0
        for attr in attrs:
            if attr[1] == 'method':
                self.document(attr[3], attr[0])
                found = 1
        if not found:
            self.push('No methods for this class')
        self.decTitleLevel()
        self.genTitle('Data')
        self.incTitleLevel()
        found = 0
        for attr in attrs:
            if attr[1] == 'data':
                self.document(attr[3], attr[0])
                found = 1
        if not found:
            self.push('No data for this class')
            self.push()
        self.decTitleLevel()
        self.decTitleLevel()

    def docroutine(self, thing, name=None, mod=None, cl=None):
        """Produce text documentation for a function or method object."""
        realname = thing.__name__
        name = name or realname
        note = ''
        skipdocs = 0
        if inspect.ismethod(thing):
            imclass = thing.im_class
            if cl:
                if imclass is not cl:
                    note = 'from ' + classname(imclass, mod)
            else:
                if thing.im_self:
                    note = 'method of %s instance' % classname(
                        thing.im_self.__class__, mod)
                else:
                    note = 'unbound %s method' % classname(imclass,mod)
            thing = thing.im_func
        if name == realname:
            title = self.emphasize(realname)
        else:
            if (cl and realname in cl.__dict__ and
                cl.__dict__[realname] is thing):
                skipdocs = 1
            title = '%s = %s' % (self.emphasize(name), realname)
        if inspect.isfunction(thing):
            args, varargs, varkw, defaults = inspect.getargspec(thing)
            argspec = inspect.formatargspec(
                args, varargs, varkw, defaults, formatvalue=self.formatvalue)
            if realname == '<lambda>':
                title = 'lambda'
                argspec = argspec[1:-1] # remove parentheses
        else:
            argspec = '(...)'
        argspec = self.escape(argspec)
        decl = '%s %s %s' % (title, argspec, note)
        if inspect.isfunction(thing):
            line = 'function %s' % name
        elif inspect.ismethod(thing):
            line = 'method %s' % name
        else:
            line = 'thing %s' % name
        self.genTitle(line)
        self.incTitleLevel()
        self.genTitle('Prototype')
        self.push(decl)
        self.push()
        if not skipdocs:
            self.genTitle('Description')
            doc = getdoc(thing)
            if doc:
                doc = doc.rstrip()
                self.push(doc)
            else:
                self.push('No description for this function/method.')
        self.push()
        self.decTitleLevel()

    def docother(self, thing, name=None, mod=None, maxlen=None, doc=None):
        """Produce text documentation for a data object."""
        objRepr = self.repr(thing)
        if name:
            line = '- %s = %s' % (self.emphasize(name), objRepr)
        else:
            line = objRepr
        self.push(line)
        self.push()

# end class ReSTDoc


class PythonLaTeXDoc(Doc):
    pass


def extract_to_rest(thing,
        usePager=None,
        title='Python Library Documentation: %s',
        forceload=0
        ):
    """Display reSturcturedText documentation, given 
    an object or a path to an object.
    Push the resulting text through a pager, if requested,
    else print it to stdout.
    Add a minimal amount of header and trailer information to
    the document.
    """
    formatter = ReSTDoc()
    try:
        thing1, name = resolve(thing, forceload)
    except (ImportError, ErrorDuringImport), value:
        print value
        return
    desc = describe(thing1)
    module = inspect.getmodule(thing1)
    if name and '.' in name:
        desc += ' in ' + name[:name.rfind('.')]
    elif module and module is not thing1:
        desc += ' in module ' + module.__name__
    title1 = title % desc
    formatter.genTitle(title1)
    formatter.incTitleLevel()
    #
    # Add and replace header content here.
    #
    formatter.push('Generated by extract_doc.py on %s.' % time.ctime())
    formatter.push()
    formatter.document(thing1, name)
    #
    # Add and replace trailer content here.
    #
    doclines = formatter.get_lines()
    content = '\n'.join(doclines)
    #
    # Add and replace post-processing here.
    # You might consider sending the output through one of the
    #   reSTructuredText writers.
    #
    if usePager:
        pager(content)
    else:
        sys.stdout.write(content)
        sys.stdout.write('\n')


def extract_to_latex(module_name):
    print 'Not implemented yet'




USAGE_TEXT = """
Usage:
    python extract_doc.py [options] <module_name>
Options:
    -h, --help      Display this help message.
    -r, --rest      Extract to decorated reST.
    -l, --latex     Extract to Python LaTeX (module doc type). Not implemented.
    -p, --pager     Use a pager; else write to stdout.
    -o, --over      Use over *and* under title adornment, else only under.
Example:
    python extract_doc.py -r mymodule1
    python extract_doc.py -p -o -r mymodule2
"""

def usage():
    print USAGE_TEXT
    sys.exit(-1)


def main():
    global UseOverTitleAdornment, TitleAdornmentChars
    args = sys.argv[1:]
    try:
        opts, args = getopt.getopt(args, 'hrlpdo',
            ['help', 'rest', 'latex', 'pager', 'over'])
    except:
        usage()
    target = None
    usePager = None
    for opt, val in opts:
        if opt in ('-h', '--help'):
            usage()
        elif opt in ('-r', '--rest'):
            target = TARGET_rest
        elif opt in ('-l', '--latex'):
            target = TARGET_latex
        elif opt in ('-p', '--pager'):
            usePager = 1
        elif opt in ('-o', '--over'):
            UseOverTitleAdornment = 1
            TitleAdornmentChars = TitleAdornmentChars_over
    if len(args) != 1:
        usage()
    if target == TARGET_rest:
        extract_to_rest(args[0], usePager)
    elif target == TARGET_latex:
        extract_to_latex(args[0], usePager)
    else:
        usage()


if __name__ == '__main__':
    args = sys.argv[1:]
    if '-d' not in args:
        main()
    else:
        import pdb
        pdb.run('main()')