~ibmcharmers/charms/xenial/ibm-cinder-storwize-svc/trunk

« back to all changes in this revision

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

  • Committer: Ankammarao
  • Date: 2017-03-06 05:11:42 UTC
  • Revision ID: achittet@in.ibm.com-20170306051142-dpg27z4es1k56hfn
Marked tests folder executable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Plugin built-in to Flake8 to treat pyflakes as a plugin."""
 
2
# -*- coding: utf-8 -*-
 
3
from __future__ import absolute_import
 
4
 
 
5
try:
 
6
    # The 'demandimport' breaks pyflakes and flake8.plugins.pyflakes
 
7
    from mercurial import demandimport
 
8
except ImportError:
 
9
    pass
 
10
else:
 
11
    demandimport.disable()
 
12
import os
 
13
 
 
14
import pyflakes
 
15
import pyflakes.checker
 
16
 
 
17
from flake8 import utils
 
18
 
 
19
 
 
20
FLAKE8_PYFLAKES_CODES = dict([line.split()[::-1] for line in (
 
21
    'F401 UnusedImport',
 
22
    'F402 ImportShadowedByLoopVar',
 
23
    'F403 ImportStarUsed',
 
24
    'F404 LateFutureImport',
 
25
    'F405 ImportStarUsage',
 
26
    'F406 ImportStarNotPermitted',
 
27
    'F407 FutureFeatureNotDefined',
 
28
    'F601 MultiValueRepeatedKeyLiteral',
 
29
    'F602 MultiValueRepeatedKeyVariable',
 
30
    'F621 TooManyExpressionsInStarredAssignment',
 
31
    'F622 TwoStarredExpressions',
 
32
    'F631 AssertTuple',
 
33
    'F701 BreakOutsideLoop',
 
34
    'F702 ContinueOutsideLoop',
 
35
    'F703 ContinueInFinally',
 
36
    'F704 YieldOutsideFunction',
 
37
    'F705 ReturnWithArgsInsideGenerator',
 
38
    'F706 ReturnOutsideFunction',
 
39
    'F707 DefaultExceptNotLast',
 
40
    'F721 DoctestSyntaxError',
 
41
    'F811 RedefinedWhileUnused',
 
42
    'F812 RedefinedInListComp',
 
43
    'F821 UndefinedName',
 
44
    'F822 UndefinedExport',
 
45
    'F823 UndefinedLocal',
 
46
    'F831 DuplicateArgument',
 
47
    'F841 UnusedVariable',
 
48
)])
 
49
 
 
50
 
 
51
def patch_pyflakes():
 
52
    """Add error codes to Pyflakes messages."""
 
53
    for name, obj in vars(pyflakes.messages).items():
 
54
        if name[0].isupper() and obj.message:
 
55
            obj.flake8_msg = '%s %s' % (
 
56
                FLAKE8_PYFLAKES_CODES.get(name, 'F999'), obj.message
 
57
            )
 
58
 
 
59
 
 
60
patch_pyflakes()
 
61
 
 
62
 
 
63
class FlakesChecker(pyflakes.checker.Checker):
 
64
    """Subclass the Pyflakes checker to conform with the flake8 API."""
 
65
 
 
66
    name = 'pyflakes'
 
67
    version = pyflakes.__version__
 
68
    with_doctest = False
 
69
    include_in_doctest = []
 
70
    exclude_from_doctest = []
 
71
 
 
72
    def __init__(self, tree, filename):
 
73
        """Initialize the PyFlakes plugin with an AST tree and filename."""
 
74
        filename = utils.normalize_paths(filename)[0]
 
75
        with_doctest = self.with_doctest
 
76
        included_by = [include for include in self.include_in_doctest
 
77
                       if include != '' and filename.startswith(include)]
 
78
        if included_by:
 
79
            with_doctest = True
 
80
 
 
81
        for exclude in self.exclude_from_doctest:
 
82
            if exclude != '' and filename.startswith(exclude):
 
83
                with_doctest = False
 
84
                overlaped_by = [include for include in included_by
 
85
                                if include.startswith(exclude)]
 
86
 
 
87
                if overlaped_by:
 
88
                    with_doctest = True
 
89
 
 
90
        super(FlakesChecker, self).__init__(tree, filename,
 
91
                                            withDoctest=with_doctest)
 
92
 
 
93
    @classmethod
 
94
    def add_options(cls, parser):
 
95
        """Register options for PyFlakes on the Flake8 OptionManager."""
 
96
        parser.add_option(
 
97
            '--builtins', parse_from_config=True, comma_separated_list=True,
 
98
            help="define more built-ins, comma separated",
 
99
        )
 
100
        parser.add_option(
 
101
            '--doctests', default=False, action='store_true',
 
102
            parse_from_config=True,
 
103
            help="check syntax of the doctests",
 
104
        )
 
105
        parser.add_option(
 
106
            '--include-in-doctest', default='',
 
107
            dest='include_in_doctest', parse_from_config=True,
 
108
            comma_separated_list=True, normalize_paths=True,
 
109
            help='Run doctests only on these files',
 
110
            type='string',
 
111
        )
 
112
        parser.add_option(
 
113
            '--exclude-from-doctest', default='',
 
114
            dest='exclude_from_doctest', parse_from_config=True,
 
115
            comma_separated_list=True, normalize_paths=True,
 
116
            help='Skip these files when running doctests',
 
117
            type='string',
 
118
        )
 
119
 
 
120
    @classmethod
 
121
    def parse_options(cls, options):
 
122
        """Parse option values from Flake8's OptionManager."""
 
123
        if options.builtins:
 
124
            cls.builtIns = cls.builtIns.union(options.builtins)
 
125
        cls.with_doctest = options.doctests
 
126
 
 
127
        included_files = []
 
128
        for included_file in options.include_in_doctest:
 
129
            if included_file == '':
 
130
                continue
 
131
            if not included_file.startswith((os.sep, './', '~/')):
 
132
                included_files.append('./' + included_file)
 
133
            else:
 
134
                included_files.append(included_file)
 
135
        cls.include_in_doctest = utils.normalize_paths(included_files)
 
136
 
 
137
        excluded_files = []
 
138
        for excluded_file in options.exclude_from_doctest:
 
139
            if excluded_file == '':
 
140
                continue
 
141
            if not excluded_file.startswith((os.sep, './', '~/')):
 
142
                excluded_files.append('./' + excluded_file)
 
143
            else:
 
144
                excluded_files.append(excluded_file)
 
145
        cls.exclude_from_doctest = utils.normalize_paths(excluded_files)
 
146
 
 
147
        inc_exc = set(cls.include_in_doctest).intersection(
 
148
            cls.exclude_from_doctest
 
149
        )
 
150
        if inc_exc:
 
151
            raise ValueError('"%s" was specified in both the '
 
152
                             'include-in-doctest and exclude-from-doctest '
 
153
                             'options. You are not allowed to specify it in '
 
154
                             'both for doctesting.' % inc_exc)
 
155
 
 
156
    def run(self):
 
157
        """Run the plugin."""
 
158
        for message in self.messages:
 
159
            col = getattr(message, 'col', 0)
 
160
            yield (message.lineno,
 
161
                   col,
 
162
                   (message.flake8_msg % message.message_args),
 
163
                   message.__class__)