1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Module containing the user function data singleton which stores all of the data."""
24
25
26 from re import search
27
28
29 from lib.errors import RelaxError
30 from user_functions.objects import Class_container, Table, Uf_container
31
32
34 """The user function data singleton class."""
35
36
37 _instance = None
38
55
56
79
80
82 """Add the user function to the object.
83
84 @param name: The name of the user function.
85 @type name: str
86 @return: The user function data object.
87 @rtype: user_functions.objects.Uf_container instance
88 """
89
90
91 if name in self._uf_names:
92 raise RelaxError("The user function %s has already been set up." % name)
93
94
95 if search(r'\.', name):
96
97 class_name, fn_name = name.split('.')
98
99
100 if class_name not in self._class_names:
101 raise RelaxError("The user function class '%s' has not been set up yet." % class_name)
102
103
104 self._uf_names.append(name)
105 self._uf[name] = Uf_container()
106
107
108 self._uf_names.sort()
109
110
111 return self._uf[name]
112
113
115 """Iterator method for looping over the user function classes.
116
117 @return: The class name and data container.
118 @rtype: tuple of str and Class_container instance
119 """
120
121
122 for i in range(len(self._class_names)):
123 yield self._class_names[i], self._classes[self._class_names[i]]
124
125
127 """Return the user function class data object corresponding to the given name.
128
129 @param name: The name of the user function class.
130 @type name: str
131 @return: The class data container.
132 @rtype: Class_container instance
133 """
134
135
136 return self._classes[name]
137
138
140 """Return the user function data object corresponding to the given name.
141
142 @param name: The name of the user function.
143 @type name: str
144 @return: The user function data container.
145 @rtype: Uf_container instance
146 """
147
148
149 return self._uf[name]
150
151
153 """Iterator method for looping over the user functions.
154
155 @keyword uf_class: If given, restrict the iterator to a user function class.
156 @type uf_class: str or None
157 @return: The user function name and data container.
158 @rtype: tuple of str and Uf_container instance
159 """
160
161
162 for i in range(len(self._uf_names)):
163
164 if uf_class and not search(r"^%s\." % uf_class, self._uf_names[i]):
165 continue
166
167 yield self._uf_names[i], self._uf[self._uf_names[i]]
168
169
171 """Validate that all of the user functions have been correctly set up."""
172
173
174 for name, uf in self.uf_loop():
175
176 if uf.title == None:
177 raise RelaxError("The title of the %s user function has not been specified." % name)
178
179
180 if uf.backend == None:
181 raise RelaxError("The back end of the %s user function has not been specified." % name)
182
183
184
186 """The data singleton holding all of the description tables."""
187
188
189 _instance = None
190
191 - def __new__(self, *args, **kargs):
205
206
207 - def add_table(self, label=None, caption=None, caption_short=None, spacing=True, longtable=False):
208 """Add a new table to the object.
209
210 @keyword label: The unique label of the table. This is used to identify tables, and is also used in the table referencing in the LaTeX compilation of the user manual.
211 @type label: str
212 @keyword caption: The caption for the table.
213 @type caption: str
214 @keyword caption_short: The optional short caption for the table, used in the LaTeX user manual list of tables section for example.
215 @type caption_short: str
216 @keyword spacing: A flag which if True will cause empty rows to be placed between elements.
217 @type spacing: bool
218 @keyword longtable: A special LaTeX flag which if True will cause the longtables package to be used to spread a table across multiple pages. This should only be used for tables that do not fit on a single page.
219 @type longtable: bool
220 @return: The table object.
221 @rtype: user_functions.objects.Table instance
222 """
223
224
225 if label == None:
226 raise RelaxError("The table label must be supplied.")
227
228
229 if label in self._labels:
230 raise RelaxError("The table with label '%s' has already been set up." % label)
231
232
233 self._labels.append(label)
234 self._tables[label] = Table(label=label, caption=caption, caption_short=caption_short, spacing=spacing, longtable=longtable)
235
236
237 return self._tables[label]
238
239
241 """Return the table matching the given label.
242
243 @param label: The unique label of the table.
244 @type label: str
245 @return: The table data container.
246 @rtype: user_functions.objects.Table instance
247 """
248
249
250 if label not in self._tables:
251 raise RelaxError("The table with label '%s' does not exist." % label)
252
253
254 return self._tables[label]
255