Completed
Push — master ( f370bc...1b3b31 )
by P.R.
02:18
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 1
        self._io = io
26
        """
27
        The IO object.
28
29
        :type: cleo.styles.output_style.OutputStyle
30
        """
31
32 1
        self._parent = parent
33
        """
34
        The formatter for the parent node.
35
36
        :type: sdoc.sdoc2.formatter.Formatter.Formatter
37
        """
38
39 1
        self._errors = 0
40 1
        """
41
        The error count.
42
        :type: int
43
        """
44
45
    # ------------------------------------------------------------------------------------------------------------------
46 1
    @property
47
    def errors(self):
48
        """
49
        Getter for the error count.
50
51
        :rtype: int
52
        """
53 1
        if self._parent:
54
            return self._parent.errors
55
56 1
        return self._errors
57
58
    # ------------------------------------------------------------------------------------------------------------------
59 1
    def error(self, message, node=None):
60
        """
61
        Logs an error.
62
63
        :param str message: The error message.this message will be appended with 'at filename:line.column' ot the token.
64
        :param sdoc.sdoc2.node.Node.Node node: The node where the error occurred.
65
        """
66
        if self._parent:
67
            self._parent.error(message, node)
68
        else:
69
            self._errors += 1
70
71
            messages = [message]
72
            if node:
73
                filename = node.position.file_name
74
                line_number = node.position.start_line
75
                column_number = node.position.start_column + 1
76
                messages.append('Position: {0!s}:{1:d}.{2:d}'.format(filename, line_number, column_number))
77
            self._io.error(messages)
78
79
    # ------------------------------------------------------------------------------------------------------------------
80 1
    def generate(self, node, file):
81
        """
82
        Generates the representation of a node is the requested output format.
83
84
        :param sdoc.sdoc2.node.Node.Node node: The node for which the output must be generated.
85
        :param file file: The output file.
86
        """
87 1
        for node_id in node.child_nodes:
88 1
            child_node = in_scope(node_id)
89
90 1
            formatter = node_store.create_formatter(self._io, child_node.get_command(), self)
91 1
            formatter.generate(child_node, file)
92
93 1
            out_scope(child_node)
94
95
# ----------------------------------------------------------------------------------------------------------------------
96