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