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

Source Code for Module data_store.data_classes

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