1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """The base class for all the user function classes."""
25
26
27 import platform
28 from re import split
29 import sys
30 from textwrap import wrap
31
32
33 import ansi
34 import help
35 from status import Status; status = Status()
36 from string import split, strip
37
38
39 if platform.uname()[0] in ['Windows', 'Microsoft']:
40 width = 80
41 else:
42 width = 100
43
44
46 """Build the fn.__doc__ docstring.
47
48 @param fn: The user function to build the docstring for.
49 @type fn: method
50 """
51
52
53 fn.__doc__ = ""
54
55
56 fn.__doc__ = "%s%s\n" % (fn.__doc__, fn._doc_title)
57
58
59 if hasattr(fn, '_doc_args'):
60 fn.__doc__ = fn.__doc__ + _build_subtitle("Keyword Arguments")
61 for arg, desc in fn._doc_args:
62
63 text = "%s: %s" % (arg, desc)
64
65
66 text = _format_text(text)
67
68
69 fn.__doc__ = "%s%s\n" % (fn.__doc__, text)
70
71
72 if hasattr(fn, '_doc_desc'):
73 fn.__doc__ = fn.__doc__ + _build_subtitle("Description")
74 fn.__doc__ = fn.__doc__ + _format_text(fn._doc_desc)
75
76
77 if hasattr(fn, '_doc_examples'):
78 fn.__doc__ = fn.__doc__ + '\n' + _build_subtitle("Examples")
79 fn.__doc__ = fn.__doc__ + _format_text(fn._doc_examples)
80
81
82 if hasattr(fn, '_doc_additional'):
83
84 for i in range(len(fn._doc_additional)):
85 fn.__doc__ = fn.__doc__ + '\n' + _build_subtitle(fn._doc_additional[i][0])
86 fn.__doc__ = fn.__doc__ + _format_text(fn._doc_additional[i][1])
87
88
89 if hasattr(fn, '_doc_args'):
90
91 fn._doc_args_dict = {}
92
93
94 for arg, desc in fn._doc_args:
95 fn._doc_args_dict[arg] = desc
96
97
98
100 """Create the formatted subtitle string.
101
102 @param text: The name of the subtitle.
103 @type text: str
104 @return: The formatted subtitle.
105 @rtype: str
106 """
107
108
109 return "\n%s\n%s\n\n" % (text, "~"*len(text))
110
111
113 """Format the text by stripping whitespace and wrapping.
114
115 @param text: The text to strip and wrap.
116 @type text: str
117 @return: The stripped and wrapped text.
118 @rtype: str
119 """
120
121
122 stripped_text = _strip_lead(text)
123
124
125 while True:
126 if stripped_text[0] == "\n":
127 stripped_text = stripped_text[1:]
128 else:
129 break
130
131
132 while True:
133 if stripped_text[-1] == "\n":
134 stripped_text = stripped_text[:-1]
135 else:
136 break
137
138
139 lines = split(stripped_text, "\n")
140
141
142 new_text = ""
143 for line in lines:
144
145 if not len(line):
146 new_text = new_text + "\n"
147
148
149 for wrapped_line in wrap(line, width):
150 new_text = new_text + wrapped_line + "\n"
151
152
153 return new_text
154
155
157 """Strip the leading whitespace from the given text.
158
159 @param text: The text to strip the leading whitespace from.
160 @type text: str
161 @return: The text with leading whitespace removed.
162 @rtype: str
163 """
164
165
166 lines = split(text, '\n')
167
168
169 min_white = 1000
170 for line in lines:
171
172 if strip(line) == '':
173 continue
174
175
176 num_white = 0
177 for i in range(len(line)):
178 if line[i] != ' ':
179 break
180 num_white = num_white + 1
181
182
183 min_white = min(min_white, num_white)
184
185
186 new_text = ''
187 for line in lines:
188 new_text = new_text + line[min_white:] + '\n'
189
190
191 return new_text
192
193
194
197 """All non-user function classes.
198
199 @keyword exec_info: The execution information container. This must contain at least the exec_info.intro boolean variable. If not supplied, an instance will be generated.
200 @type exec_info: None or class instance
201 """
202
203
204 if exec_info == None:
205 exec_info = Exec_info()
206
207
208 self._exec_info = exec_info
209
210
211
213 """Container for execution information."""
214
216 """Initialise the data of this container.
217
218 This includes the introduction flag as well as the strings to change the Python prompts.
219 """
220
221
222 self.intro = True
223
224
225 self.ps1_orig = 'relax> '
226 self.ps2_orig = 'relax| '
227 self.ps3_orig = '\n%s' % self.ps1_orig
228
229
230 self.ps1_colour = "%s%s%s" % (ansi.relax_prompt, self.ps1_orig, ansi.end)
231 self.ps2_colour = "%s%s%s" % (ansi.relax_prompt, self.ps2_orig, ansi.end)
232 self.ps3_colour = "\n%s%s%s" % (ansi.relax_prompt, self.ps1_orig, ansi.end)
233
234
235 self.prompt_colour_off()
236
237
239 """Turn the prompt colouring ANSI escape sequences off."""
240
241 sys.ps1 = self.ps1 = self.ps1_orig
242 sys.ps2 = self.ps2 = self.ps2_orig
243 sys.ps3 = self.ps3 = self.ps3_orig
244
245
247 """Turn the prompt colouring ANSI escape sequences off."""
248
249 sys.ps1 = self.ps1 = self.ps1_colour
250 sys.ps2 = self.ps2 = self.ps2_colour
251 sys.ps3 = self.ps3 = self.ps3_colour
252
253
254
257 """Initialise the user function class, compiling the help string.
258
259 @keyword exec_info: The execution information container. This must contain at least the exec_info.intro boolean variable. If not supplied, an instance will be generated.
260 @type exec_info: None or class instance
261 """
262
263
264 if exec_info == None:
265 exec_info = Exec_info()
266
267
268 self._exec_info = exec_info
269
270
271 self.__relax_help__ = self.__doc__ + "\n" + help.relax_class_help
272
273
274 if hasattr(self, '__description__'):
275 self.__relax_help__ = self.__relax_help__ + "\n\n" + _strip_lead(self.__description__)
276