~ibmcharmers/charms/trusty/ibm-dsm-base/trunk

« back to all changes in this revision

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

  • Committer: anita nayak
  • Date: 2016-12-08 14:39:17 UTC
  • Revision ID: anitanayak@in.ibm.com-20161208143917-3pcyiz4cmbey1fol
Initial Check in for ibm-dsm-base trusty

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Statistic collection logic for Flake8."""
 
2
import collections
 
3
 
 
4
 
 
5
class Statistics(object):
 
6
    """Manager of aggregated statistics for a run of Flake8."""
 
7
 
 
8
    def __init__(self):
 
9
        """Initialize the underlying dictionary for our statistics."""
 
10
        self._store = {}
 
11
 
 
12
    def error_codes(self):
 
13
        """Return all unique error codes stored.
 
14
 
 
15
        :returns:
 
16
            Sorted list of error codes.
 
17
        :rtype:
 
18
            list(str)
 
19
        """
 
20
        return list(sorted(set(key.code for key in self._store.keys())))
 
21
 
 
22
    def record(self, error):
 
23
        """Add the fact that the error was seen in the file.
 
24
 
 
25
        :param error:
 
26
            The Error instance containing the information about the violation.
 
27
        :type error:
 
28
            flake8.style_guide.Error
 
29
        """
 
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()
 
34
 
 
35
    def statistics_for(self, prefix, filename=None):
 
36
        """Generate statistics for the prefix and filename.
 
37
 
 
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.
 
41
 
 
42
        .. code-block:: python
 
43
 
 
44
            >>> stats = Statistics()
 
45
            >>> stats.statistics_for('E12',
 
46
                                     filename='src/flake8/statistics.py')
 
47
            <generator ...>
 
48
            >>> stats.statistics_for('W')
 
49
            <generator ...>
 
50
 
 
51
        :param str prefix:
 
52
            The error class or specific error code to find statistics for.
 
53
        :param str filename:
 
54
            (Optional) The filename to further filter results by.
 
55
        :returns:
 
56
            Generator of instances of :class:`Statistic`
 
57
        """
 
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]
 
62
 
 
63
 
 
64
class Key(collections.namedtuple('Key', ['filename', 'code'])):
 
65
    """Simple key structure for the Statistics dictionary.
 
66
 
 
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
 
69
    Statistics object.
 
70
    """
 
71
 
 
72
    __slots__ = ()
 
73
 
 
74
    @classmethod
 
75
    def create_from(cls, error):
 
76
        """Create a Key from :class:`flake8.style_guide.Error`."""
 
77
        return cls(
 
78
            filename=error.filename,
 
79
            code=error.code,
 
80
        )
 
81
 
 
82
    def matches(self, prefix, filename):
 
83
        """Determine if this key matches some constraints.
 
84
 
 
85
        :param str prefix:
 
86
            The error code prefix that this key's error code should start with.
 
87
        :param str filename:
 
88
            The filename that we potentially want to match on. This can be
 
89
            None to only match on error prefix.
 
90
        :returns:
 
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.
 
93
        :rtype:
 
94
            bool
 
95
        """
 
96
        return (self.code.startswith(prefix) and
 
97
                (filename is None or
 
98
                    self.filename == filename))
 
99
 
 
100
 
 
101
class Statistic(object):
 
102
    """Simple wrapper around the logic of each statistic.
 
103
 
 
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.
 
107
    """
 
108
 
 
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
 
114
        self.count = count
 
115
 
 
116
    @classmethod
 
117
    def create_from(cls, error):
 
118
        """Create a Statistic from a :class:`flake8.style_guide.Error`."""
 
119
        return cls(
 
120
            error_code=error.code,
 
121
            filename=error.filename,
 
122
            message=error.text,
 
123
            count=0,
 
124
        )
 
125
 
 
126
    def increment(self):
 
127
        """Increment the number of times we've seen this error in this file."""
 
128
        self.count += 1