This file is indexed.

/usr/share/pyshared/epydoc/docwriter/plaintext.py is in python-epydoc 3.0.1-11.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# epydoc -- Plaintext output generation
#
# Copyright (C) 2005 Edward Loper
# Author: Edward Loper <edloper@loper.org>
# URL: <http://epydoc.sf.net>
#
# $Id: plaintext.py 1473 2007-02-13 19:46:05Z edloper $

"""
Plaintext output generation.
"""
__docformat__ = 'epytext en'

from epydoc.apidoc import *
import re

class PlaintextWriter:
    def write(self, api_doc, **options):
        result = []
        out = result.append

        self._cols = options.get('cols', 75)

        try:
            if isinstance(api_doc, ModuleDoc):
                self.write_module(out, api_doc)
            elif isinstance(api_doc, ClassDoc):
                self.write_class(out, api_doc)
            elif isinstance(api_doc, RoutineDoc):
                self.write_function(out, api_doc)
            else:
                assert 0, ('%s not handled yet' % api_doc.__class__)
        except Exception, e:
            print '\n\n'
            print ''.join(result)
            raise

        return ''.join(result)

    def write_module(self, out, mod_doc):
        #for n,v in mod_doc.variables.items():
        #    print n, `v.value`, `v.value.value`
        
        # The cannonical name of the module.
        out(self.section('Module Name'))
        out('    %s\n\n' % mod_doc.canonical_name)

        # The module's description.
        if mod_doc.descr not in (None, '', UNKNOWN):
            out(self.section('Description'))
            out(mod_doc.descr.to_plaintext(None, indent=4))

        #out('metadata: %s\n\n' % mod_doc.metadata) # [xx] testing

        self.write_list(out, 'Classes', mod_doc, value_type='class')
        self.write_list(out, 'Functions', mod_doc, value_type='function')
        self.write_list(out, 'Variables', mod_doc, value_type='other')
        # hmm.. do this as just a flat list??
        #self.write_list(out, 'Imports', mod_doc, imported=True, verbose=False)

    def baselist(self, class_doc):
        if class_doc.bases is UNKNOWN:
            return '(unknown bases)'
        if len(class_doc.bases) == 0: return ''
        s = '('
        class_parent = class_doc.canonical_name.container()
        for i, base in enumerate(class_doc.bases):
            if base.canonical_name is None:
                if base.parse_repr is not UNKNOWN:
                    s += base.parse_repr
                else:
                    s += '??'
            elif base.canonical_name.container() == class_parent:
                s += str(base.canonical_name[-1])
            else:
                s += str(base.canonical_name)
            if i < len(class_doc.bases)-1: out(', ')
        return s+')'

    def write_class(self, out, class_doc, name=None, prefix='', verbose=True):
        baselist = self.baselist(class_doc)
        
        # If we're at the top level, then list the cannonical name of
        # the class; otherwise, our parent will have already printed
        # the name of the variable containing the class.
        if prefix == '':
            out(self.section('Class Name'))
            out('    %s%s\n\n' % (class_doc.canonical_name, baselist))
        else:
            out(prefix + 'class %s' % self.bold(str(name)) + baselist+'\n')

        if not verbose: return

        # Indent the body
        if prefix != '':
            prefix += ' |  '

        # The class's description.
        if class_doc.descr not in (None, '', UNKNOWN):
            if prefix == '':
                out(self.section('Description', prefix))
                out(self._descr(class_doc.descr, '    '))
            else:
                out(self._descr(class_doc.descr, prefix))

        # List of nested classes in this class.
        self.write_list(out, 'Methods', class_doc,
                        value_type='instancemethod', prefix=prefix,
                        noindent=len(prefix)>4)
        self.write_list(out, 'Class Methods', class_doc,
                        value_type='classmethod', prefix=prefix)
        self.write_list(out, 'Static Methods', class_doc,
                        value_type='staticmethod', prefix=prefix)
        self.write_list(out, 'Nested Classes', class_doc,
                        value_type='class', prefix=prefix)
        self.write_list(out, 'Instance Variables', class_doc,
                        value_type='instancevariable', prefix=prefix)
        self.write_list(out, 'Class Variables', class_doc,
                        value_type='classvariable', prefix=prefix)
        
        self.write_list(out, 'Inherited Methods', class_doc,
                        value_type='method', prefix=prefix,
                        inherited=True, verbose=False)
        self.write_list(out, 'Inherited Instance Variables', class_doc,
                        value_type='instancevariable', prefix=prefix,
                        inherited=True, verbose=False)
        self.write_list(out, 'Inherited Class Variables', class_doc,
                        value_type='classvariable', prefix=prefix,
                        inherited=True, verbose=False)
        self.write_list(out, 'Inherited Nested Classes', class_doc,
                        value_type='class', prefix=prefix,
                        inherited=True, verbose=False)

    def write_variable(self, out, var_doc, name=None, prefix='', verbose=True):
        if name is None: name = var_doc.name
        out(prefix+self.bold(str(name)))
        if (var_doc.value not in (UNKNOWN, None) and
            var_doc.is_alias is True and
            var_doc.value.canonical_name not in (None, UNKNOWN)):
            out(' = %s' % var_doc.value.canonical_name)
        elif var_doc.value not in (UNKNOWN, None):
            val_repr = var_doc.value.summary_pyval_repr(
                max_len=self._cols-len(name)-len(prefix)-3)
            out(' = %s' % val_repr.to_plaintext(None))
        out('\n')
        if not verbose: return
        prefix += '    ' # indent the body.
        if var_doc.descr not in (None, '', UNKNOWN):
            out(self._descr(var_doc.descr, prefix))

    def write_property(self, out, prop_doc, name=None, prefix='',
                       verbose=True):
        if name is None: name = prop_doc.canonical_name
        out(prefix+self.bold(str(name)))
        if not verbose: return
        prefix += '    ' # indent the body.
            
        if prop_doc.descr not in (None, '', UNKNOWN):
            out(self._descr(prop_doc.descr, prefix))


    def write_function(self, out, func_doc, name=None, prefix='',
                       verbose=True):
        if name is None: name = func_doc.canonical_name
        self.write_signature(out, func_doc, name, prefix)
        if not verbose: return
        
        prefix += '    ' # indent the body.
            
        if func_doc.descr not in (None, '', UNKNOWN):
            out(self._descr(func_doc.descr, prefix))

        if func_doc.return_descr not in (None, '', UNKNOWN):
            out(self.section('Returns:', prefix))
            out(self._descr(func_doc.return_descr, prefix+'    '))

        if func_doc.return_type not in (None, '', UNKNOWN):
            out(self.section('Return Type:', prefix))
            out(self._descr(func_doc.return_type, prefix+'    '))

    def write_signature(self, out, func_doc, name, prefix):
        args = [self.fmt_arg(argname, default) for (argname, default) 
                in zip(func_doc.posargs, func_doc.posarg_defaults)]
        if func_doc.vararg: args.append('*'+func_doc.vararg)
        if func_doc.kwarg: args.append('**'+func_doc.kwarg)

        out(prefix+self.bold(str(name))+'(')
        x = left = len(prefix) + len(name) + 1
        for i, arg in enumerate(args):
            if x > left and x+len(arg) > 75:
                out('\n'+prefix + ' '*len(name) + ' ')
                x = left
            out(arg)
            x += len(arg)
            if i < len(args)-1:
                out(', ')
                x += 2
        out(')\n')

    # [xx] tuple args!
    def fmt_arg(self, name, default):
        if default is None:
            return '%s' % name
        else:
            default_repr = default.summary_pyval_repr()
            return '%s=%s' % (name, default_repr.to_plaintext(None))

    def write_list(self, out, heading, doc, value_type=None, imported=False,
                   inherited=False, prefix='', noindent=False,
                   verbose=True):
        # Get a list of the VarDocs we should describe.
        if isinstance(doc, ClassDoc):
            var_docs = doc.select_variables(value_type=value_type,
                                            imported=imported,
                                            inherited=inherited)
        else:
            var_docs = doc.select_variables(value_type=value_type,
                                            imported=imported)
        if not var_docs: return

        out(prefix+'\n')
        if not noindent:
            out(self.section(heading, prefix))
            prefix += '    '

        for i, var_doc in enumerate(var_docs):
            val_doc, name = var_doc.value, var_doc.name

            if verbose:
                out(prefix+'\n')

            # hmm:
            if not verbose:
                if isinstance(doc, ClassDoc):
                    name = var_doc.canonical_name
                elif val_doc not in (None, UNKNOWN):
                    name = val_doc.canonical_name
                    
            if isinstance(val_doc, RoutineDoc):
                self.write_function(out, val_doc, name, prefix, verbose)
            elif isinstance(val_doc, PropertyDoc):
                self.write_property(out, val_doc, name, prefix, verbose)
            elif isinstance(val_doc, ClassDoc):
                self.write_class(out, val_doc, name, prefix, verbose)
            else:
                self.write_variable(out, var_doc, name, prefix, verbose)

    def _descr(self, descr, prefix):
        s = descr.to_plaintext(None, indent=len(prefix)).rstrip()
        s = '\n'.join([(prefix+l[len(prefix):]) for l in s.split('\n')])
        return s+'\n'#+prefix+'\n'
                               

#    def drawline(self, s, x):
#        s = re.sub(r'(?m)^(.{%s}) ' % x, r'\1|', s)
#        return re.sub(r'(?m)^( {,%s})$(?=\n)' % x, x*' '+'|', s)

        
    #////////////////////////////////////////////////////////////
    # Helpers
    #////////////////////////////////////////////////////////////
    
    def bold(self, text):
        """Write a string in bold by overstriking."""
        return ''.join([ch+'\b'+ch for ch in text])

    def title(self, text, indent):
        return ' '*indent + self.bold(text.capitalize()) + '\n\n'

    def section(self, text, indent=''):
        if indent == '':
            return indent + self.bold(text.upper()) + '\n'
        else:
            return indent + self.bold(text.capitalize()) + '\n'