Completed
Push — master ( 793cf7...172351 )
by P.R.
04:33
created

Formatter._error()   A

Complexity

Conditions 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 9.762

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 1
cts 11
cp 0.0909
rs 9.4285
cc 3
crap 9.762
1
"""
2
SDoc
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
# ----------------------------------------------------------------------------------------------------------------------
9 1
from sdoc.sdoc2 import node_store, in_scope, out_scope
10
11
12 1
class Formatter:
13
    """
14
    Abstract parent class for all formatters for generating the output of nodes in a requested format.
15
    """
16
17
    # ------------------------------------------------------------------------------------------------------------------
18 1
    def __init__(self, io, parent):
19
        """
20
        Object constructor.
21
22
        :param cleo.styles.output_style.OutputStyle io: The IO object.
23
        :param sdoc.sdoc2.formatter.Formatter.Formatter parent: The formatter for the parent node.
24
        """
25
        self._io = io
26
        """
27
        The IO object.
28
29
        :type: cleo.styles.output_style.OutputStyle
30
        """
31
32
        self._parent = parent
33
        """
34
        The formatter for the parent node.
35
36
        :type: sdoc.sdoc2.formatter.Formatter.Formatter
37
        """
38
39
        self._errors = 0
40
        """
41
        The error count.
42
43
        :type: int
44
        """
45
46
    # ------------------------------------------------------------------------------------------------------------------
47 1
    @property
48
    def errors(self):
49
        """
50
        Getter for the error count.
51
52
        :rtype: int
53
        """
54
        if self._parent:
55
            return self._parent.errors
56
57
        return self._errors
58
59
    # ------------------------------------------------------------------------------------------------------------------
60 1
    def _error(self, message, node=None):
61
        """
62
        Logs an error.
63
64
        :param str message: The error message.this message will be appended with 'at filename:line.column' ot the token.
65
        :param sdoc.sdoc2.node.Node.Node node: The node where the error occurred.
66
        """
67
        if self._parent:
68
            self._parent._error(message, node)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _error was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
69
        else:
70
            self._errors += 1
71
72
            messages = [message]
73
            if node:
74
                filename = node.position.file_name
75
                line_number = node.position.start_line
76
                column_number = node.position.start_column + 1
77
                messages.append('Position: {0!s}:{1:d}.{2:d}'.format(filename, line_number, column_number))
78
            self._io.error(messages)
79
80
    # ------------------------------------------------------------------------------------------------------------------
81 1
    def generate(self, node, file):
82
        """
83
        Generates the representation of a node is the requested output format.
84
85
        :param sdoc.sdoc2.node.Node.Node node: The node for which the output must be generated.
86
        :param file file: The output file.
87
        """
88
        for node_id in node.child_nodes:
89
            child_node = in_scope(node_id)
90
91
            formatter = node_store.create_formatter(self._io, child_node.get_command(), self)
92
            formatter.generate(child_node, file)
93
94
            out_scope(child_node)
95
96
97
# ----------------------------------------------------------------------------------------------------------------------
98