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

Source Code for Module user_functions.data

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2012 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  from relax_errors import RelaxError 
 28   
 29  # relax module imports. 
 30  from relax_errors import RelaxError 
 31  from user_functions.objects import Class_container, Table, Uf_container 
 32   
 33   
34 -class Uf_info(object):
35 """The user function data singleton class.""" 36 37 # Class variable for storing the class instance (for the singleton). 38 _instance = None 39
40 - def __new__(self, *args, **kargs):
41 """Replacement method for implementing the singleton design pattern.""" 42 43 # First instantiation. 44 if self._instance is None: 45 # Instantiate. 46 self._instance = object.__new__(self, *args, **kargs) 47 48 # Initialise a number of class variables. 49 self._uf_names = [] 50 self._uf = {} 51 self._class_names = [] 52 self._classes = {} 53 54 # Already instantiated, so return the instance. 55 return self._instance
56 57
58 - def add_class(self, name):
59 """Add a new user function class. 60 61 @param name: The name of the user function class. 62 @type name: str 63 @return: The user function class data object. 64 @rtype: user_functions.objects.Class_container instance 65 """ 66 67 # Check if the user function already exists. 68 if name in self._class_names: 69 raise RelaxError("The user function class %s has already been set up." % name) 70 71 # Store the name and initialise a new object. 72 self._class_names.append(name) 73 self._classes[name] = Class_container() 74 75 # Alphabetically sort the names. 76 self._class_names.sort() 77 78 # Return the object. 79 return self._classes[name]
80 81
82 - def add_uf(self, name):
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 # Check if the user function already exists. 92 if name in self._uf_names: 93 raise RelaxError("The user function %s has already been set up." % name) 94 95 # First check if the user function class has been set up. 96 if search('\.', name): 97 # Split up the name. 98 class_name, fn_name = name.split('.') 99 100 # Check for the class name. 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 # Store the name and initialise a new object. 105 self._uf_names.append(name) 106 self._uf[name] = Uf_container() 107 108 # Alphabetically sort the names. 109 self._uf_names.sort() 110 111 # Return the object. 112 return self._uf[name]
113 114
115 - def class_loop(self):
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 # Loop over the classes. 123 for i in range(len(self._class_names)): 124 yield self._class_names[i], self._classes[self._class_names[i]]
125 126
127 - def get_class(self, name):
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 # Return the object. 137 return self._classes[name]
138 139
140 - def get_uf(self, name):
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 # Return the object. 150 return self._uf[name]
151 152
153 - def uf_loop(self, uf_class=None):
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 # Loop over the user functions. 163 for i in range(len(self._uf_names)): 164 # Restriction. 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
171 - def validate(self):
172 """Validate that all of the user functions have been correctly set up.""" 173 174 # Loop over the user functions. 175 for name, uf in self.uf_loop(): 176 # Check the title. 177 if uf.title == None: 178 raise RelaxError("The title of the %s user function has not been specified." % name) 179 180 # Check the backend. 181 if uf.backend == None: 182 raise RelaxError("The back end of the %s user function has not been specified." % name)
183 184 185
186 -class Uf_tables(object):
187 """The data singleton holding all of the description tables.""" 188 189 # Class variable for storing the class instance (for the singleton). 190 _instance = None 191
192 - def __new__(self, *args, **kargs):
193 """Replacement method for implementing the singleton design pattern.""" 194 195 # First instantiation. 196 if self._instance is None: 197 # Instantiate. 198 self._instance = object.__new__(self, *args, **kargs) 199 200 # Initialise a number of class variables. 201 self._tables = {} 202 self._labels = [] 203 204 # Already instantiated, so return the instance. 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 # Check that the label is supplied. 226 if label == None: 227 raise RelaxError("The table label must be supplied.") 228 229 # Check if the table already exists. 230 if label in self._labels: 231 raise RelaxError("The table with label '%s' has already been set up." % label) 232 233 # Store the label and initialise a new object. 234 self._labels.append(label) 235 self._tables[label] = Table(label=label, caption=caption, caption_short=caption_short, spacing=spacing, longtable=longtable) 236 237 # Return the table. 238 return self._tables[label]
239 240
241 - def get_table(self, label):
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 # Check the label. 251 if label not in self._tables.keys(): 252 raise RelaxError("The table with label '%s' does not exist." % label) 253 254 # Return the table. 255 return self._tables[label]
256