Esempio n. 1
0
    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, [])
Esempio n. 2
0
 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))