|
| 1 | +from fnmatch import fnmatch |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +from nose.tools import assert_equal |
| 6 | +import pep8 |
| 7 | + |
| 8 | +import matplotlib |
| 9 | + |
| 10 | + |
| 11 | +class StandardReportWithExclusions(pep8.StandardReport): |
| 12 | + #; A class attribute to store the exception exclusion file patterns. |
| 13 | + expected_bad_files = [ |
| 14 | + '*/matplotlib/__init__.py', |
| 15 | + '*/matplotlib/_cm.py', |
| 16 | + '*/matplotlib/_mathtext_data.py', |
| 17 | + '*/matplotlib/_pylab_helpers.py', |
| 18 | + '*/matplotlib/afm.py', |
| 19 | + '*/matplotlib/animation.py', |
| 20 | + '*/matplotlib/artist.py', |
| 21 | + '*/matplotlib/axes.py', |
| 22 | + '*/matplotlib/axis.py', |
| 23 | + '*/matplotlib/backend_bases.py', |
| 24 | + '*/matplotlib/bezier.py', |
| 25 | + '*/matplotlib/cbook.py', |
| 26 | + '*/matplotlib/collections.py', |
| 27 | + '*/matplotlib/docstring.py', |
| 28 | + '*/matplotlib/dviread.py', |
| 29 | + '*/matplotlib/finance.py', |
| 30 | + '*/matplotlib/font_manager.py', |
| 31 | + '*/matplotlib/fontconfig_pattern.py', |
| 32 | + '*/matplotlib/gridspec.py', |
| 33 | + '*/matplotlib/legend.py', |
| 34 | + '*/matplotlib/legend_handler.py', |
| 35 | + '*/matplotlib/mathtext.py', |
| 36 | + '*/matplotlib/mlab.py', |
| 37 | + '*/matplotlib/path.py', |
| 38 | + '*/matplotlib/patheffects.py', |
| 39 | + '*/matplotlib/pylab.py', |
| 40 | + '*/matplotlib/pyplot.py', |
| 41 | + '*/matplotlib/rcsetup.py', |
| 42 | + '*/matplotlib/stackplot.py', |
| 43 | + '*/matplotlib/texmanager.py', |
| 44 | + '*/matplotlib/text.py', |
| 45 | + '*/matplotlib/transforms.py', |
| 46 | + '*/matplotlib/type1font.py', |
| 47 | + '*/matplotlib/widgets.py', |
| 48 | + '*/matplotlib/testing/compare.py', |
| 49 | + '*/matplotlib/testing/decorators.py', |
| 50 | + '*/matplotlib/testing/image_util.py', |
| 51 | + '*/matplotlib/testing/noseclasses.py', |
| 52 | + '*/matplotlib/testing/jpl_units/Duration.py', |
| 53 | + '*/matplotlib/testing/jpl_units/Epoch.py', |
| 54 | + '*/matplotlib/testing/jpl_units/EpochConverter.py', |
| 55 | + '*/matplotlib/testing/jpl_units/StrConverter.py', |
| 56 | + '*/matplotlib/testing/jpl_units/UnitDbl.py', |
| 57 | + '*/matplotlib/testing/jpl_units/UnitDblConverter.py', |
| 58 | + '*/matplotlib/testing/jpl_units/UnitDblFormatter.py', |
| 59 | + '*/matplotlib/testing/jpl_units/__init__.py', |
| 60 | + '*/matplotlib/tri/tricontour.py', |
| 61 | + '*/matplotlib/tri/triinterpolate.py', |
| 62 | + '*/matplotlib/tri/tripcolor.py', |
| 63 | + '*/matplotlib/tri/triplot.py', |
| 64 | + '*/matplotlib/tests/__init__.py', |
| 65 | + '*/matplotlib/tests/test_agg.py', |
| 66 | + '*/matplotlib/tests/test_animation.py', |
| 67 | + '*/matplotlib/tests/test_arrow_patches.py', |
| 68 | + '*/matplotlib/tests/test_artist.py', |
| 69 | + '*/matplotlib/tests/test_axes.py', |
| 70 | + '*/matplotlib/tests/test_backend_pdf.py', |
| 71 | + '*/matplotlib/tests/test_backend_pgf.py', |
| 72 | + '*/matplotlib/tests/test_backend_svg.py', |
| 73 | + '*/matplotlib/tests/test_basic.py', |
| 74 | + '*/matplotlib/tests/test_bbox_tight.py', |
| 75 | + '*/matplotlib/tests/test_cbook.py', |
| 76 | + '*/matplotlib/tests/test_colorbar.py', |
| 77 | + '*/matplotlib/tests/test_colors.py', |
| 78 | + '*/matplotlib/tests/test_compare_images.py', |
| 79 | + '*/matplotlib/tests/test_contour.py', |
| 80 | + '*/matplotlib/tests/test_dates.py', |
| 81 | + '*/matplotlib/tests/test_delaunay.py', |
| 82 | + '*/matplotlib/tests/test_dviread.py', |
| 83 | + '*/matplotlib/tests/test_image.py', |
| 84 | + '*/matplotlib/tests/test_legend.py', |
| 85 | + '*/matplotlib/tests/test_lines.py', |
| 86 | + '*/matplotlib/tests/test_mathtext.py', |
| 87 | + '*/matplotlib/tests/test_patches.py', |
| 88 | + '*/matplotlib/tests/test_pickle.py', |
| 89 | + '*/matplotlib/tests/test_png.py', |
| 90 | + '*/matplotlib/tests/test_rcparams.py', |
| 91 | + '*/matplotlib/tests/test_simplification.py', |
| 92 | + '*/matplotlib/tests/test_spines.py', |
| 93 | + '*/matplotlib/tests/test_streamplot.py', |
| 94 | + '*/matplotlib/tests/test_subplots.py', |
| 95 | + '*/matplotlib/tests/test_text.py', |
| 96 | + '*/matplotlib/tests/test_tightlayout.py', |
| 97 | + '*/matplotlib/tests/test_transforms.py', |
| 98 | + '*/matplotlib/tests/test_triangulation.py', |
| 99 | + '*/matplotlib/compat/subprocess.py', |
| 100 | + '*/matplotlib/backends/__init__.py', |
| 101 | + '*/matplotlib/backends/backend_agg.py', |
| 102 | + '*/matplotlib/backends/backend_cairo.py', |
| 103 | + '*/matplotlib/backends/backend_cocoaagg.py', |
| 104 | + '*/matplotlib/backends/backend_gdk.py', |
| 105 | + '*/matplotlib/backends/backend_gtk.py', |
| 106 | + '*/matplotlib/backends/backend_gtk3.py', |
| 107 | + '*/matplotlib/backends/backend_gtk3cairo.py', |
| 108 | + '*/matplotlib/backends/backend_gtkagg.py', |
| 109 | + '*/matplotlib/backends/backend_gtkcairo.py', |
| 110 | + '*/matplotlib/backends/backend_macosx.py', |
| 111 | + '*/matplotlib/backends/backend_mixed.py', |
| 112 | + '*/matplotlib/backends/backend_pdf.py', |
| 113 | + '*/matplotlib/backends/backend_pgf.py', |
| 114 | + '*/matplotlib/backends/backend_ps.py', |
| 115 | + '*/matplotlib/backends/backend_qt4.py', |
| 116 | + '*/matplotlib/backends/backend_qt4agg.py', |
| 117 | + '*/matplotlib/backends/backend_svg.py', |
| 118 | + '*/matplotlib/backends/backend_template.py', |
| 119 | + '*/matplotlib/backends/backend_tkagg.py', |
| 120 | + '*/matplotlib/backends/backend_webagg.py', |
| 121 | + '*/matplotlib/backends/backend_wx.py', |
| 122 | + '*/matplotlib/backends/backend_wxagg.py', |
| 123 | + '*/matplotlib/backends/qt4_compat.py', |
| 124 | + '*/matplotlib/backends/tkagg.py', |
| 125 | + '*/matplotlib/backends/windowing.py', |
| 126 | + '*/matplotlib/backends/qt4_editor/figureoptions.py', |
| 127 | + '*/matplotlib/backends/qt4_editor/formlayout.py', |
| 128 | + '*/matplotlib/sphinxext/__init__.py', |
| 129 | + '*/matplotlib/sphinxext/ipython_console_highlighting.py', |
| 130 | + '*/matplotlib/sphinxext/ipython_directive.py', |
| 131 | + '*/matplotlib/sphinxext/mathmpl.py', |
| 132 | + '*/matplotlib/sphinxext/only_directives.py', |
| 133 | + '*/matplotlib/sphinxext/plot_directive.py', |
| 134 | + '*/matplotlib/projections/__init__.py', |
| 135 | + '*/matplotlib/projections/geo.py', |
| 136 | + '*/matplotlib/projections/polar.py'] |
| 137 | + |
| 138 | + #: A class attribute to store patterns which have seen exceptions. |
| 139 | + matched_exclusions = set() |
| 140 | + |
| 141 | + def get_file_results(self): |
| 142 | + # If the file had no errors, return self.file_errors (which will be 0) |
| 143 | + if not self._deferred_print: |
| 144 | + return self.file_errors |
| 145 | + |
| 146 | + # Iterate over all of the patterns, to find a possible exclusion. If we |
| 147 | + # the filename is to be excluded, go ahead and remove the counts that |
| 148 | + # self.error added. |
| 149 | + for pattern in self.expected_bad_files: |
| 150 | + if fnmatch(self.filename, pattern): |
| 151 | + self.matched_exclusions.add(pattern) |
| 152 | + # invert the error method's counters. |
| 153 | + for _, _, code, _, _ in self._deferred_print: |
| 154 | + self.counters[code] -= 1 |
| 155 | + if self.counters[code] == 0: |
| 156 | + self.counters.pop(code) |
| 157 | + self.messages.pop(code) |
| 158 | + self.file_errors -= 1 |
| 159 | + self.total_errors -= 1 |
| 160 | + return self.file_errors |
| 161 | + |
| 162 | + # Otherwise call the superclass' method to print the bad results. |
| 163 | + return super(StandardReportWithExclusions, |
| 164 | + self).get_file_results() |
| 165 | + |
| 166 | + |
| 167 | +def test_pep8_conformance(): |
| 168 | +# Tests the matplotlib codebase against the "pep8" tool. |
| 169 | +# |
| 170 | +# Users can add their own excluded files (should files exist in the |
| 171 | +# local directory which is not in the repository) by adding a |
| 172 | +# ".pep8_test_exclude.txt" file in the same directory as this test. |
| 173 | +# The file should be a line separated list of filenames/directories |
| 174 | +# as can be passed to the "pep8" tool's exclude list. |
| 175 | + |
| 176 | + # Only run this test with Python 2 - the 2to3 tool generates non pep8 |
| 177 | + # compliant code. |
| 178 | + if sys.version_info[0] != 2: |
| 179 | + return |
| 180 | + |
| 181 | + # to get a list of bad files, rather than the specific errors, add |
| 182 | + # "reporter=pep8.FileReport" to the StyleGuide constructor. |
| 183 | + pep8style = pep8.StyleGuide(quiet=False, |
| 184 | + reporter=StandardReportWithExclusions) |
| 185 | + |
| 186 | + # Extend the number of PEP8 guidelines which are not checked. |
| 187 | + pep8style.options.ignore = pep8style.options.ignore + ('E121', 'E122', |
| 188 | + 'E123', 'E124', 'E125', 'E126', 'E127', |
| 189 | + 'E128') |
| 190 | + |
| 191 | + # Support for egg shared object wrappers, which are not PEP8 compliant, |
| 192 | + # nor part of the matplotlib repository. |
| 193 | + # DO NOT ADD FILES *IN* THE REPOSITORY TO THIS LIST. |
| 194 | + pep8style.options.exclude.extend( |
| 195 | + ['_delaunay.py', |
| 196 | + '_image.py', |
| 197 | + '_tri.py', |
| 198 | + '_backend_agg.py', |
| 199 | + '_tkagg.py', |
| 200 | + 'ft2font.py', |
| 201 | + '_cntr.py', |
| 202 | + '_png.py', |
| 203 | + '_path.py', |
| 204 | + 'ttconv.py', |
| 205 | + 'pyparsing*']) |
| 206 | + |
| 207 | + # Allow users to add their own exclude list. |
| 208 | + extra_exclude_file = os.path.join(os.path.dirname(__file__), |
| 209 | + '.pep8_test_exclude.txt') |
| 210 | + if os.path.exists(extra_exclude_file): |
| 211 | + with open(extra_exclude_file, 'r') as fh: |
| 212 | + extra_exclude = [line.strip() for line in fh if line.strip()] |
| 213 | + pep8style.options.exclude.extend(extra_exclude) |
| 214 | + |
| 215 | + result = pep8style.check_files([os.path.dirname(matplotlib.__file__)]) |
| 216 | + assert_equal(result.total_errors, 0, "Found code syntax " |
| 217 | + "errors (and warnings).") |
| 218 | + |
| 219 | + reporter = pep8style.options.reporter |
| 220 | + # If we've been using the exclusions reporter, check that we didn't |
| 221 | + # exclude files unnecessarily. |
| 222 | + if reporter is StandardReportWithExclusions: |
| 223 | + unexpectedly_good = sorted(set(reporter.expected_bad_files) - |
| 224 | + reporter.matched_exclusions) |
| 225 | + |
| 226 | + if unexpectedly_good: |
| 227 | + raise ValueError('Some exclude patterns were unnecessary as the ' |
| 228 | + 'files they pointed to either passed the PEP8 ' |
| 229 | + 'tests or do not point to a file:\n ' |
| 230 | + '{}'.format('\n '.join(unexpectedly_good))) |
| 231 | + |
| 232 | + |
| 233 | +if __name__ == '__main__': |
| 234 | + import nose |
| 235 | + nose.runmodule(argv=['-s', '--with-doctest'], exit=False) |
0 commit comments