Sublime Forum

PEP 8 style guide checker

#1

This plugin will check your Python code for errors and warnings per PEP 8.

You must install pep.py for this to work, and it must be in your system’s path.

Then copy the below code and save it as pep8check.py and put it in ST2’s Packages/User directory.

pep8check.py:

[code]import os
import sublime
import sublime_plugin

class Pep8CheckCommand(sublime_plugin.TextCommand):

"""This will invoke PEP8 checking on the given file.
pep8.py must be in your system's path for this to work.

http://pypi.python.org/pypi/pep8

Options:
--version            show program's version number and exit
--help               show this help message and exit
--verbose            print status messages, or debug with -vv
--quiet              report only file names, or nothing with -qq
--repeat             show all occurrences of the same error
--exclude=patterns   exclude files or directories which match these comma
                     separated patterns (default: .svn,CVS,.bzr,.hg,.git)
--filename=patterns  when parsing directories, only check filenames
                   matching these comma separated patterns (default: *.py)
--select=errors      select errors and warnings (e.g. E,W6)
--ignore=errors      skip errors and warnings (e.g. E4,W)
--show-source        show source code for each error
--show-pep8          show text of PEP 8 for each error
--statistics         count errors and warnings
--count              print total number of errors and warnings to standard
                   error and set exit code to 1 if total is not null
--benchmark          measure processing speed
--testsuite=dir      run regression tests from dir
--doctest            run doctest on myself
"""

def run(self, edit):
    if self.view.file_name().endswith('.py'):
        folder_name, file_name = os.path.split(self.view.file_name())
        self.view.window().run_command('exec', {'cmd': 'pep8',         \
            '--repeat', '--verbose', '--ignore=E501', '--show-source',  \
            '--statistics', '--count',                                  \
             file_name], 'working_dir': folder_name})
        sublime.status_message("pep8 " + file_name)

def is_enabled(self):
    return self.view.file_name().endswith('.py')

[/code]

I’ve added this to my User Key Bindings (Command-Shift-8 on a Mac):

{ "keys": "super+shift+8"], "command": "pep8_check" }

…and this in Default/Context.sublime-menu, which allows context menu access:

{ "command": "pep8_check", "caption": "PEP8" }
0 Likes

#2

What a coincidence, I wrote pep-8/pyflakes checker a few days ago - https://github.com/vorushin/sublimetext_python_checker

0 Likes