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
|
|
|
@property |
47
|
|
|
def name(self): |
48
|
|
|
""" |
49
|
|
|
Returns the name of this port. |
50
|
|
|
|
51
|
|
|
:rtype: str |
52
|
|
|
""" |
53
|
|
|
return self._port_name |
54
|
|
|
|
55
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
56
|
|
|
@property |
57
|
|
|
def node(self): |
58
|
|
|
""" |
59
|
|
|
Returns the node of this port. |
60
|
|
|
|
61
|
|
|
:rtype: Node |
62
|
|
|
""" |
63
|
|
|
return self._node |
64
|
|
|
|
65
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
66
|
|
|
@property |
67
|
|
|
def prt_id(self): |
68
|
|
|
""" |
69
|
|
|
Returns the ID of this port. |
70
|
|
|
|
71
|
|
|
:rtype: int |
72
|
|
|
""" |
73
|
|
|
return self._prt_id |
74
|
|
|
|
75
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
76
|
|
|
def read_xml(self, xml): |
77
|
|
|
""" |
78
|
|
|
:param xml.etree.ElementTree.Element xml: |
79
|
|
|
""" |
80
|
|
|
for element in list(xml): |
81
|
|
|
self.read_xml_element(element) |
82
|
|
|
|
83
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
84
|
|
|
def read_xml_element(self, xml): |
85
|
|
|
""" |
86
|
|
|
:param xml.etree.ElementTree.Element xml: |
87
|
|
|
""" |
88
|
|
|
tag = xml.tag |
89
|
|
|
if tag == 'PortName': |
90
|
|
|
self._port_name = xml.text |
91
|
|
|
|
92
|
|
|
elif tag == 'Dependencies': |
93
|
|
|
self._read_xml_dependencies(xml) |
94
|
|
|
|
95
|
|
|
else: |
96
|
|
|
raise Exception("Unexpected tag '{0!s}'.".format(tag)) |
97
|
|
|
|
98
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
99
|
|
|
def _read_xml_dependencies(self, xml): |
100
|
|
|
""" |
101
|
|
|
:param xml.etree.ElementTree.Element xml: |
102
|
|
|
""" |
103
|
|
|
for element in list(xml): |
104
|
|
|
tag = element.tag |
105
|
|
|
if tag == 'Dependency': |
106
|
|
|
dependency = Dependency(self) |
107
|
|
|
dependency.read_xml(element) |
108
|
|
|
self._dependencies.append(dependency) |
109
|
|
|
|
110
|
|
|
else: |
111
|
|
|
raise Exception("Unexpected tag '{0!s}'.".format(tag)) |
112
|
|
|
|
113
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
114
|
|
|
def validate(self, errors): |
115
|
|
|
""" |
116
|
|
|
Validates this port against rules which are not imposed by XSD. |
117
|
|
|
|
118
|
|
|
:param list errors: A list of error messages. |
119
|
|
|
""" |
120
|
|
|
for dependency in self._dependencies: |
121
|
|
|
dependency.validate(errors) |
122
|
|
|
|
123
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
124
|
|
|
@abc.abstractmethod |
125
|
|
|
def store(self, nod_id): |
126
|
|
|
""" |
127
|
|
|
Stores the definition of this port into the database. |
128
|
|
|
|
129
|
|
|
:param int nod_id: The ID of the node to which this node belongs |
130
|
|
|
""" |
131
|
|
|
pass |
132
|
|
|
|
133
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
134
|
|
|
@abc.abstractmethod |
135
|
|
|
def store_dependencies(self): |
136
|
|
|
""" |
137
|
|
|
Stores the dependencies of this port into the database. |
138
|
|
|
""" |
139
|
|
|
pass |
140
|
|
|
|
141
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
142
|
|
|
|