Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PyFlakes Script for BBEdit 10
#!/usr/bin/env python2.6
"""
A quick script to install into your `Application Support/BBEdit/Scripts` folder.
This runs PyFlakes (requires PyFlakes to be installed at `/usr/local/bin` -
try ``/usr/bin/easy_install-2.6 pyflakes==0.4.0``) 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-V. You must save your Python file first before
running the check.
"""
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/pyflakes"
stdout, stderr = Popen([pyflakes, doc_file], 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+):\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)
print(groups['message'], file=sys.stderr)
print('^', file=sys.stderr)
sys.exit(1)
@jshell

This comment has been minimized.

Copy link
Owner Author

jshell commented Jul 10, 2012

This should also work with the 'flake8' script. Just change the path to 'pyflakes' to point to a 'flake8' script to get PEP 8 and PyFlakes checking in one.

@mwchase

This comment has been minimized.

Copy link

mwchase commented Dec 6, 2013

Did you intend to license this in any way? I combined some of this code with another, similar gist (neither does quite what I want out of the box), and made some modifications on top of that, and I can't see any way to legally distribute the result, as it stands.

@WallGeckoPoint

This comment has been minimized.

Copy link

WallGeckoPoint commented Sep 15, 2018

Updated for python3 / BBedit 12:
https://gist.github.com/WallGeckoPoint/f4d20d91400c35b230b38e7d3df333d2

I couldn't figure out what the caret line

print('^', file=sys.stderr)

was supposed to do, so I removed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.