Пример #1
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))