1
"""Statistic collection logic for Flake8."""
5
class Statistics(object):
6
"""Manager of aggregated statistics for a run of Flake8."""
9
"""Initialize the underlying dictionary for our statistics."""
12
def error_codes(self):
13
"""Return all unique error codes stored.
16
Sorted list of error codes.
20
return list(sorted(set(key.code for key in self._store.keys())))
22
def record(self, error):
23
"""Add the fact that the error was seen in the file.
26
The Error instance containing the information about the violation.
28
flake8.style_guide.Error
30
key = Key.create_from(error)
31
if key not in self._store:
32
self._store[key] = Statistic.create_from(error)
33
self._store[key].increment()
35
def statistics_for(self, prefix, filename=None):
36
"""Generate statistics for the prefix and filename.
38
If you have a :class:`Statistics` object that has recorded errors,
39
you can generate the statistics for a prefix (e.g., ``E``, ``E1``,
40
``W50``, ``W503``) with the optional filter of a filename as well.
42
.. code-block:: python
44
>>> stats = Statistics()
45
>>> stats.statistics_for('E12',
46
filename='src/flake8/statistics.py')
48
>>> stats.statistics_for('W')
52
The error class or specific error code to find statistics for.
54
(Optional) The filename to further filter results by.
56
Generator of instances of :class:`Statistic`
58
matching_errors = sorted(key for key in self._store.keys()
59
if key.matches(prefix, filename))
60
for error_code in matching_errors:
61
yield self._store[error_code]
64
class Key(collections.namedtuple('Key', ['filename', 'code'])):
65
"""Simple key structure for the Statistics dictionary.
67
To make things clearer, easier to read, and more understandable, we use a
68
namedtuple here for all Keys in the underlying dictionary for the
75
def create_from(cls, error):
76
"""Create a Key from :class:`flake8.style_guide.Error`."""
78
filename=error.filename,
82
def matches(self, prefix, filename):
83
"""Determine if this key matches some constraints.
86
The error code prefix that this key's error code should start with.
88
The filename that we potentially want to match on. This can be
89
None to only match on error prefix.
91
True if the Key's code starts with the prefix and either filename
92
is None, or the Key's filename matches the value passed in.
96
return (self.code.startswith(prefix) and
98
self.filename == filename))
101
class Statistic(object):
102
"""Simple wrapper around the logic of each statistic.
104
Instead of maintaining a simple but potentially hard to reason about
105
tuple, we create a namedtuple which has attributes and a couple
106
convenience methods on it.
109
def __init__(self, error_code, filename, message, count):
110
"""Initialize our Statistic."""
111
self.error_code = error_code
112
self.filename = filename
113
self.message = message
117
def create_from(cls, error):
118
"""Create a Statistic from a :class:`flake8.style_guide.Error`."""
120
error_code=error.code,
121
filename=error.filename,
127
"""Increment the number of times we've seen this error in this file."""