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
|
__author__ = 'Robert Smallshire'
class Node(object):
def __init__(self, indent=None, lines=None, parent=None):
if indent is not None:
self.indent = indent
else:
self.indent = 0
if lines is not None:
self.lines = lines
else:
self.lines = []
self._parent = parent
self.children = []
parent = property(lambda self: self._parent)
def add_child(self, child):
assert(child.parent is self)
self.children.append(child)
def __repr__(self):
return "Node(" + repr(self.indent) + ", " + repr(self.lines) + ", children=" + repr(self.children) + ")"
def render_rst(self, *args, **kwargs):
result = []
prefix = ' ' * self.indent
result.extend(prefix + line for line in self.lines)
for child in self.children:
result.extend(child.render_rst())
return result
class Arg(Node):
def __init__(self, indent, child_indent, name):
super(Arg, self).__init__(indent)
self.child_indent = child_indent
self.name = name
self.type = None
def __repr__(self):
return "Arg(" + repr(self.name) + ", " + repr(self.type) + ", children=" + repr(self.children) + ")"
def render_rst(self, *args, **kwargs):
result = []
indent = ' ' * self.indent
# Render the param description
description = []
for child in self.children:
child_lines = child.render_rst()
description.extend(child_lines)
dedent = self.child_indent - self.indent
name = self.name.replace('*', r'\*')
first_description = description[0].lstrip() if len(description) else ''
if not first_description:
# TODO: Emit a warning about a missing argument description
pass
result.append("{indent}:param {name}: {first_description}".format(indent=indent, name=name,
first_description=first_description))
dedented_body = [line[dedent:] for line in description[1:]]
result.extend(dedented_body)
# If a type was specified render the type
if self.type is not None:
result.append("{indent}:type {name}: {type}".format(indent=indent, name=self.name, type=self.type))
result.append('')
ensure_terminal_blank(result)
return result
class Raises(Node):
def __init__(self, indent=None):
super(Raises, self).__init__(indent=indent)
def __repr__(self):
return "Raises(" + repr(self.indent) + ", children=" + repr(self.children) + ")"
def render_rst(self, *args, **kwargs):
result = []
indent = ' ' * self.indent
result.append(indent + ':raises:')
for child in self.children:
result.extend(child.render_rst(only_child=len(self.children) == 1))
ensure_terminal_blank(result)
return result
class Except(Node):
def __init__(self, indent, type):
super(Except, self).__init__(indent=indent)
#self.child_indent = child_indent
self.type = type
def __repr__(self):
return "Except(" + repr(self.type) + ", children=" + repr(self.children) + ")"
def render_rst(self, only_child=False, *args, **kwargs):
result = []
indent = ' ' * self.indent
# Render the param description
description = []
for child in self.children:
child_lines = child.render_rst()
description.extend(child_lines)
#dedent = self.child_indent - self.indent
bullet = '* ' if not only_child else ''
first_description = description[0].lstrip() if len(description) else ''
result.append("{indent}{bullet}{type} - {first_description}".format(indent=indent,
bullet=bullet, type=self.type,
first_description=first_description))
#dedented_body = [' ' * len(bullet) + line[dedent:] for line in description[1:]]
#result.extend(dedented_body)
result.extend(description[1:])
ensure_terminal_blank(result)
return result
class Returns(Node):
def __init__(self, indent):
super(Returns, self).__init__(indent=indent)
self.title = 'Returns'
self.line = ''
def __repr__(self):
return "Returns(" + str(self.indent) + ", children=" + str(self.children) + ")"
def render_rst(self, *args, **kwargs):
result = []
indent = ' ' * self.indent
# Render the param description
description = [self.line] if self.line else []
for child in self.children:
child_lines = child.render_rst()
description.extend(child_lines)
self.render_title(description, indent, result)
result.extend(description[1:])
ensure_terminal_blank(result)
return result
def render_title(self, description, indent, result):
result.append(
"{indent}:returns: {first_description}".format(indent=indent,
first_description=description[0].lstrip()))
class Warning(Node):
def __init__(self, indent):
super(Warning, self).__init__(indent=indent)
def __repr__(self):
return "Warning(" + repr(self.indent) + ", children=" + str(self.children) + ")"
def render_rst(self, *args, **kwargs):
# TODO: Factor out the commonality between this and Note below
result = []
indent = ' ' * self.indent
# Render the param description
description = [self.line] if self.line else []
for child in self.children:
child_lines = child.render_rst()
description.extend(child_lines)
# Fix the indent on the first line
if len(description) > 1 and len(description[1].strip()) != 0:
body_indent = len(description[1]) - len(description[1].strip())
else:
body_indent = self.indent + 4
if len(description) > 0:
description[0] = ' ' * body_indent + description[0]
result.append(indent + ".. warning::")
result.append(indent + '')
result.extend(description)
ensure_terminal_blank(result)
return result
class Note(Node):
def __init__(self, indent):
super(Note, self).__init__(indent=indent)
self.line = ''
def __repr__(self):
return "Note(" + repr(self.indent) + ", children=" + str(self.children) + ")"
def render_rst(self, *args, **kwargs):
# TODO: Factor out the commonality between this and Warning above
result = []
indent = ' ' * self.indent
# Render the param description
description = [self.line] if self.line else []
for child in self.children:
child_lines = child.render_rst()
description.extend(child_lines)
# Fix the indent on the first line
if len(description) > 1 and len(description[1].strip()) != 0:
body_indent = len(description[1]) - len(description[1].strip())
else:
body_indent = self.indent + 4
if len(description) > 0:
description[0] = ' ' * body_indent + description[0]
result.append(indent + ".. note::")
result.append(indent + '')
result.extend(description)
ensure_terminal_blank(result)
return result
def ensure_terminal_blank(result):
'''If the description didn't end with a blank line add one here.'''
if len(result) > 0:
if len(result[-1].strip()) != 0:
result.append('')
|