1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """Module containing generic fns for creation and parsing of XML representations of python objects."""
25
26
27 from numpy import set_printoptions, array, float32, float64, inf, nan
28 try:
29 from numpy import float16
30 except ImportError:
31 float16 = float32
32 try:
33 from numpy import float128
34 except ImportError:
35 float128 = float64
36 from re import search
37 from string import strip
38
39
40 set_printoptions(precision=15, threshold=nan)
41
42
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
60 for name in dir(object):
61
62 if name in blacklist:
63 continue
64
65
66 if search("^_", name):
67 continue
68
69
70 if hasattr(object, '__mod_attr__') and name not in object.__mod_attr__:
71 continue
72
73
74 sub_elem = doc.createElement(name)
75 elem.appendChild(sub_elem)
76
77
78 subobj = getattr(object, name)
79
80
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
85 text_val = doc.createTextNode(repr(subobj))
86 sub_elem.appendChild(text_val)
87
88
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
97 val = strip(elem.nodeValue)
98
99
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
118 for node in elem.childNodes:
119
120 if node.localName == None:
121 continue
122
123
124 name = str(node.localName)
125
126
127 if name in blacklist:
128 continue
129
130
131 ieee_array = node.getAttribute('ieee_754_byte_array')
132 if ieee_array:
133 val = packBytesAsPyFloat(eval(ieee_array))
134
135
136 else:
137 val = node_value_to_python(node.childNodes[0])
138
139
140 setattr(base_object, name, val)
141