You might find the autopep8 tool useful to easily take care of the rest. Here is a script that I use to integrate it with vim. It is used to automatically fix pep8 warnings in the current file being edited. It reads setup.cfg and tells autopep8 to ignore those classes of warnings.
Code:
#!/usr/bin/env python2
# vim:fileencoding=utf-8
from __future__ import (unicode_literals, division, absolute_import,
print_function)
__license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal'
import sys
import os
import subprocess
from ConfigParser import SafeConfigParser
arg = sys.argv[-1]
args = []
# Look for setup.cfg
parent = arg
while parent and os.path.dirname(parent) != parent:
parent = os.path.dirname(parent)
cfg = os.path.join(parent, 'setup.cfg')
if os.path.exists(cfg):
c = SafeConfigParser()
c.read(cfg)
if c.has_section('flake8'):
if c.has_option('flake8', 'select'):
args.append('--select=' + c.get('flake8', 'select'))
if c.has_option('flake8', 'ignore'):
args.append('--ignore=' + c.get('flake8', 'ignore'))
if c.has_option('flake8', 'max-line-length'):
args.append('--max-line-length=' +
c.get('flake8', 'max-line-length'))
break
cmd = ['autopep8'] + args
if '--as-filter' in sys.argv:
raw = sys.stdin.read()
p = subprocess.Popen(
cmd + ['-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdout = p.communicate(raw)[0]
rc = p.wait()
if rc != 0:
stdout = raw
sys.stdout.write(stdout)
raise SystemExit(rc)
cmd += [arg]
print (' '.join(cmd))
raise SystemExit(subprocess.Popen(cmd).wait())