-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlobject.py
95 lines (85 loc) · 2.74 KB
/
xmlobject.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/python
#
# CLUES ONE Connector - ONE Connector for Cluster Energy Saving System
# Copyright (C) 2011 - GRyCAP - Universitat Politecnica de Valencia
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import xml.dom.minidom
import logging
class XMLObject:
tuples = {}
tuples_lists = {}
values = []
values_lists = []
numeric = []
noneval = None
@staticmethod
def getText(nodelist):
rc = []
for node in nodelist:
if node.nodeType == node.TEXT_NODE or node.nodeType == node.CDATA_SECTION_NODE:
rc.append(node.data)
return ''.join(rc)
@staticmethod
def handleField(fieldName, VM):
try:
fieldElements = VM.getElementsByTagName(fieldName)[0]
return XMLObject.getText(fieldElements.childNodes)
except:
return None
@staticmethod
def handleFieldAsList(fieldName, VM):
try:
fieldElements = VM.getElementsByTagName(fieldName)
local_list = []
for fieldElement in fieldElements:
local_list.append(XMLObject.getText(fieldElement.childNodes))
return local_list
except:
return []
def __setattr__(self, name, value):
self.__dict__[name] = value
def __init__(self, xml_str):
dom = xml.dom.minidom.parseString(xml_str)
for tag, className in self.__class__.tuples.items():
objs = dom.getElementsByTagName(tag)
if (len(objs) > 0):
newObj = className(objs[0].toxml())
dom.childNodes[0].removeChild(objs[0])
else:
newObj = None
self.__setattr__(tag, newObj)
for tag, className in self.__class__.tuples_lists.items():
objs = dom.getElementsByTagName(tag)
obj_list = []
for obj in objs:
newObj = className(obj.toxml())
dom.childNodes[0].removeChild(obj)
obj_list.append(newObj)
self.__setattr__(tag, obj_list)
for tag in self.__class__.values_lists:
self.__setattr__(tag, XMLObject.handleFieldAsList(tag, dom))
for tag in self.__class__.values:
value = XMLObject.handleField(tag, dom)
if (value is None):
value = self.noneval
if (tag in self.__class__.numeric):
try:
value = float(value)
if (value == int(value)):
value = int(value)
except:
logging.warning("numeric value expected for %s - found %s" % (tag, value))
self.__setattr__(tag, value)