1
|
|
|
""" |
2
|
|
|
Enarksh |
3
|
|
|
|
4
|
|
|
Copyright 2013-2016 Set Based IT Consultancy |
5
|
|
|
|
6
|
|
|
Licence MIT |
7
|
|
|
""" |
8
|
|
|
import abc |
9
|
|
|
from xml.etree.ElementTree import Element |
|
|
|
|
10
|
|
|
|
11
|
|
|
from enarksh.XmlReader.Dependency import Dependency |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class Port: |
|
|
|
|
15
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
16
|
|
|
def __init__(self, node): |
17
|
|
|
self._dependencies = [] |
18
|
|
|
""" |
19
|
|
|
The dependencies of this port. |
20
|
|
|
|
21
|
|
|
:type: list |
22
|
|
|
""" |
23
|
|
|
|
24
|
|
|
self._node = node |
25
|
|
|
""" |
26
|
|
|
The node (owner) of this port. |
27
|
|
|
|
28
|
|
|
:type: Node |
29
|
|
|
""" |
30
|
|
|
|
31
|
|
|
self._port_name = '' |
32
|
|
|
""" |
33
|
|
|
The port name of this port. |
34
|
|
|
|
35
|
|
|
:type: str |
36
|
|
|
""" |
37
|
|
|
|
38
|
|
|
self._prt_id = 0 |
39
|
|
|
""" |
40
|
|
|
The ID of this port when it is stored in the database. |
41
|
|
|
|
42
|
|
|
:type: int |
43
|
|
|
""" |
44
|
|
|
|
45
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
46
|
|
|
def get_prt_id(self): |
47
|
|
|
""" |
48
|
|
|
Returns the ID of this port. |
49
|
|
|
|
50
|
|
|
:rtype: int |
51
|
|
|
""" |
52
|
|
|
return self._prt_id |
53
|
|
|
|
54
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
55
|
|
|
def get_name(self): |
56
|
|
|
""" |
57
|
|
|
Returns the name of this port. |
58
|
|
|
|
59
|
|
|
:rtype: str |
60
|
|
|
""" |
61
|
|
|
return self._port_name |
62
|
|
|
|
63
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
64
|
|
|
def get_node(self): |
65
|
|
|
""" |
66
|
|
|
Returns the node of this port. |
67
|
|
|
|
68
|
|
|
:rtype: Node |
69
|
|
|
""" |
70
|
|
|
return self._node |
71
|
|
|
|
72
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
73
|
|
|
def read_xml(self, xml): |
74
|
|
|
""" |
75
|
|
|
:param xml.etree.ElementTree.Element xml: |
76
|
|
|
""" |
77
|
|
|
for element in list(xml): |
78
|
|
|
self.read_xml_element(element) |
79
|
|
|
|
80
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
81
|
|
|
def read_xml_element(self, xml): |
82
|
|
|
""" |
83
|
|
|
:param xml.etree.ElementTree.Element xml: |
84
|
|
|
""" |
85
|
|
|
tag = xml.tag |
86
|
|
|
if tag == 'PortName': |
87
|
|
|
self._port_name = xml.text |
88
|
|
|
|
89
|
|
|
elif tag == 'Dependencies': |
90
|
|
|
self._read_xml_dependencies(xml) |
91
|
|
|
|
92
|
|
|
else: |
93
|
|
|
raise Exception("Unexpected tag '{0!s}'.".format(tag)) |
94
|
|
|
|
95
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
96
|
|
|
def _read_xml_dependencies(self, xml): |
97
|
|
|
""" |
98
|
|
|
:param xml.etree.ElementTree.Element xml: |
99
|
|
|
""" |
100
|
|
|
for element in list(xml): |
101
|
|
|
tag = element.tag |
102
|
|
|
if tag == 'Dependency': |
103
|
|
|
dependency = Dependency(self) |
104
|
|
|
dependency.read_xml(element) |
105
|
|
|
self._dependencies.append(dependency) |
106
|
|
|
|
107
|
|
|
else: |
108
|
|
|
raise Exception("Unexpected tag '{0!s}'.".format(tag)) |
109
|
|
|
|
110
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
111
|
|
|
def validate(self, errors): |
112
|
|
|
""" |
113
|
|
|
Validates this port against rules which are not imposed by XSD. |
114
|
|
|
|
115
|
|
|
:param list errors: A list of error messages. |
116
|
|
|
""" |
117
|
|
|
for dependency in self._dependencies: |
118
|
|
|
dependency.validate(errors) |
119
|
|
|
|
120
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
121
|
|
|
@abc.abstractmethod |
122
|
|
|
def store(self, nod_id): |
123
|
|
|
""" |
124
|
|
|
Stores the definition of this port into the database. |
125
|
|
|
|
126
|
|
|
:param int nod_id: The ID of the node to which this node belongs |
127
|
|
|
""" |
128
|
|
|
pass |
129
|
|
|
|
130
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
131
|
|
|
@abc.abstractmethod |
132
|
|
|
def store_dependencies(self): |
133
|
|
|
""" |
134
|
|
|
Stores the dependencies of this port into the database. |
135
|
|
|
""" |
136
|
|
|
pass |
137
|
|
|
|
138
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
139
|
|
|
|