本文を読み飛ばす

ZopeのPython Scriptとsyntastic

ZopeのPython Scriptをローカルに mirrorした状態でvimからいじることがあるのですが, 最近syntasticを導入したらwarningが出て困ります.

Stackoverflowの このエントリ を参考に なんとかエラーがでないようにしてみました.

1. flake8をPython Scriptに対応させる...

これは無理です.

というのもstackoverflowに書いてます.

そこでflake8に渡す前にスクリプトをいじってしまいます...

こんな感じ:

#! /usr/bin/python
import sys
import re
from subprocess import Popen, PIPE, call
from tempfile import SpooledTemporaryFile as tempfile


def main():
  fname = sys.argv[1]
  f = tempfile()
  f1st = True
  for line in file(fname):
      if f1st and 'Script (Python)' not in line:
          ret = call(['flake8', fname])
          return ret
      f1st = False

      if not line.strip():
          f.write('\n')
          continue

      if re.search(r"^ \*#", line):
          pass
      else:
          line = re.sub(r"(^[^#])", r"    \1", line)
          #line = re.sub(r"^##bind [a-z]*=([a-z][a-z]*)$", r"import \1", line)
          line = re.sub(r"^##parameters=(.*)", r"def foo(printed, \1):", line)
          f.write(line)
          f.seek(0)
          pipe = Popen(["flake8", '--ignore=E3', '/dev/stdin'], stdin=f, stdout=PIPE)
          stdout, stderr = pipe.communicate(input=f)

  for line in stdout.splitlines():
      line = re.sub(r"^/dev/stdin", fname, line)
      sys.stdout.write(line + "\n")

main()
# vi: ft=python

実行権限つけてどこかに保存しておきます.

私の場合は/opt/binあたりに放り込んでおきました.

名前は flake8custom.py です.

2. Syntasticでflake8の代わりに作ったスクリプトを起動

syntasticのsyntax_checkersフォルダにある

python/flake8.vim のflake8の部分をflake8customに修正します.:

let makeprg = 'flake8'.g:syntastic_python_checker_args.' '.shellescape(expand('%'))

ここを:

let makeprg = 'flake8custom.py '.g:syntastic_python_checker_args.' '.shellescape(expand('%'))

こうです.

おわり

subprocessのところ, いっつも覚えてなくて 調べながらやるんで時間かかっちゃいました...

コメント

Comments powered by Disqus
宣伝: