|
1
|
|
|
import abc |
|
|
|
|
|
|
2
|
|
|
from xml.etree.ElementTree import Element |
|
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
class Resource: |
|
|
|
|
|
|
6
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
7
|
|
|
def __init__(self, node): |
|
8
|
|
|
self._node = node |
|
9
|
|
|
""" |
|
10
|
|
|
The node that owns this resource. |
|
11
|
|
|
|
|
12
|
|
|
:type: Node |
|
13
|
|
|
""" |
|
14
|
|
|
|
|
15
|
|
|
self._resource_name = '' |
|
16
|
|
|
""" |
|
17
|
|
|
The name of this resource. |
|
18
|
|
|
|
|
19
|
|
|
:type: str |
|
20
|
|
|
""" |
|
21
|
|
|
|
|
22
|
|
|
self._rsc_id = 0 |
|
23
|
|
|
""" |
|
24
|
|
|
The ID of this resource when it is stored in the databases. |
|
25
|
|
|
|
|
26
|
|
|
:type: int |
|
27
|
|
|
""" |
|
28
|
|
|
|
|
29
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
30
|
|
|
@property |
|
31
|
|
|
def name(self): |
|
32
|
|
|
""" |
|
33
|
|
|
Returns the name of this resource. |
|
34
|
|
|
|
|
35
|
|
|
:rtype: str |
|
36
|
|
|
""" |
|
37
|
|
|
return self._resource_name |
|
38
|
|
|
|
|
39
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
40
|
|
|
@property |
|
41
|
|
|
def rsc_id(self): |
|
42
|
|
|
""" |
|
43
|
|
|
Returns the ID of this resource. |
|
44
|
|
|
|
|
45
|
|
|
:rtype: str |
|
46
|
|
|
""" |
|
47
|
|
|
return self._rsc_id |
|
48
|
|
|
|
|
49
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
50
|
|
|
def read_xml(self, xml): |
|
51
|
|
|
""" |
|
52
|
|
|
:param xml.etree.ElementTree.Element xml: |
|
53
|
|
|
""" |
|
54
|
|
|
for element in list(xml): |
|
55
|
|
|
self.read_xml_element(element) |
|
56
|
|
|
|
|
57
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
58
|
|
|
def read_xml_element(self, xml): |
|
59
|
|
|
""" |
|
60
|
|
|
:param xml.etree.ElementTree.Element xml: |
|
61
|
|
|
""" |
|
62
|
|
|
tag = xml.tag |
|
63
|
|
|
if tag == 'ResourceName': |
|
64
|
|
|
self._resource_name = xml.text |
|
65
|
|
|
|
|
66
|
|
|
else: |
|
67
|
|
|
Resource.read_xml_element(self, xml) |
|
68
|
|
|
|
|
69
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
70
|
|
|
def get_uri(self, obj_type='resource'): |
|
71
|
|
|
""" |
|
72
|
|
|
Returns the URI of this resource. |
|
73
|
|
|
|
|
74
|
|
|
:param str obj_type: The entity type. |
|
75
|
|
|
|
|
76
|
|
|
:rtype: str |
|
77
|
|
|
""" |
|
78
|
|
|
if self._node: |
|
79
|
|
|
uri = self._node.get_uri(obj_type) |
|
80
|
|
|
else: |
|
81
|
|
|
uri = '//' + obj_type |
|
82
|
|
|
|
|
83
|
|
|
return uri + '/' + self._resource_name |
|
84
|
|
|
|
|
85
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
86
|
|
|
@abc.abstractmethod |
|
87
|
|
|
def store(self, hst_id, nod_id): |
|
88
|
|
|
""" |
|
89
|
|
|
:param int hst_id: |
|
90
|
|
|
:param int nod_id: |
|
91
|
|
|
""" |
|
92
|
|
|
pass |
|
93
|
|
|
|
|
94
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
95
|
|
|
def validate(self, errors): |
|
96
|
|
|
""" |
|
97
|
|
|
Validates this resource against rules which are not imposed by XSD. |
|
98
|
|
|
|
|
99
|
|
|
:param list errors: A list of error messages. |
|
100
|
|
|
""" |
|
101
|
|
|
# Nothing to do. |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
|
105
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.