Package user_functions :: Module data
[hide private]
[frames] | no frames]

Source Code for Module user_functions.data

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2012-2013 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program is free software: you can redistribute it and/or modify        # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation, either version 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program is distributed in the hope that it will be useful,             # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Module docstring. 
 23  """Module containing the user function data singleton which stores all of the data.""" 
 24   
 25  # Python module imports. 
 26  from re import search 
 27   
 28  # relax module imports. 
 29  from lib.errors import RelaxError 
 30  from user_functions.objects import Class_container, Table, Uf_container 
 31   
 32   
33 -class Uf_info(object):
34 """The user function data singleton class.""" 35 36 # Class variable for storing the class instance (for the singleton). 37 _instance = None 38
39 - def __new__(self, *args, **kargs):
40 """Replacement method for implementing the singleton design pattern.""" 41 42 # First instantiation. 43 if self._instance is None: 44 # Instantiate. 45 self._instance = object.__new__(self, *args, **kargs) 46 47 # Initialise a number of class variables. 48 self._uf_names = [] 49 self._uf = {} 50 self._class_names = [] 51 self._classes = {} 52 53 # Already instantiated, so return the instance. 54 return self._instance
55 56
57 - def add_class(self, name):
58 """Add a new user function class. 59 60 @param name: The name of the user function class. 61 @type name: str 62 @return: The user function class data object. 63 @rtype: user_functions.objects.Class_container instance 64 """ 65 66 # Check if the user function already exists. 67 if name in self._class_names: 68 raise RelaxError("The user function class %s has already been set up." % name) 69 70 # Store the name and initialise a new object. 71 self._class_names.append(name) 72 self._classes[name] = Class_container() 73 74 # Alphabetically sort the names. 75 self._class_names.sort() 76 77 # Return the object. 78 return self._classes[name]
79 80
81 - def add_uf(self, name):
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 # Check if the user function already exists. 91 if name in self._uf_names: 92 raise RelaxError("The user function %s has already been set up." % name) 93 94 # First check if the user function class has been set up. 95 if search('\.', name): 96 # Split up the name. 97 class_name, fn_name = name.split('.') 98 99 # Check for the class name. 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 # Store the name and initialise a new object. 104 self._uf_names.append(name) 105 self._uf[name] = Uf_container() 106 107 # Alphabetically sort the names. 108 self._uf_names.sort() 109 110 # Return the object. 111 return self._uf[name]
112 113
114 - def class_loop(self):
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 # Loop over the classes. 122 for i in range(len(self._class_names)): 123 yield self._class_names[i], self._classes[self._class_names[i]]
124 125
126 - def get_class(self, name):
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 # Return the object. 136 return self._classes[name]
137 138
139 - def get_uf(self, name):
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 # Return the object. 149 return self._uf[name]
150 151
152 - def uf_loop(self, uf_class=None):
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 # Loop over the user functions. 162 for i in range(len(self._uf_names)): 163 # Restriction. 164 if uf_class and not search("^%s\." % uf_class, self._uf_names[i]): 165 continue 166 167 yield self._uf_names[i], self._uf[self._uf_names[i]]
168 169
170 - def validate(self):
171 """Validate that all of the user functions have been correctly set up.""" 172 173 # Loop over the user functions. 174 for name, uf in self.uf_loop(): 175 # Check the title. 176 if uf.title == None: 177 raise RelaxError("The title of the %s user function has not been specified." % name) 178 179 # Check the backend. 180 if uf.backend == None: 181 raise RelaxError("The back end of the %s user function has not been specified." % name)
182 183 184
185 -class Uf_tables(object):
186 """The data singleton holding all of the description tables.""" 187 188 # Class variable for storing the class instance (for the singleton). 189 _instance = None 190
191 - def __new__(self, *args, **kargs):
192 """Replacement method for implementing the singleton design pattern.""" 193 194 # First instantiation. 195 if self._instance is None: 196 # Instantiate. 197 self._instance = object.__new__(self, *args, **kargs) 198 199 # Initialise a number of class variables. 200 self._tables = {} 201 self._labels = [] 202 203 # Already instantiated, so return the instance. 204 return self._instance
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 # Check that the label is supplied. 225 if label == None: 226 raise RelaxError("The table label must be supplied.") 227 228 # Check if the table already exists. 229 if label in self._labels: 230 raise RelaxError("The table with label '%s' has already been set up." % label) 231 232 # Store the label and initialise a new object. 233 self._labels.append(label) 234 self._tables[label] = Table(label=label, caption=caption, caption_short=caption_short, spacing=spacing, longtable=longtable) 235 236 # Return the table. 237 return self._tables[label]
238 239
240 - def get_table(self, label):
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 # Check the label. 250 if label not in self._tables.keys(): 251 raise RelaxError("The table with label '%s' does not exist." % label) 252 253 # Return the table. 254 return self._tables[label]
255