def test_flake8(self): self.requireFeature(features.flake8) # Older versions of flake8 don't support the 'paths' # variable new_path = list(sys.path) new_path.insert( 0, os.path.join(os.path.dirname(__file__), '..', '..', 'tools')) self.overrideAttr(sys, 'path', new_path) from flake8.main.application import Application from flake8.formatting.base import BaseFormatter app = Application() app.config = u'setup.cfg' app.jobs = 1 class Formatter(BaseFormatter): def __init__(self): self.errors = [] def start(self): pass def stop(self): app.file_checker_manager.report() def handle(self, error): self.errors.append(error) app.formatter = Formatter() app.initialize([]) app.run_checks() app.report() self.assertEqual(app.formatter.errors, [])
def find_options(self, config): """Use flake8's library to find a valid config for flake8""" flk8 = Application() args = [] if config: args.append("--config={}".format(config)) flk8.initialize(args) opts = {} for key, value in list(vars(flk8.options).items()): if re.match(r'pytest_mark.*', key): for option in value: try: opts[key] except KeyError: opts[key] = {} val = option.split('=') opts[key][val[0]] = val[1] self.options = opts
def run(self): """ 代码检测工作函数 """ while 1: if not self._running: logger.info('code checker quit') break if not self.background_checking: QThread.msleep(500) continue if self._queue.qsize() == 0: QThread.msleep(500) continue try: widget, code = self._queue.get(False, 0.5) # 创建临时文件 file = QTemporaryFile(self) file.setAutoRemove(True) if file.open(): with open(file.fileName(), 'wb') as fp: fp.write(code.encode()) file.close() # 使用flake8检测代码 results = [] with StringIO() as out, redirect_stdout(out): app = Application() app.initialize( ['flake8', '--exit-zero', '--config', os.path.join(os.path.dirname(__file__), 'config', '.flake8')]) app.run_checks([file.fileName()]) app.report() results = out.getvalue().split('\n') results = [ret for ret in results if re.search(r'\d+:\d+:[EFW]\d+:.*?', ret)] # if results: self.checked.emit(widget, results) # 如果为空,也应该这样做。将空列表传入可以清空所有的标记。 file.deleteLater() del file except Exception as e: logger.warning(str(e))
def make_linter(code, path='example.py', argv=None): app = Application() app.initialize(argv) Checker.parse_options(app.options) tree = ast.parse(code, path) return Checker(tree, path)
def make_linter(code, path='example.py', argv=None): app = Application() app.initialize(argv) Checker.parse_options(app.options) tree = ast.parse(code, path) return Checker(tree, path)