Passed
Push — master ( d9f660...c4ca4f )
by Konstantin
03:02
created

ocrd_models.ocrd_agent.OcrdAgent.notes()   B

Complexity

Conditions 6

Size

Total Lines 9
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 9
rs 8.6666
c 0
b 0
f 0
cc 6
nop 2
1
"""
2
API to ``mets:agent``
3
"""
4
#  import os
5
from .constants import NAMESPACES as NS, TAG_METS_AGENT, TAG_METS_NAME, TAG_METS_NOTE
6
from .ocrd_xml_base import ET
7
8
class OcrdAgent():
9
    """
10
    Represents a <mets:agent>
11
    """
12
13
    #  @staticmethod
14
    #  from_el(el):
15
    #      role = el_agent.get('ROLE')
16
    #      _type = el_agent.get('TYPE')
17
    #      otherrole = el_agent.get('OTHERROLE')
18
    #      name_parts = string.split(el.find('mets:name', NS).text, ' ', 2)
19
    #      #  name = name_parts[0]
20
    #      #  version = name_parts[1][1:]     # v0.0.1 => 0.0.1
21
    #      return OcrdAgent(el, name, role, _type, otherrole)
22
23
    def __init__(self, el=None, name=None, _type=None, othertype=None, role=None, otherrole=None,
24
                 notes=None):
25
        """
26
        Args:
27
            el (LxmlElement):
28
            name (string):
29
            _type (string):
30
            othertype (string):
31
            role (string):
32
            otherrole (string):
33
            notes (dict):
34
        """
35
        if el is None:
36
            el = ET.Element(TAG_METS_AGENT)
37
        self._el = el
38
        self.name = name
39
        self.type = _type
40
        self.othertype = othertype
41
        self.role = role
42
        self.otherrole = otherrole
43
        self.notes = notes
44
45
    def __str__(self):
46
        """
47
        String representation
48
        """
49
        props = ', '.join([
50
            '='.join([k, getattr(self, k) if getattr(self, k) else '---'])
51
            for k in ['type', 'othertype', 'role', 'otherrole', 'name']
52
        ])
53
        return '<OcrdAgent [' + props + ']/>'
54
55
    @property
56
    def type(self):
57
        """
58
        Get the ``TYPE`` attribute value.
59
        """
60
        return self._el.get('TYPE')
61
62
    @type.setter
63
    def type(self, _type):
64
        """
65
        Set the ``TYPE`` attribute value.
66
        """
67
        if _type is not None:
68
            self._el.set('TYPE', _type)
69
70
    @property
71
    def othertype(self):
72
        """
73
        Get the ``OTHERTYPE`` attribute value.
74
        """
75
        return self._el.get('OTHERTYPE')
76
77
    @othertype.setter
78
    def othertype(self, othertype):
79
        """
80
        Set the ``OTHERTYPE`` attribute value.
81
        """
82
        if othertype is not None:
83
            self._el.set('TYPE', 'OTHER')
84
            self._el.set('OTHERTYPE', othertype)
85
86
    @property
87
    def role(self):
88
        """
89
        Get the ``ROLE`` attribute value.
90
        """
91
        return self._el.get('ROLE')
92
93
    @role.setter
94
    def role(self, role):
95
        """
96
        Set the ``ROLE`` attribute value.
97
        """
98
        if role is not None:
99
            self._el.set('ROLE', role)
100
101
    @property
102
    def otherrole(self):
103
        """
104
        Get the ``OTHERROLE`` attribute value.
105
        """
106
        return self._el.get('OTHERROLE')
107
108
    @otherrole.setter
109
    def otherrole(self, otherrole):
110
        """
111
        Set the ``OTHERROLE`` attribute value.
112
        """
113
        if otherrole is not None:
114
            self._el.set('ROLE', 'OTHER')
115
            self._el.set('OTHERROLE', otherrole)
116
117
    @property
118
    def name(self):
119
        """
120
        Get the ``mets:name`` element value.
121
        """
122
        el_name = self._el.find(TAG_METS_NAME)
123
        if el_name is not None:
124
            return el_name.text
125
126
    @name.setter
127
    def name(self, name):
128
        """
129
        Set the ``mets:name`` element value.
130
        """
131
        if name is not None:
132
            el_name = self._el.find(TAG_METS_NAME)
133
            if el_name is None:
134
                el_name = ET.SubElement(self._el, TAG_METS_NAME)
135
            el_name.text = name
136
137
    @property
138
    def notes(self):
139
        """
140
        Get the ``mets:note`` element values (as tuples of attributes and text).
141
        """
142
        el_notes = self._el.findall(TAG_METS_NOTE)
143
        if el_notes is not None:
144
            return [(note.attrib, note.text)
145
                    for note in el_notes]
146
147
    @notes.setter
148
    def notes(self, notes):
149
        """
150
        Set the ``mets:note`` element values.
151
        """
152
        el_notes = self._el.findall(TAG_METS_NOTE)
153
        if el_notes:
154
            for el_note in el_notes:
155
                self._el.remove(el_note)
156
        if notes:
157
            for note in notes:
158
                el_note = ET.SubElement(self._el, TAG_METS_NOTE, nsmap={'ocrd': NS['ocrd']})
159
                attrib, text = note
160
                el_note.text = text
161
                for name, value in attrib.items():
162
                    el_note.set('{%s}' % NS["ocrd"] + name, value)
163