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

Source Code for Module data.data_classes

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