~anitanayak/charms/trusty/ibm-mq/ibm-mq-trusty

« back to all changes in this revision

Viewing changes to .tox/py35/lib/python3.5/site-packages/flake8/formatting/base.py

  • Committer: Anita Nayak
  • Date: 2016-10-24 07:10:08 UTC
  • Revision ID: anitanayak@in.ibm.com-20161024071008-tqk3cefak6nc1c69
checking in after fixing lint errors

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""The base class and interface for all formatting plugins."""
 
2
from __future__ import print_function
 
3
 
 
4
 
 
5
class BaseFormatter(object):
 
6
    """Class defining the formatter interface.
 
7
 
 
8
    .. attribute:: options
 
9
 
 
10
        The options parsed from both configuration files and the command-line.
 
11
 
 
12
    .. attribute:: filename
 
13
 
 
14
        If specified by the user, the path to store the results of the run.
 
15
 
 
16
    .. attribute:: output_fd
 
17
 
 
18
        Initialized when the :meth:`start` is called. This will be a file
 
19
        object opened for writing.
 
20
 
 
21
    .. attribute:: newline
 
22
 
 
23
        The string to add to the end of a line. This is only used when the
 
24
        output filename has been specified.
 
25
    """
 
26
 
 
27
    def __init__(self, options):
 
28
        """Initialize with the options parsed from config and cli.
 
29
 
 
30
        This also calls a hook, :meth:`after_init`, so subclasses do not need
 
31
        to call super to call this method.
 
32
 
 
33
        :param optparse.Values options:
 
34
            User specified configuration parsed from both configuration files
 
35
            and the command-line interface.
 
36
        """
 
37
        self.options = options
 
38
        self.filename = options.output_file
 
39
        self.output_fd = None
 
40
        self.newline = '\n'
 
41
        self.after_init()
 
42
 
 
43
    def after_init(self):
 
44
        """Initialize the formatter further."""
 
45
        pass
 
46
 
 
47
    def start(self):
 
48
        """Prepare the formatter to receive input.
 
49
 
 
50
        This defaults to initializing :attr:`output_fd` if :attr:`filename`
 
51
        """
 
52
        if self.filename:
 
53
            self.output_fd = open(self.filename, 'a')
 
54
 
 
55
    def handle(self, error):
 
56
        """Handle an error reported by Flake8.
 
57
 
 
58
        This defaults to calling :meth:`format`, :meth:`show_source`, and
 
59
        then :meth:`write`. To extend how errors are handled, override this
 
60
        method.
 
61
 
 
62
        :param error:
 
63
            This will be an instance of :class:`~flake8.style_guide.Error`.
 
64
        :type error:
 
65
            flake8.style_guide.Error
 
66
        """
 
67
        line = self.format(error)
 
68
        source = self.show_source(error)
 
69
        self.write(line, source)
 
70
 
 
71
    def format(self, error):
 
72
        """Format an error reported by Flake8.
 
73
 
 
74
        This method **must** be implemented by subclasses.
 
75
 
 
76
        :param error:
 
77
            This will be an instance of :class:`~flake8.style_guide.Error`.
 
78
        :type error:
 
79
            flake8.style_guide.Error
 
80
        :returns:
 
81
            The formatted error string.
 
82
        :rtype:
 
83
            str
 
84
        """
 
85
        raise NotImplementedError('Subclass of BaseFormatter did not implement'
 
86
                                  ' format.')
 
87
 
 
88
    def show_statistics(self, statistics):
 
89
        """Format and print the statistics."""
 
90
        for error_code in statistics.error_codes():
 
91
            stats_for_error_code = statistics.statistics_for(error_code)
 
92
            statistic = next(stats_for_error_code)
 
93
            count = statistic.count
 
94
            count += sum(stat.count for stat in stats_for_error_code)
 
95
            self._write('{count:<5} {error_code} {message}'.format(
 
96
                count=count,
 
97
                error_code=error_code,
 
98
                message=statistic.message,
 
99
            ))
 
100
 
 
101
    def show_benchmarks(self, benchmarks):
 
102
        """Format and print the benchmarks."""
 
103
        # NOTE(sigmavirus24): The format strings are a little confusing, even
 
104
        # to me, so here's a quick explanation:
 
105
        # We specify the named value first followed by a ':' to indicate we're
 
106
        # formatting the value.
 
107
        # Next we use '<' to indicate we want the value left aligned.
 
108
        # Then '10' is the width of the area.
 
109
        # For floats, finally, we only want only want at most 3 digits after
 
110
        # the decimal point to be displayed. This is the precision and it
 
111
        # can not be specified for integers which is why we need two separate
 
112
        # format strings.
 
113
        float_format = '{value:<10.3} {statistic}'.format
 
114
        int_format = '{value:<10} {statistic}'.format
 
115
        for statistic, value in benchmarks:
 
116
            if isinstance(value, int):
 
117
                benchmark = int_format(statistic=statistic, value=value)
 
118
            else:
 
119
                benchmark = float_format(statistic=statistic, value=value)
 
120
            self._write(benchmark)
 
121
 
 
122
    def show_source(self, error):
 
123
        """Show the physical line generating the error.
 
124
 
 
125
        This also adds an indicator for the particular part of the line that
 
126
        is reported as generating the problem.
 
127
 
 
128
        :param error:
 
129
            This will be an instance of :class:`~flake8.style_guide.Error`.
 
130
        :type error:
 
131
            flake8.style_guide.Error
 
132
        :returns:
 
133
            The formatted error string if the user wants to show the source.
 
134
            If the user does not want to show the source, this will return
 
135
            ``None``.
 
136
        :rtype:
 
137
            str
 
138
        """
 
139
        if not self.options.show_source or error.physical_line is None:
 
140
            return ''
 
141
 
 
142
        # Because column numbers are 1-indexed, we need to remove one to get
 
143
        # the proper number of space characters.
 
144
        pointer = (' ' * (error.column_number - 1)) + '^'
 
145
        # Physical lines have a newline at the end, no need to add an extra
 
146
        # one
 
147
        return error.physical_line + pointer
 
148
 
 
149
    def _write(self, output):
 
150
        """Handle logic of whether to use an output file or print()."""
 
151
        if self.output_fd is not None:
 
152
            self.output_fd.write(output + self.newline)
 
153
        else:
 
154
            print(output)
 
155
 
 
156
    def write(self, line, source):
 
157
        """Write the line either to the output file or stdout.
 
158
 
 
159
        This handles deciding whether to write to a file or print to standard
 
160
        out for subclasses. Override this if you want behaviour that differs
 
161
        from the default.
 
162
 
 
163
        :param str line:
 
164
            The formatted string to print or write.
 
165
        :param str source:
 
166
            The source code that has been formatted and associated with the
 
167
            line of output.
 
168
        """
 
169
        if line:
 
170
            self._write(line)
 
171
        if source:
 
172
            self._write(source)
 
173
 
 
174
    def stop(self):
 
175
        """Clean up after reporting is finished."""
 
176
        if self.output_fd is not None:
 
177
            self.output_fd.close()
 
178
            self.output_fd = None