Skip to content

Instantly share code, notes, and snippets.

@coryalder
Forked from jshell/BBFlakes.py
Created August 10, 2012 20:46
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save coryalder/3317713 to your computer and use it in GitHub Desktop.
Save coryalder/3317713 to your computer and use it in GitHub Desktop.
Flake8 Script for BBEdit 10
#!/usr/bin/env python
"""
A quick script to install into your `Application Support/BBEdit/Scripts` folder.
This runs flake8 (requires flake8 to be installed at `/usr/local/bin` -
try ``pip install flake8``) and reformats the results
so that they show up in BBEdit's search results / error / warnings window. Then
the errors can be stepped through one at a time.
I've bound this to control-shift-l. You must save your Python file first before
running the check.
original script: https://gist.github.com/1157742
Ported to work with:
- flake8 --version: 1.4 (pyflakes: 0.5.0, pep8: 1.2)
- python --version: Python 2.7.2
BBEdit 10.1.2 (MAS)
Here: https://gist.github.com/3317713
"""
from __future__ import print_function
import os
import re
import sys
from subprocess import Popen, PIPE
if os.environ['BB_DOC_LANGUAGE'].lower() != 'python':
# Bail out quietly if language isn't Python
sys.exit(0)
doc_file = os.environ['BB_DOC_PATH']
pyflakes = "/usr/local/bin/flake8"
line_length = 256 # I don't like PEP8 warning be about long lines.
stdout, stderr = Popen([pyflakes, doc_file, "--max-line-length=" + str(line_length)], stdout=PIPE, stderr=PIPE).communicate()
output = stdout if stdout else stderr
if not output:
sys.exit(0)
line_format = re.compile('(?P<path>[^:]+):(?P<line>\d+):(?P<character>\d+:)?\s(?P<message>.*$)')
for line in output.splitlines():
m = line_format.match(line)
if not m:
continue
groups = m.groupdict()
print(''' File "{path}", line {line}'''.format(**groups), file=sys.stderr)
outstr = ""
if groups['character']:
outstr = "At column " + groups['character'] + " "
print(outstr + groups['message'], file=sys.stderr)
print('^', file=sys.stderr)
sys.exit(1)
@madamimadam
Copy link

This seems to do absolutely nothing when I run it (from the scripts menu). I even shoved in some deliberate errors, still no dice. I have the search results window already open and nothing gets printed there. Any idea what I'm doing wrong?

@madamimadam
Copy link

Never mind. You have the damn caret in the wrong place.

@madamimadam
Copy link

For those who come after, if there are any: the caret goes between the file and line location info and the error info. In this case, move the print statement that prints the caret up to just above the 'outstr = ""' line. I'd

@splaisan
Copy link

splaisan commented Sep 8, 2021

I do not get error reports when I introduce errors in my python code and save it (AND the errors are found using flake8 at CLI)

python 3.7
pyflakes 2.3.1
version 14.0.1 (416084, 64-bit Intel, sandboxed)

'Part of' the problem seems to be that the BBedit error output window does not open after running the script.
If I have an error window still open from another BBEdit process, lines are added to it after running the script and all is fine!

Any idea how I can force the error window to open in the code (I thought that the last line was doing this but maybe we never reach that line!) - maybe because I run a pre-release of BBedit?

Thanks in advance

@splaisan
Copy link

splaisan commented Sep 8, 2021

also, I do not see an effect of printing the caret and there is no apparent way to do this: <Then
the errors can be stepped through one at a time.>
I mean that the output is plain text with no link to the code page as in a BBedit search result or file compare

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment