root/galaxy-central/eggs/docutils-0.4-py2.6.egg/docutils/readers/python/__init__.py

リビジョン 3, 4.4 KB (コミッタ: kohda, 14 年 前)

Install Unix tools  http://hannonlab.cshl.edu/galaxy_unix_tools/galaxy.html

行番号 
1# Author: David Goodger
2# Contact: goodger@users.sourceforge.net
3# Revision: $Revision: 3038 $
4# Date: $Date: 2005-03-14 17:16:57 +0100 (Mon, 14 Mar 2005) $
5# Copyright: This module has been placed in the public domain.
6
7"""
8This package contains the Python Source Reader modules.
9
10It requires Python 2.2 or higher (`moduleparser` depends on the
11`compiler` and `tokenize` modules).
12"""
13
14__docformat__ = 'reStructuredText'
15
16
17import sys
18import docutils.readers
19from docutils.readers.python import moduleparser
20from docutils import parsers
21from docutils import nodes
22from docutils.readers.python import pynodes
23from docutils import readers
24
25class Reader(docutils.readers.Reader):
26
27    config_section = 'python reader'
28    config_section_dependencies = ('readers',)
29
30    default_parser = 'restructuredtext'
31
32    def parse(self):
33        """Parse `self.input` into a document tree."""
34        self.document = document = self.new_document()
35        module_section = moduleparser.parse_module(self.input,
36                                                   self.source.source_path)
37        module_section.walk(DocformatVisitor(self.document))
38        visitor = DocstringFormattingVisitor(
39            document=document,
40            default_parser=self.default_parser)
41        module_section.walk(visitor)
42        self.document.append(module_section)
43
44
45class DocformatVisitor(nodes.SparseNodeVisitor):
46
47    """
48    This sets docformat attributes in a module.  Wherever an assignment
49    to __docformat__ is found, we look for the enclosing scope -- a class,
50    a module, or a function -- and set the docformat attribute there.
51
52    We can't do this during the DocstringFormattingVisitor walking,
53    because __docformat__ may appear below a docstring in that format
54    (typically below the module docstring).
55    """
56
57    def visit_attribute(self, node):
58        assert isinstance(node[0], pynodes.object_name)
59        name = node[0][0].data
60        if name != '__docformat__':
61            return
62        value = None
63        for child in children:
64            if isinstance(child, pynodes.expression_value):
65                value = child[0].data
66                break
67        assert value.startswith("'") or value.startswith('"'), "__docformat__ must be assigned a string literal (not %s); line: %s" % (value, node['lineno'])
68        name = name[1:-1]
69        looking_in = node.parent
70        while not isinstance(looking_in, (pynodes.module_section,
71                                          pynodes.function_section,
72                                          pynodes.class_section)):
73            looking_in = looking_in.parent
74        looking_in['docformat'] = name
75
76
77class DocstringFormattingVisitor(nodes.SparseNodeVisitor):
78
79    def __init__(self, document, default_parser):
80        self.document = document
81        self.default_parser = default_parser
82        self.parsers = {}
83
84    def visit_docstring(self, node):
85        text = node[0].data
86        docformat = self.find_docformat(node)
87        del node[0]
88        node['docformat'] = docformat
89        parser = self.get_parser(docformat)
90        parser.parse(text, self.document)
91        for child in self.document.children:
92            node.append(child)
93        self.document.current_source = self.document.current_line = None
94        del self.document[:]
95
96    def get_parser(self, parser_name):
97        """
98        Get a parser based on its name.  We reuse parsers during this
99        visitation, so parser instances are cached.
100        """
101        parser_name = parsers._parser_aliases.get(parser_name, parser_name)
102        if not self.parsers.has_key(parser_name):
103            cls = parsers.get_parser_class(parser_name)
104            self.parsers[parser_name] = cls()
105        return self.parsers[parser_name]
106
107    def find_docformat(self, node):
108        """
109        Find the __docformat__ closest to this node (i.e., look in the
110        class or module)
111        """
112        while node:
113            if node.get('docformat'):
114                return node['docformat']
115            node = node.parent
116        return self.default_parser
117
118
119if __name__ == '__main__':
120    try:
121        import locale
122        locale.setlocale(locale.LC_ALL, '')
123    except:
124        pass
125
126    from docutils.core import publish_cmdline, default_description
127
128    description = ('Generates pseudo-XML from Python modules '
129                   '(for testing purposes).  ' + default_description)
130
131    publish_cmdline(description=description,
132                    reader=Reader())
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。