Completed
Push — master ( 0ea567...c0c100 )
by P.R.
02:52
created

DocumentNode.prepare_content_tree()   A

Complexity

Conditions 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
crap 2
1
"""
2
SDoc
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
# ----------------------------------------------------------------------------------------------------------------------
9 1
from sdoc.sdoc2 import in_scope, out_scope
10 1
from sdoc.sdoc2.NodeStore import NodeStore
11 1
from sdoc.sdoc2.node.DateNode import DateNode
12 1
from sdoc.sdoc2.node.Node import Node
13 1
from sdoc.sdoc2.node.TitleNode import TitleNode
14 1
from sdoc.sdoc2.node.VersionNode import VersionNode
15
16
17 1
class DocumentNode(Node):
18
    """
19
    SDoc2 node for documents.
20
    """
21
22
    # ------------------------------------------------------------------------------------------------------------------
23 1
    def __init__(self, io, options):
24
        """
25
        Object constructor.
26
27
        :param None|cleo.styles.output_style.OutputStyle io: The IO object.
28
        :param dict[str,str] options: The options of this document.
29
        """
30 1
        super().__init__(io, 'document', options)
31
32 1
        self.title_node_id = None
33
        """
34
        The ID of the node the title of the sdoc document.
35
36
        :type: int|None
37
        """
38
39 1
        self.date_node_id = None
40
        """
41
        The ID of the node the date of the sdoc document.
42
43
        :type: int|None
44
        """
45
46 1
        self.version_node_id = None
47 1
        """
48
        The ID of the node with the version of the sdoc document.
49
50
        :type: int|None
51
        """
52
53
    # ------------------------------------------------------------------------------------------------------------------
54 1
    def get_command(self):
55
        """
56
        Returns the command of this node, i.e. document.
57
58
        :rtype: str
59
        """
60 1
        return 'document'
61
62
    # ------------------------------------------------------------------------------------------------------------------
63 1
    def get_hierarchy_level(self, parent_hierarchy_level=-1):
64
        """
65
        Returns 0.
66
67
        :rtype: int
68
        """
69 1
        return 0
70
71
    # ------------------------------------------------------------------------------------------------------------------
72 1
    def get_hierarchy_name(self):
73
        """
74
        Returns 'sectioning'.
75
76
        :rtype: str
77
        """
78 1
        return 'sectioning'
79
80
    # ------------------------------------------------------------------------------------------------------------------
81 1
    def is_block_command(self):
82
        """
83
        Returns True.
84
85
        :rtype: bool
86
        """
87 1
        return True
88
89
    # ------------------------------------------------------------------------------------------------------------------
90 1
    def is_document_root(self):
91
        """
92
        Returns True.
93
        :rtype: bool
94
        """
95 1
        return True
96
97
    # ------------------------------------------------------------------------------------------------------------------
98 1
    def is_inline_command(self):
99
        """
100
        Returns False.
101
102
        :rtype: bool
103
        """
104
        return False
105
106
    # ------------------------------------------------------------------------------------------------------------------
107 1
    def is_phrasing(self):
108
        """
109
        Returns False.
110
111
        :rtype: bool
112
        """
113
        return False
114
115
    # ------------------------------------------------------------------------------------------------------------------
116 1
    def prepare_content_tree(self):
117
        """
118
        Prepares this node for further processing.
119
        """
120 1
        for node_id in self.child_nodes:
121 1
            node = in_scope(node_id)
122
123 1
            node.prepare_content_tree()
124 1
            self.__set_document_info(node)
125
126 1
            out_scope(node)
127
128 1
        self.__remove_document_info_nodes()
129
130
    # ------------------------------------------------------------------------------------------------------------------
131 1
    def __set_document_info(self, node):
132
        """
133
        Sets the info of a document (i.e. date, version or title) to DocumentNode attributes.
134
135
        :param sdoc.sdoc2.node.Node.Node node: The current node.
136
        """
137 1
        if isinstance(node, DateNode):
138
            self.__check_document_info(self.date_node_id, node)
139
            self.date_node_id = node.id
140
141 1
        elif isinstance(node, TitleNode):
142
            self.__check_document_info(self.title_node_id, node)
143
            self.title_node_id = node.id
144
145 1
        elif isinstance(node, VersionNode):
146
            self.__check_document_info(self.version_node_id, node)
147
            self.version_node_id = node.id
148
149
    # ------------------------------------------------------------------------------------------------------------------
150 1
    @staticmethod
151
    def __check_document_info(info_node_current, info_node_new):
152
        """
153
        Checks if a document info node has been set already. If so, an error will be logged.
154
155
        :param int|None info_node_current: The current document info node (i.e. a property of the document).
156
        :param sdoc.sdoc2.node.Node.Node info_node_new: The (new) document info node.
157
        """
158
        if info_node_current:
159
            node = in_scope(info_node_current)
160
            position = node.position
161
            out_scope(node)
162
163
            NodeStore.error("Document info {0} can be specified only once. Previous definition at {1}.".format(
164
                info_node_new.name, str(position)), info_node_new)
165
166
    # ------------------------------------------------------------------------------------------------------------------
167 1
    def __remove_document_info_nodes(self):
168
        """
169
        Removes the nodes with document info from the list of child nodes.
170
        """
171 1
        node_ids = [self.date_node_id, self.title_node_id, self.version_node_id]
172 1
        node_ids = [node_id for node_id in node_ids if node_id is not None]
173 1
        self._remove_child_nodes(node_ids)
174
175
176
# ----------------------------------------------------------------------------------------------------------------------
177
NodeStore.register_block_command('document', DocumentNode)
178