Completed
Pull Request — master (#32)
by Oleg
01:49
created

PartNode   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
dl 0
loc 68
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get_hierarchy_level() 0 7 1
A get_command() 0 7 1
A __init__() 0 8 1
A number() 0 15 2
A set_part_numeration() 0 17 2
1
"""
2
SDoc
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
# ----------------------------------------------------------------------------------------------------------------------
9
from sdoc.sdoc2 import node_store, in_scope, out_scope
10
from sdoc.sdoc2.node.HeadingNode import HeadingNode
11
12
13
class PartNode(HeadingNode):
14
    """
15
    SDoc2 node for parts.
16
    """
17
18
    # ------------------------------------------------------------------------------------------------------------------
19
    def __init__(self, options, argument):
20
        """
21
        PartNode constructor
22
23
        :param dict[str, str] options: The options of this part.
24
        :param str argument: The title of this part.
25
        """
26
        super().__init__('part', options, argument)
27
28
    # ------------------------------------------------------------------------------------------------------------------
29
    def get_command(self):
30
        """
31
        Returns command of this node (i.e. 'part').
32
33
        :rtype: str
34
        """
35
        return 'part'
36
37
    # ------------------------------------------------------------------------------------------------------------------
38
    def get_hierarchy_level(self, parent_hierarchy_level=-1):
39
        """
40
        Returns 0.
41
42
        :rtype: int
43
        """
44
        return 0
45
46
    # ------------------------------------------------------------------------------------------------------------------
47
    @staticmethod
48
    def set_part_numeration(enumerable_numbers):
49
        """
50
        Sets and returns the part number. If we already haven't got a part, we set it to 1 (i.e. first part).
51
        If we already have part, we increment part number, and reset heading nodes numbering.
52
53
        This method changes original list with values!
54
55
        :param dict[str,str] enumerable_numbers: The current numbers of enumerable nodes.
56
57
        :rtype: str
58
        """
59
        if 'part' not in enumerable_numbers:
60
            enumerable_numbers['part'] = '1'
61
        else:
62
            del enumerable_numbers['heading']
63
            enumerable_numbers['part'] = str(int(enumerable_numbers['part']) + 1)
64
65
    # ------------------------------------------------------------------------------------------------------------------
66
    def number(self, enumerable_numbers):
67
        """
68
        Sets number of part nodes.
69
70
        :param dict[str,str] enumerable_numbers: The current numbers of enumerable nodes.
71
        """
72
        self.set_part_numeration(enumerable_numbers)
73
        self._options['number'] = self._trim_levels(enumerable_numbers['part'])
74
75
        for node_id in self.child_nodes:
76
            node = in_scope(node_id)
77
78
            node.number(enumerable_numbers)
79
80
            out_scope(node)
81
82
# ----------------------------------------------------------------------------------------------------------------------
83
node_store.register_inline_command('part', PartNode)
84