Enumerable.get_string()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
eloc 8
dl 15
loc 15
ccs 7
cts 8
cp 0.875
rs 10
c 0
b 0
f 0
cc 4
nop 1
crap 4.0312
1 1
from typing import Dict, Optional
2
3
4 1 View Code Duplication
class Enumerable:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5
    """
6
    Class for storing information about numeration of heading nodes.
7
    """
8
9
    # ------------------------------------------------------------------------------------------------------------------
10 1
    def __init__(self):
11
        """
12
        Object constructor.
13
        """
14
15 1
        self._numerate_data: Dict[int, int] = {}
16 1
        """
17
        The info about node numeration.
18
        """
19
20
    # ------------------------------------------------------------------------------------------------------------------
21 1
    def get_level(self, level: int) -> Optional[int]:
22
        """
23
        Gets the level from numerate data.
24
25
        :param: int level: The level.
26
        """
27 1
        if level in self._numerate_data:
28 1
            return self._numerate_data[level]
29
        else:
30
            return None
31
32
    # ------------------------------------------------------------------------------------------------------------------
33 1
    def generate_numeration(self, level: int) -> None:
34
        """
35
        Sets current enumeration of headings at a heading level.
36
37
        :param int level: The level of nested heading.
38
        """
39 1
        for num in range(level + 1):
40 1
            if num not in self._numerate_data:
41 1
                self._numerate_data[num] = 0
42
43
        # Truncate levels.
44 1
        new_numerate_data = {}
45 1
        for key in self._numerate_data:
46 1
            if key <= level:
47 1
                new_numerate_data[key] = self._numerate_data[key]
48
49 1
        self._numerate_data = new_numerate_data
50
51
    # ------------------------------------------------------------------------------------------------------------------
52 1
    def increment_last_level(self) -> None:
53
        """
54
        Increments the last level in number of a heading number.
55
        """
56 1
        last_level = max(self._numerate_data)
57 1
        self._numerate_data[last_level] += 1
58
59
    # ------------------------------------------------------------------------------------------------------------------
60 1
    def remove_starting_zeros(self) -> None:
61
        """
62
        Removes starting multiple zero symbols. And lefts one if we have omitted levels.
63
        """
64
        # Check if we have level 'bigger' than part and
65
        # if first encountered level is equal 0, we start passing from level '2' to max level.
66 1
        if 1 in self._numerate_data and self._numerate_data[1] == 0:
67 1
            for key in range(2, max(self._numerate_data)):
68
69
                # If we have '0' we remove them until we not encountered '0', otherwise we go out the loop.
70 1
                if self._numerate_data[key] == 0:
71
                    del self._numerate_data[key]
72
                else:
73 1
                    break
74
75
    # ------------------------------------------------------------------------------------------------------------------
76 1
    def get_string(self) -> str:
77
        """
78
        Returns the string equivalent of levels for future output.
79
        """
80 1
        numbering = []
81
82 1
        if max(self._numerate_data) == 0:
83
            return str(self._numerate_data[0])
84
        else:
85 1
            for key in self._numerate_data:
86
                # If we need to generate chapter, subsection ... etc. we omit outputting part number.
87 1
                if key != 0:
88 1
                    numbering.append(self._numerate_data[key])
89
90 1
            return '.'.join(map(str, numbering))
91
92
# ----------------------------------------------------------------------------------------------------------------------
93