Completed
Pull Request — master (#49)
by Oleg
02:03
created

DocumentNode.setup_document_info()   A

Complexity

Conditions 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 7.456

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 17
ccs 4
cts 10
cp 0.4
rs 9.2
cc 4
crap 7.456
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.Node import Node
12 1
from sdoc.sdoc2.node.DateNode import DateNode
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 = None
33
        """
34
        The title of the sdoc document.
35
36
        :type: sdoc.sdoc2.node.TitleNode.TitleNode
37
        """
38
39 1
        self.date_node = None
40
        """
41
        The date of the sdoc document.
42
43
        :type: sdoc.sdoc2.node.DateNode.DateNode
44
        """
45
46 1
        self.version_node = None
47 1
        """
48
        The version of the sdoc document.
49
50
        :type: sdoc.sdoc2.node.VersionNode.VersionNode
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
            self.setup_document_info(node)
124 1
            node.prepare_content_tree()
125
126 1
            out_scope(node)
127
128 1
        self.remove_nodes_from_child()
129
130
    # ------------------------------------------------------------------------------------------------------------------
131 1
    def setup_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_attr(self.date_node, node)
139
            self.date_node = node
140
141 1
        elif isinstance(node, TitleNode):
142
            self.check_attr(self.title_node, node)
143
            self.title_node = node
144
145 1
        elif isinstance(node, VersionNode):
146
            self.check_attr(self.version_node, node)
147
            self.version_node = node
148
149
    # ------------------------------------------------------------------------------------------------------------------
150 1
    @staticmethod
151
    def check_attr(attr, node):
152
        """
153
        Checks if attribute is set. If it set, and we want to set it again, we raise an error.
154
155
        :param sdoc.sdoc2.node.Node.Node attr: The attribute which we check.
156
        :param sdoc.sdoc2.node.Node.Node node: The node which we want to set.
157
        """
158
        if attr:
159
            NodeStore.error("There are more than 1 {}'s are set. Please Fix it.".format(node.name))
160
161
    # ------------------------------------------------------------------------------------------------------------------
162 1
    def remove_nodes_from_child(self):
163
        """
164
        Removes the node from a child list node.
165
        """
166 1
        for node_id in (self.date_node, self.title_node, self.version_node):
167 1
            if node_id:
168
                self.child_nodes.remove(node_id.id)
169
170
# ----------------------------------------------------------------------------------------------------------------------
171
NodeStore.register_block_command('document', DocumentNode)
172