-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcolumns_definition.py
118 lines (92 loc) · 2.81 KB
/
columns_definition.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
definition = dict(
port={
'xpath': '.',
'children': dict(
number={
'attribute': 'portid'
},
protocol={
'attribute': 'protocol'
}
),
},
state={
'attribute': 'state',
'xpath': 'state'
},
service={
'xpath': 'service',
'children': dict(
name={
'attribute': 'name'
},
product={
'attribute': 'product'
},
version={
'attribute': 'version'
}
)
}
)
class Element:
name = None
xpath = ""
parent = None
children = []
attribute = None
text = False
level = 0
def __init__(self, key):
self.name = key
def xpathfull(self, xpath=None):
"""Return a full xPath for the element"""
if xpath is None:
xpath = []
xpath.append(self.xpath)
if self.parent:
return self.parent.xpathfull(xpath)
return "".join(reversed(xpath))
def data(self, xml_element, default=""):
"""Get data from the element depending on the "type" of element,
either it's attribute data or the text value from the XML element
"""
if self.text:
if xml_element.text:
return xml_element.text
else:
return default
if self.attribute:
return xml_element.get(self.attribute, default)
return default
def find(self, key):
if key == self.name:
return self
splitted_names = key.split(".")
for k in splitted_names:
if self.name == k:
if self.children:
for child in self.children:
elem = child.find(".".join(splitted_names[1:]))
if elem:
return elem
return None
@staticmethod
def build(definition_object, parent=None):
elements = []
for _, key in enumerate(definition_object):
elem = Element(key)
if 'xpath' in definition_object[key]:
elem.xpath = definition_object[key]['xpath']
if 'text' in definition_object[key]:
elem.text = definition_object[key]['text']
if parent:
elem.level = parent.level + 1
elem.parent = parent
if 'attribute' in definition_object[key]:
elem.attribute = definition_object[key]['attribute']
# Recursively build children elements
if 'children' in definition_object[key]:
elem.children = Element.build(definition_object[key]['children'], elem)
elements.append(elem)
return elements