Package data_store :: Module data_classes
[hide private]
[frames] | no frames]

Source Code for Module data_store.data_classes

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2001-2004,2006,2008-2012 Edward d'Auvergne                    # 
  4  # Copyright (C) 2006 Chris MacRaild                                           # 
  5  #                                                                             # 
  6  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  7  #                                                                             # 
  8  # This program is free software: you can redistribute it and/or modify        # 
  9  # it under the terms of the GNU General Public License as published by        # 
 10  # the Free Software Foundation, either version 3 of the License, or           # 
 11  # (at your option) any later version.                                         # 
 12  #                                                                             # 
 13  # This program is distributed in the hope that it will be useful,             # 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 16  # GNU General Public License for more details.                                # 
 17  #                                                                             # 
 18  # You should have received a copy of the GNU General Public License           # 
 19  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """Basic objects used to build the relax data store.""" 
 25   
 26  # Python module imports. 
 27  from re import search 
 28  from numpy import ndarray 
 29   
 30  # relax module imports. 
 31  from lib.xml import fill_object_contents, xml_to_object 
 32   
 33   
34 -class Element(object):
35 """Empty data container.""" 36
37 - def __init__(self, name='element', desc='container object'):
38 """Initialise some class variables. 39 40 @keyword name: The name of the object. 41 @type name: str 42 @keyword desc: The description of the object. 43 @type desc: str 44 """ 45 46 # Execute the base class __init__() method. 47 super(Element, self).__init__() 48 49 # Some generic initial names. 50 self.name = name 51 self.desc = desc 52 53 # Blacklisted objects. 54 self.blacklist = []
55 56
57 - def __repr__(self):
58 # Header. 59 text = "%-25s%-100s\n\n" % ("Data structure", "Value") 60 61 # Data structures. 62 for name in dir(self): 63 # Skip Element and derived class methods. 64 if name in Element.__dict__ or name in self.__class__.__dict__: 65 continue 66 67 # Skip special objects. 68 if search("^_", name): 69 continue 70 71 # Get the object. 72 obj = getattr(self, name) 73 74 # Numpy matrices. 75 if isinstance(obj, ndarray) and isinstance(obj[0], ndarray): 76 spacer = '\n' 77 else: 78 spacer = '' 79 80 # Generate the text. 81 text = text + "%-25s%s%-100s\n" % (name, spacer, repr(obj)) 82 83 # Return the lot. 84 return text
85 86
87 - def from_xml(self, super_node, file_version=1):
88 """Recreate the element data structure from the XML element node. 89 90 @param super_node: The element XML node. 91 @type super_node: xml.dom.minicompat.Element instance 92 @keyword file_version: The relax XML version of the XML file. 93 @type file_version: int 94 """ 95 96 # Recreate all the other data structures. 97 xml_to_object(super_node, self, file_version=file_version)
98 99
100 - def is_empty(self):
101 """Method for testing if the Element container is empty. 102 103 @return: True if the Element container is empty, False otherwise. 104 @rtype: bool 105 """ 106 107 # An object has been added to the container. 108 for name in dir(self): 109 # Skip Element and derived class methods. 110 if name in Element.__dict__ or name in self.__class__.__dict__: 111 continue 112 113 # Skip special objects. 114 if search("^__", name): 115 continue 116 117 # An object has been added. 118 return False 119 120 # The Element container is empty. 121 return True
122 123
124 - def to_xml(self, doc, element):
125 """Create an XML element for the container. 126 127 @param doc: The XML document object. 128 @type doc: xml.dom.minidom.Document instance 129 @param element: The element to add the data container XML element to. 130 @type element: XML element object 131 """ 132 133 # Create a new element for this container and add it to the higher level element. 134 cont_element = doc.createElement(self.name) 135 element.appendChild(cont_element) 136 137 # Set the list attributes. 138 cont_element.setAttribute('desc', self.desc) 139 140 # Blacklisted objects. 141 blacklist = self.blacklist + ['name', 'desc', 'blacklist'] + list(Element.__dict__.keys()) + list(self.__class__.__dict__.keys()) + list(object.__dict__.keys()) 142 143 # Store and blacklist the objects which have to_xml() methods. 144 to_xml_list = [] 145 for name in dir(self): 146 # Skip blacklisted objects. 147 if name in blacklist: 148 continue 149 150 # Skip special objects. 151 if search('^_', name): 152 continue 153 154 # Execute any to_xml() methods, and add that object to the blacklist. 155 obj = getattr(self, name) 156 if hasattr(obj, 'to_xml'): 157 to_xml_list.append(obj) 158 blacklist = blacklist + [name] 159 160 # Add all simple python objects within the container to the XML element. 161 fill_object_contents(doc, cont_element, object=self, blacklist=blacklist) 162 163 # Execute the object to_xml() methods. 164 for obj in to_xml_list: 165 obj.to_xml(doc, cont_element)
166 167 168
169 -class RelaxListType(list):
170 """An empty list type container.""" 171
172 - def __init__(self):
173 """Initialise some class variables.""" 174 175 # Execute the base class __init__() method. 176 super(RelaxListType, self).__init__() 177 178 # Some generic initial names. 179 self.list_name = 'relax_list' 180 self.list_desc = 'relax list container' 181 self.element_name = 'relax_list_element' 182 self.element_desc = 'relax container' 183 184 # Blacklisted objects. 185 self.blacklist = []
186 187
188 - def from_xml(self, super_node, file_version=1):
189 """Recreate the data structure from the XML node. 190 191 @param super_node: The XML nodes. 192 @type super_node: xml.dom.minicompat.Element instance 193 @keyword file_version: The relax XML version of the XML file. 194 @type file_version: int 195 """ 196 197 # Recreate all the data structures. 198 xml_to_object(super_node, self, file_version=file_version, blacklist=self.blacklist) 199 200 # Get the individual elements. 201 nodes = super_node.getElementsByTagName(self.element_name) 202 203 # Loop over the child nodes. 204 for node in nodes: 205 # Add the data container. 206 self.add_item(node.getAttribute('name')) 207 208 # Recreate all the other data structures. 209 xml_to_object(node, self[-1], file_version=file_version)
210 211
212 - def to_xml(self, doc, element):
213 """Create an XML element for the list data structure. 214 215 @param doc: The XML document object. 216 @type doc: xml.dom.minidom.Document instance 217 @param element: The element to add the list data structure XML element to. 218 @type element: XML element object 219 """ 220 221 # Create the element and add it to the higher level element. 222 list_element = doc.createElement(self.list_name) 223 element.appendChild(list_element) 224 225 # Set the list attributes. 226 list_element.setAttribute('desc', self.list_desc) 227 228 # Blacklisted objects. 229 blacklist = ['list_name', 'list_desc', 'element_name', 'element_desc', 'blacklist'] + list(self.__dict__.keys()) + list(RelaxListType.__dict__.keys()) + list(self.__class__.__dict__.keys()) + list(list.__dict__.keys()) + list(list.__dict__.keys()) 230 231 # Add all simple python objects within the list to the list element. 232 fill_object_contents(doc, list_element, object=self, blacklist=blacklist) 233 234 # Loop over the list. 235 for i in range(len(self)): 236 # The element has its own to_xml() method. 237 if hasattr(self[i], 'to_xml'): 238 self[i].to_xml(doc, list_element) 239 240 # Normal element. 241 else: 242 # Create an XML element for each container. 243 list_item_element = doc.createElement(self.element_name) 244 list_element.appendChild(list_item_element) 245 list_item_element.setAttribute('index', repr(i)) 246 list_item_element.setAttribute('desc', self.element_desc) 247 248 # Blacklisted objects. 249 blacklist = list(self[i].__class__.__dict__.keys()) 250 251 # Add objects which have to_xml() methods. 252 for name in dir(self[i]): 253 # Skip blacklisted objects. 254 if name in blacklist: 255 continue 256 257 # Skip special objects. 258 if search('^_', name): 259 continue 260 261 # Execute any to_xml() methods, and add that object to the blacklist. 262 obj = getattr(self[i], name) 263 if hasattr(obj, 'to_xml'): 264 obj.to_xml(doc, list_item_element) 265 blacklist = blacklist + [name] 266 267 # Add all simple python objects within the container to the XML element. 268 fill_object_contents(doc, list_item_element, object=self[i], blacklist=blacklist)
269