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

Source Code for Module data.data_classes

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2004, 2006-2010 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):
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 """ 94 95 # Recreate all the other data structures. 96 xml_to_object(super_node, self)
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(Element.__dict__.keys() + self.__class__.__dict__.keys() + 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(ListType):
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):
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 """ 193 194 # Recreate all the data structures. 195 xml_to_object(super_node, self, blacklist=self.blacklist) 196 197 # Get the individual elements. 198 nodes = super_node.getElementsByTagName(self.element_name) 199 200 # Loop over the child nodes. 201 for node in nodes: 202 # Add the data container. 203 self.add_item(node.getAttribute('name')) 204 205 # Recreate all the other data structures. 206 xml_to_object(node, self[-1])
207 208
209 - def to_xml(self, doc, element):
210 """Create an XML element for the list data structure. 211 212 @param doc: The XML document object. 213 @type doc: xml.dom.minidom.Document instance 214 @param element: The element to add the list data structure XML element to. 215 @type element: XML element object 216 """ 217 218 # Create the element and add it to the higher level element. 219 list_element = doc.createElement(self.list_name) 220 element.appendChild(list_element) 221 222 # Set the list attributes. 223 list_element.setAttribute('desc', self.list_desc) 224 225 # Blacklisted objects. 226 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()) 227 228 # Add all simple python objects within the list to the list element. 229 fill_object_contents(doc, list_element, object=self, blacklist=blacklist) 230 231 # Loop over the list. 232 for i in xrange(len(self)): 233 # The element has its own to_xml() method. 234 if hasattr(self[i], 'to_xml'): 235 self[i].to_xml(doc, list_element) 236 237 # Normal element. 238 else: 239 # Create an XML element for each container. 240 list_item_element = doc.createElement(self.element_name) 241 list_element.appendChild(list_item_element) 242 list_item_element.setAttribute('index', repr(i)) 243 list_item_element.setAttribute('desc', self.element_desc) 244 245 # Blacklisted objects. 246 blacklist = list(self[i].__class__.__dict__.keys()) 247 248 # Add objects which have to_xml() methods. 249 for name in dir(self[i]): 250 # Skip blacklisted objects. 251 if name in blacklist: 252 continue 253 254 # Skip special objects. 255 if search('^_', name): 256 continue 257 258 # Execute any to_xml() methods, and add that object to the blacklist. 259 obj = getattr(self[i], name) 260 if hasattr(obj, 'to_xml'): 261 obj.to_xml(doc, list_item_element) 262 blacklist = blacklist + [name] 263 264 # Add all simple python objects within the container to the XML element. 265 fill_object_contents(doc, list_item_element, object=self[i], blacklist=blacklist)
266