summaryrefslogtreecommitdiff
path: root/docutils/readers/python/moduleparser.py
blob: 9ab3eea79cce85b88bc93acea81e773c9b6c02b1 (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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# Author: David Goodger
# Contact: goodger@users.sourceforge.net
# Revision: $Revision$
# Date: $Date$
# Copyright: This module has been placed in the public domain.

"""
Parser for Python modules.

Ideas:

* Tokenize the module in parallel to extract initial values, comments, etc.

* Merge the compiler & tokenize output such that the raw text hangs off of
  nodes?  Especially assignment expressions (RHS).

What I'd like to do is to take a module, read in the text, run it through the
module parser (using compiler.py and tokenize.py) and produce a high-level AST
full of nodes that are interesting from an auto-documentation standpoint.  For
example, given this module (x.py)::

    # comment

    '''Docstring'''

    '''Additional docstring'''

    __docformat__ = 'reStructuredText'

    a = 1
    '''Attribute docstring'''

    class C(Super):

        '''C's docstring'''

        class_attribute = 1
        '''class_attribute's docstring'''

        def __init__(self, text=None):
            '''__init__'s docstring'''

            self.instance_attribute = (text * 7
                                       + ' whaddyaknow')
            '''instance_attribute's docstring'''


    def f(x,                            # parameter x
          y=a*5,                        # parameter y
          *args):                       # parameter args
        '''f's docstring'''
        return [x + item for item in args]

    f.function_attribute = 1
    '''f.function_attribute's docstring'''

The module parser should produce a high-level AST, something like this::

    <Module filename="x.py">
        <Comment lineno=1>
            comment
        <Docstring lineno=3>
            Docstring
        <Docstring lineno=...>           (I'll leave out the lineno's)
            Additional docstring
        <Attribute name="__docformat__">
            <Expression>
                'reStructuredText'
        <Attribute name="a">
            <Expression>
                1
            <Docstring>
                Attribute docstring
        <Class name="C" inheritance="Super">
            <Docstring>
                C's docstring
            <Attribute name="class_attribute">
                <Expression>
                    1
                <Docstring>
                    class_attribute's docstring
            <Method name="__init__" argnames=['self', ('text', 'None')]>
                <Docstring>
                    __init__'s docstring
                <Attribute name="instance_attribute" instance=True>
                    <Expression>
                        (text * 7
                         + ' whaddyaknow')
                    <Docstring>
                        class_attribute's docstring
        <Function name="f">
            <Parameters>
                <Parameter name="x">
                    <Comment>
                        # parameter x
                <Parameter name="y">
                    <Expression>
                        a*5
                    <Comment>
                        # parameter y
                <Parameter name="args" varargs=True>
                    <Comment>
                        # parameter args
            <Docstring>
                f's docstring
            <Attribute name="function_attribute">
                <Expression>
                    1
                <Docstring>
                    f.function_attribute's docstring

compiler.parse() provides most of what's needed for this AST.  I think that
"tokenize" can be used to get the rest, and all that's left is to hunker down
and figure out how.  We can determine the line number from the
compiler.parse() AST, and a get_rhs(lineno) method would provide the rest.

The Docutils Python reader component will transform this AST into a
Python-specific doctree, and then a `stylist transform`_ would further
transform it into a generic doctree.  Namespaces will have to be compiled for
each of the scopes, but I'm not certain at what stage of processing.

It's very important to keep all docstring processing out of this, so that it's
a completely generic and not tool-specific.

> Why perform all of those transformations?  Why not go from the AST to a
> generic doctree?  Or, even from the AST to the final output?

I want the docutils.readers.python.moduleparser.parse_module() function to
produce a standard documentation-oriented AST that can be used by any tool.
We can develop it together without having to compromise on the rest of our
design (i.e., HappyDoc doesn't have to be made to work like Docutils, and
vice-versa).  It would be a higher-level version of what compiler.py provides.

The Python reader component transforms this generic AST into a Python-specific
doctree (it knows about modules, classes, functions, etc.), but this is
specific to Docutils and cannot be used by HappyDoc or others.  The stylist
transform does the final layout, converting Python-specific structures
("class" sections, etc.) into a generic doctree using primitives (tables,
sections, lists, etc.).  This generic doctree does *not* know about Python
structures any more.  The advantage is that this doctree can be handed off to
any of the output writers to create any output format we like.

The latter two transforms are separate because I want to be able to have
multiple independent layout styles (multiple runtime-selectable "stylist
transforms").  Each of the existing tools (HappyDoc, pydoc, epydoc, Crystal,
etc.) has its own fixed format.  I personally don't like the tables-based
format produced by these tools, and I'd like to be able to customize the
format easily.  That's the goal of stylist transforms, which are independent
from the Reader component itself.  One stylist transform could produce
HappyDoc-like output, another could produce output similar to module docs in
the Python library reference manual, and so on.

It's for exactly this reason:

>> It's very important to keep all docstring processing out of this, so that
>> it's a completely generic and not tool-specific.

... but it goes past docstring processing.  It's also important to keep style
decisions and tool-specific data transforms out of this module parser.

"""

__docformat__ = 'reStructuredText'

import sys
import compiler
import compiler.ast
import tokenize
import token
from compiler.consts import OP_ASSIGN
from compiler.visitor import ASTVisitor
from types import StringType, UnicodeType


def parse_module(module_text, filename):
    ast = compiler.parse(module_text)
    visitor = ModuleVisitor(filename)
    compiler.walk(ast, visitor, walker=visitor)
    return visitor.module


class ModuleVisitor(ASTVisitor):

    def __init__(self, filename):
        ASTVisitor.__init__(self)
        self.filename = filename
        self.module = None
        self.context = []
        self.documentable = None

    def default(self, node, *args):
        self.documentable = None
        #print 'in default (%s)' % node.__class__.__name__
        #ASTVisitor.default(self, node, *args)

    def default_ignore(self, node, *args):
        #print 'in default_ignore (%s)' % node.__class__.__name__
        ASTVisitor.default(self, node, *args)

    def visitModule(self, node):
        #print dir(node)
        self.module = module = Module(node, self.filename)
        if node.doc is not None:
            module.append(Docstring(node, node.doc))
        self.context.append(module)
        self.documentable = module
        self.visit(node.node)
        self.context.pop()

    def visitStmt(self, node):
        self.default_ignore(node)

    def visitDiscard(self, node):
        if self.documentable:
            self.visit(node.expr)

    def visitConst(self, node):
        if self.documentable:
            if type(node.value) in (StringType, UnicodeType):
                self.documentable.append(Docstring(node, node.value))
            else:
                self.documentable = None

    def visitImport(self, node):
        self.context[-1].append(Import(node, node.names))
        self.documentable = None

    def visitFrom(self, node):
        self.context[-1].append(
            Import(node, node.names, from_name=node.modname))
        self.documentable = None

    def visitAssign(self, node):
        visitor = AssignmentVisitor()
        compiler.walk(node, visitor, walker=visitor)
        if visitor.attributes:
            self.context[-1].extend(visitor.attributes)
        if len(visitor.attributes) == 1:
            self.documentable = visitor.attributes[0]
        else:
            self.documentable = None


class AssignmentVisitor(ASTVisitor):

    """
    Tried reconstructing expressions (the RHS of assignments) by
    visiting the compiler.parse() tree, but a lot of information is
    missing, like parenthesis-grouping of expressions.

    Gotta do it by parsing tokens.
    """

    def __init__(self):
        ASTVisitor.__init__(self)
        self.attributes = []
        self.parts = []

    def default(self, node, *args):
        print >>sys.stderr, '%s not visited!' % node.__class__.__name__
        ASTVisitor.default(self, node)

    def visitAssign(self, node):
        ASTVisitor.default(self, node)
        self.attributes[-1].append(Expression(node, ''.join(self.parts)))

    def visitAssName(self, node):
        self.attributes.append(Attribute(node, node.name))

    def visitAdd(self, node):
        ASTVisitor.default(self, node)
        self.parts[-2:] = ' + '.join(self.parts[-2:])

    def visitAnd(self, node):
        ASTVisitor.default(self, node)
        self.parts.insert(len(self.parts) - 1, ' and ')

    def visitBackquote(self, node):
        self.parts.append('`')
        ASTVisitor.default(self, node)
        self.parts.append('`')

    def visitBitand(self, node):
        ASTVisitor.default(self, node)
        self.parts.insert(len(self.parts) - 1, ' & ')

    def visitBitor(self, node):
        ASTVisitor.default(self, node)
        self.parts.insert(len(self.parts) - 1, ' | ')

    def visitBitxor(self, node):
        ASTVisitor.default(self, node)
        self.parts.insert(len(self.parts) - 1, ' ^ ')

    def visitConst(self, node):
        self.parts.append(repr(node.value))

    def visitConst(self, node):
        self.parts.append(repr(node.value))

    def visitInvert(self, node):
        self.parts.append('~ ')
        ASTVisitor.default(self, node)


class Node:

    def __init__(self, node):
        self.children = []
        """List of child nodes."""

        self.lineno = node.lineno
        """Line number of this node (or ``None``)."""

    def __str__(self, indent='    ', level=0):
        return ''.join(['%s%s\n' % (indent * level, repr(self))] +
                       [child.__str__(indent, level+1)
                        for child in self.children])

    def __repr__(self):
        parts = [self.__class__.__name__]
        for name, value in self.attlist():
            parts.append('%s="%s"' % (name, value))
        return '<%s>' % ' '.join(parts)

    def attlist(self, **atts):
        if self.lineno is not None:
            atts['lineno'] = self.lineno
        attlist = atts.items()
        attlist.sort()
        return attlist

    def append(self, node):
        self.children.append(node)

    def extend(self, node_list):
        self.children.extend(node_list)


class Module(Node):

    def __init__(self, node, filename):
        Node.__init__(self, node)
        self.filename = filename

    def attlist(self):
        return Node.attlist(self, filename=self.filename)


class Docstring(Node):

    def __init__(self, node, text):
        Node.__init__(self, node)
        self.text = trim_docstring(text)

    def __str__(self, indent='    ', level=0):
        prefix = indent * (level + 1)
        text = '\n'.join([prefix + line for line in self.text.splitlines()])
        return Node.__str__(self, indent, level) + text + '\n'


class Import(Node):

    def __init__(self, node, names, from_name=None):
        Node.__init__(self, node)
        self.names = names
        self.from_name = from_name

    def __str__(self, indent='    ', level=0):
        prefix = indent * (level + 1)
        lines = []
        for name, as in self.names:
            if as:
                lines.append('%s%s as %s' % (prefix, name, as))
            else:
                lines.append('%s%s' % (prefix, name))
        text = '\n'.join(lines)
        return Node.__str__(self, indent, level) + text + '\n'

    def attlist(self):
        if self.from_name:
            atts = {'from': self.from_name}
        else:
            atts = {}
        return Node.attlist(self, **atts)


class Attribute(Node):

    def __init__(self, node, name):
        Node.__init__(self, node)
        self.name = name

    def attlist(self):
        return Node.attlist(self, name=self.name)


class Expression(Node):

    def __init__(self, node, text):
        Node.__init__(self, node)
        self.text = text

    def __str__(self, indent='    ', level=0):
        prefix = indent * (level + 1)
        return '%s%s%s\n' % (Node.__str__(self, indent, level),
                             prefix, self.text)


class TokenReader:

    def __init__(self, text):
        self.text = text
        self.lines = text.splitlines(1)
        self.generator = tokenize.generate_tokens(iter(self.lines).next)

    def __iter__(self):
        return self

    def next(self):
        token = self.generator.next()
        self.type, self.string, self.start, self.end, self.line = token
        return token

    def goto_line(self, lineno):
        for token in self:
            if self.start[0] >= lineno:
                return token
        else:
            raise IndexError

    def rhs(self, name, lineno):
        self.goto_line(lineno)
        while self.start[0] == lineno:
            if self.type == token.OP and self.string == '=':
                break
            self.next()
        else:
            raise IndexError
        

def trim_docstring(text):
    """
    Trim indentation and blank lines from docstring text & return it.

    See PEP 257.
    """
    if not text:
        return ''
    # Convert tabs to spaces (following the normal Python rules)
    # and split into a list of lines:
    lines = text.expandtabs().splitlines()
    # Determine minimum indentation (first line doesn't count):
    indent = sys.maxint
    for line in lines[1:]:
        stripped = line.lstrip()
        if stripped:
            indent = min(indent, len(line) - len(stripped))
    # Remove indentation (first line is special):
    trimmed = [lines[0].strip()]
    if indent < sys.maxint:
        for line in lines[1:]:
            trimmed.append(line[indent:].rstrip())
    # Strip off trailing and leading blank lines:
    while trimmed and not trimmed[-1]:
        trimmed.pop()
    while trimmed and not trimmed[0]:
        trimmed.pop(0)
    # Return a single string:
    return '\n'.join(trimmed)