File: happydocstring.py

package info (click to toggle)
python-happydoc 2.1-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,392 kB
  • ctags: 3,479
  • sloc: python: 12,382; makefile: 99; sh: 37
file content (218 lines) | stat: -rw-r--r-- 7,370 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
#
# $Id: happydocstring.py,v 1.3 2002/08/04 10:47:30 doughellmann Exp $
#
# Copyright 2001 Doug Hellmann.
#
#
#                         All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appear in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of Doug
# Hellmann not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
#
# DOUG HELLMANN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL DOUG HELLMANN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

"""Base class for docstring converters.

    A docstring converter is responsible for translating the docstring
    markup syntax to the output formatter syntax.  Docstring
    converters should be as generic as possible, but by their nature
    will have a close relationship with a happyformatter type.

"""

__rcs_info__ = {
    #
    #  Creation Information
    #
    'module_name'  : '$RCSfile: happydocstring.py,v $',
    'rcs_id'       : '$Id: happydocstring.py,v 1.3 2002/08/04 10:47:30 doughellmann Exp $',
    'creator'      : 'Doug Hellmann <DougHellmann@bigfoot.com>',
    'project'      : 'UNSPECIFIED',
    'created'      : 'Wed, 26-Sep-2001 09:41:36 EDT',

    #
    #  Current Information
    #
    'author'       : '$Author: doughellmann $',
    'version'      : '$Revision: 1.3 $',
    'date'         : '$Date: 2002/08/04 10:47:30 $',
}
try:
    __version__ = __rcs_info__['version'].split(' ')[1]
except:
    __version__ = '0.0'

#
# Import system modules
#


#
# Import Local modules
#
from StreamFlushTest import StreamFlushTest

#
# Module
#

class ExternalDocumentationFileBase:
    "Formatted documentation in an outside file."

    _input_type = None

    def __init__(self, filename, body=None):
        self.filename = filename

        self._oneliner = ''
        if body:
            self._file_contents = body
        else:
            self._file_contents = open(filename, 'rt').read()
        return

    def oneLiner(self):
        "Returns the one line description from the file."
        return self._oneliner

    def __str__(self):
        "String representation of file."
        return str(self._file_contents)

    def getInputType(self):
        "Input type of file contents."
        return self._input_type

        

class HappyDocStringConverterBase:
    "Base class for docstring converters."

    externalDocumentFactory = ExternalDocumentationFileBase

    def __init__(self, **extraNamedParameters):
        """Initialize the docstring converter.

        Parameters:

          'extraNamedParameters' -- Parameters specified by name which
          were not interpreted by a subclass initialization.

        """
        #
        # Warn about extra named parameters
        #
        for extra_param, extra_value in extraNamedParameters.items():
            print 'WARNING: Parameter "%s" (%s) unrecognized by docstring converter %s.' % \
                  (extra_param, extra_value, self.__class__.__name__)
        return

    
    def convert(self, inputText, outputFormat, *args, **namedArgs):
        """Returns the 'inputText' data translated into the 'outputFormat'.

        Parameters:

          'inputText' -- String or other sequence of characters to be
          converted.  This string should be in the format advertised
          by the docstring converter.

          'outputFormat' -- String defined by the docstring converter
          class to represent a supported output scheme.  This value is
          converter-specific, and not all converters will support the
          same output formats.

          '*args' -- Additional, converter-specific, positional arguments.

          '**namedArgs' -- Additional, converter-specific, named arguments.
        """
        raise ValueError('%s does not implement convert' % self.__class__.__name__)

    def quote(self, inputText, outputFormat, *args, **namedArgs):
        """Returns the 'inputText' quoted in a way that special characters are escaped.

        Parameters:

          'inputText' -- String or other sequence of characters to be
          converted.  This string should be in the format advertised
          by the docstring converter.

          'outputFormat' -- String defined by the docstring converter
          class to represent a supported output scheme.  This value is
          converter-specific, and not all converters will support the
          same output formats.

          '*args' -- Additional, converter-specific, positional arguments.

          '**namedArgs' -- Additional, converter-specific, named arguments.
          
        """
        raise ValueError('%s does not implement quote' % self.__class__.__name__)
        
    def getExternalDocumentationFile(self, filename, *args, **namedArgs):
        """Returns the 'inputText' quoted in a way that special characters are escaped.

        Parameters:

          'filename' -- Name of the file to retrieve.
          
          '*args' -- Additional, converter-specific, positional arguments.

          '**namedArgs' -- Additional, converter-specific, named arguments.
        """
        try:
            file = self.externalDocumentFactory(filename)
        except IOError:
            file = None
        return file



class DocStringConverterTest(StreamFlushTest):

    def __init__(self, methodName, outputDir=''):
        StreamFlushTest.__init__(self, methodName, outputDir)
        
        import happydoclib.docstring
        self._hddocstring = happydoclib.docstring
        return

    def _testConversion(self, inputText, inputFormat, outputFormat, expectedText,
                       errorMessage,
                       debug=0):
        converter_factory = self._hddocstring.getConverterFactory(inputFormat)
        converter = converter_factory()
        converted_text = converter.convert( inputText, outputFormat )
        if debug:
            print '\n[[%s]]' % converted_text
        if converted_text != expectedText:
            print '[INPUT[%s]INPUT]' % inputText
            print '[EXPECTED[%s]EXPECTED]' % expectedText
            print '[CONVERTED[%s]CONVERTED]' % converted_text
        assert (converted_text == expectedText), '%s ("%s", "%s") ' % (errorMessage,
                                                                   expectedText,
                                                                   converted_text)
        return

    def _testQuote(self, inputText, inputFormat, outputFormat, expectedText,
                  errorMessage):
        converter_factory = self._hddocstring.getConverterFactory(inputFormat)
        converter = converter_factory()
        quoted_text = converter.quote(inputText, 'html' )
        assert (quoted_text == expectedText), errorMessage
        return