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 from relax_errors import RelaxError
28
29
30 from relax_errors import RelaxError
31 from user_functions.objects import Class_container, Table, Uf_container
32
33
35 """The user function data singleton class."""
36
37
38 _instance = None
39
56
57
80
81
83 """Add the user function to the object.
84
85 @param name: The name of the user function.
86 @type name: str
87 @return: The user function data object.
88 @rtype: user_functions.objects.Uf_container instance
89 """
90
91
92 if name in self._uf_names:
93 raise RelaxError("The user function %s has already been set up." % name)
94
95
96 if search('\.', name):
97
98 class_name, fn_name = name.split('.')
99
100
101 if class_name not in self._class_names:
102 raise RelaxError("The user function class '%s' has not been set up yet." % class_name)
103
104
105 self._uf_names.append(name)
106 self._uf[name] = Uf_container()
107
108
109 self._uf_names.sort()
110
111
112 return self._uf[name]
113
114
116 """Iterator method for looping over the user function classes.
117
118 @return: The class name and data container.
119 @rtype: tuple of str and Class_container instance
120 """
121
122
123 for i in range(len(self._class_names)):
124 yield self._class_names[i], self._classes[self._class_names[i]]
125
126
128 """Return the user function class data object corresponding to the given name.
129
130 @param name: The name of the user function class.
131 @type name: str
132 @return: The class data container.
133 @rtype: Class_container instance
134 """
135
136
137 return self._classes[name]
138
139
141 """Return the user function data object corresponding to the given name.
142
143 @param name: The name of the user function.
144 @type name: str
145 @return: The user function data container.
146 @rtype: Uf_container instance
147 """
148
149
150 return self._uf[name]
151
152
154 """Iterator method for looping over the user functions.
155
156 @keyword uf_class: If given, restrict the iterator to a user function class.
157 @type uf_class: str or None
158 @return: The user function name and data container.
159 @rtype: tuple of str and Uf_container instance
160 """
161
162
163 for i in range(len(self._uf_names)):
164
165 if uf_class and not search("^%s\." % uf_class, self._uf_names[i]):
166 continue
167
168 yield self._uf_names[i], self._uf[self._uf_names[i]]
169
170
172 """Validate that all of the user functions have been correctly set up."""
173
174
175 for name, uf in self.uf_loop():
176
177 if uf.title == None:
178 raise RelaxError("The title of the %s user function has not been specified." % name)
179
180
181 if uf.backend == None:
182 raise RelaxError("The back end of the %s user function has not been specified." % name)
183
184
185
187 """The data singleton holding all of the description tables."""
188
189
190 _instance = None
191
192 - def __new__(self, *args, **kargs):
193 """Replacement method for implementing the singleton design pattern."""
194
195
196 if self._instance is None:
197
198 self._instance = object.__new__(self, *args, **kargs)
199
200
201 self._tables = {}
202 self._labels = []
203
204
205 return self._instance
206
207
208 - def add_table(self, label=None, caption=None, caption_short=None, spacing=True, longtable=False):
209 """Add a new table to the object.
210
211 @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.
212 @type label: str
213 @keyword caption: The caption for the table.
214 @type caption: str
215 @keyword caption_short: The optional short caption for the table, used in the LaTeX user manual list of tables section for example.
216 @type caption_short: str
217 @keyword spacing: A flag which if True will cause empty rows to be placed between elements.
218 @type spacing: bool
219 @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.
220 @type longtable: bool
221 @return: The table object.
222 @rtype: user_functions.objects.Table instance
223 """
224
225
226 if label == None:
227 raise RelaxError("The table label must be supplied.")
228
229
230 if label in self._labels:
231 raise RelaxError("The table with label '%s' has already been set up." % label)
232
233
234 self._labels.append(label)
235 self._tables[label] = Table(label=label, caption=caption, caption_short=caption_short, spacing=spacing, longtable=longtable)
236
237
238 return self._tables[label]
239
240
242 """Return the table matching the given label.
243
244 @param label: The unique label of the table.
245 @type label: str
246 @return: The table data container.
247 @rtype: user_functions.objects.Table instance
248 """
249
250
251 if label not in self._tables.keys():
252 raise RelaxError("The table with label '%s' does not exist." % label)
253
254
255 return self._tables[label]
256