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

Source Code for Module data.relax_xml

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2008-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  """Module containing generic fns for creation and parsing of XML representations of python objects.""" 
 25   
 26  # Python module imports. 
 27  from numpy import set_printoptions, array, float32, float64, inf, nan 
 28  try: 
 29      from numpy import float16 
 30  except ImportError: 
 31      float16 = float32    # Support for old numpy versions. 
 32  try: 
 33      from numpy import float128 
 34  except ImportError: 
 35      float128 = float64    # Support for 32-bit numpy versions. 
 36  from re import search 
 37  from string import strip 
 38   
 39  # Modify numpy for better output of numbers and structures. 
 40  set_printoptions(precision=15, threshold=nan) 
 41   
 42  # relax module imports. 
 43  from float import floatAsByteArray, packBytesAsPyFloat 
 44   
 45   
46 -def fill_object_contents(doc, elem, object=None, blacklist=[]):
47 """Place all simple python objects into the XML element namespace. 48 49 @param doc: The XML document object. 50 @type doc: xml.dom.minidom.Document instance 51 @param elem: The element to add all python objects to. 52 @type elem: XML element object 53 @keyword object: The python class instance containing the objects to add. 54 @type object: instance 55 @keyword blacklist: A list of object names to exclude. 56 @type blacklist: list of str 57 """ 58 59 # Loop over the elements of the object. 60 for name in dir(object): 61 # Skip blacklisted objects. 62 if name in blacklist: 63 continue 64 65 # Skip special objects. 66 if search("^_", name): 67 continue 68 69 # Only pack objects in the __mod_attr__ list, if that list exists. 70 if hasattr(object, '__mod_attr__') and name not in object.__mod_attr__: 71 continue 72 73 # Create a new element for this object, and add it to the main element. 74 sub_elem = doc.createElement(name) 75 elem.appendChild(sub_elem) 76 77 # Get the sub-object. 78 subobj = getattr(object, name) 79 80 # Store floats as IEEE-754 byte arrays (for full precision storage). 81 if isinstance(subobj, float) or isinstance(subobj, float16) or isinstance(subobj, float32) or isinstance(subobj, float64) or isinstance(subobj, float128): 82 sub_elem.setAttribute('ieee_754_byte_array', repr(floatAsByteArray(subobj))) 83 84 # Add the text value to the sub element. 85 text_val = doc.createTextNode(repr(subobj)) 86 sub_elem.appendChild(text_val)
87 88
89 -def node_value_to_python(elem):
90 """Convert the node value to a python expression. 91 92 @param elem: The XML element. 93 @type elem: xml.dom.minidom.Element instance 94 """ 95 96 # Remove whitespace. 97 val = strip(elem.nodeValue) 98 99 # Convert to python and return. 100 return eval(val)
101 102
103 -def xml_to_object(elem, base_object=None, set_fn=None, blacklist=[]):
104 """Convert the XML elements into python objects, and place these into the base object. 105 106 @param elem: The element to extract all python objects from. 107 @type elem: xml.dom.minidom.Element instance 108 @keyword base_object: The python class instance to place the objects into. 109 @type base_object: instance 110 @keyword set_fn: A function used to replace setattr for placing the object into the base 111 object. 112 @type set_fn: function 113 @keyword blacklist: A list of object names to exclude. 114 @type blacklist: list of str 115 """ 116 117 # Loop over the nodes of the element 118 for node in elem.childNodes: 119 # Skip empty nodes. 120 if node.localName == None: 121 continue 122 123 # The name of the python object to recreate. 124 name = str(node.localName) 125 126 # Skip blacklisted objects. 127 if name in blacklist: 128 continue 129 130 # IEEE-754 floats (for full precision restoration). 131 ieee_array = node.getAttribute('ieee_754_byte_array') 132 if ieee_array: 133 val = packBytesAsPyFloat(eval(ieee_array)) 134 135 # Get the node contents. 136 else: 137 val = node_value_to_python(node.childNodes[0]) 138 139 # Set the value. 140 setattr(base_object, name, val)
141