From 98890d4748108496920b6ccca1bce70728e76e32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=AF=E5=B1=95=E6=84=8F?= <1295752786@qq.com> Date: Thu, 24 Sep 2020 23:47:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=9C=A8=E7=BB=88?= =?UTF-8?q?=E7=AB=AF=E4=B8=AD=E8=BF=90=E8=A1=8C=E4=BB=A3=E7=A0=81=E7=9A=84?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将平台有关操作移动到features/util包中 添加测试用例 --- .../extensions/extensionlib/extension_lib.py | 22 +++++++++++-------- .../packages/applications_toolbar/main.py | 1 + .../packages/code_editor/codeeditor/editor.py | 20 +++++++++++++++-- .../code_editor/codeeditor/tabwidget.py | 16 +++++++++++++- .../packages/code_editor/toolbar.py | 4 ++++ pyminer2/features/util/__init__.py | 0 .../common => features/util}/openprocess.py | 0 .../common => features/util}/platformutil.py | 8 ++++++- pyminer2/tests/test_features/__init__.py | 0 .../tests/test_features/python_file_to_run.py | 2 ++ pyminer2/tests/test_features/test_util.py | 22 +++++++++++++++++++ pyminer2/ui/common/open_process_with_pyqt.py | 6 ++--- 12 files changed, 85 insertions(+), 16 deletions(-) create mode 100644 pyminer2/features/util/__init__.py rename pyminer2/{ui/common => features/util}/openprocess.py (100%) rename pyminer2/{ui/common => features/util}/platformutil.py (80%) create mode 100644 pyminer2/tests/test_features/__init__.py create mode 100644 pyminer2/tests/test_features/python_file_to_run.py create mode 100644 pyminer2/tests/test_features/test_util.py diff --git a/pyminer2/extensions/extensionlib/extension_lib.py b/pyminer2/extensions/extensionlib/extension_lib.py index 8885ec1e..86b54aa2 100644 --- a/pyminer2/extensions/extensionlib/extension_lib.py +++ b/pyminer2/extensions/extensionlib/extension_lib.py @@ -2,12 +2,10 @@ import os from typing import TYPE_CHECKING, Callable, Dict -from PyQt5.QtCore import QRect, pyqtSignal +from PyQt5.QtCore import QRect from PyQt5.QtWidgets import QWidget if TYPE_CHECKING: - from pyminer2.extensions.packages.ipython_console.main import ConsoleWidget, ConsoleInterface - from pyminer2.workspace.datamanager.datamanager import DataManager from pmgwidgets import PMGToolBar # from pyminer2.extensions.packages.ipython_console.main import ConsoleWidget @@ -46,12 +44,13 @@ def wrapper(): class UI(): @staticmethod - def get_toolbar(toolbar_name:str)->'PMGToolBar': - tb= get_main_window().toolbars.get(toolbar_name) + def get_toolbar(toolbar_name: str) -> 'PMGToolBar': + tb = get_main_window().toolbars.get(toolbar_name) return tb + @staticmethod - def get_toolbar_widget(toolbar_name:str,widget_name:str)->'QWidget': + def get_toolbar_widget(toolbar_name: str, widget_name: str) -> 'QWidget': toolbar = ExtensionLib.UI.get_toolbar(toolbar_name) if toolbar is not None: widget = toolbar.get_control_widget(widget_name) @@ -80,9 +79,9 @@ def wrapper(): return os.path.join(app.font_dir, app.default_font) @staticmethod - def switch_toolbar(toolbar_name:str, switch_only:bool=True): - app=get_main_window() - app.switch_toolbar(toolbar_name,switch_only) + def switch_toolbar(toolbar_name: str, switch_only: bool = True): + app = get_main_window() + app.switch_toolbar(toolbar_name, switch_only) class Signal(): @staticmethod @@ -133,6 +132,11 @@ def wrapper(): ''' return get_main_window().settings['work_dir'] + @staticmethod + def run_python_file(file_path: str): + from pyminer2.features.util.platformutil import run_python_file_in_terminal + run_python_file_in_terminal(file_path) + ######################## # 以下属性与数据管理类相关。 def get_all_var(self) -> dict: diff --git a/pyminer2/extensions/packages/applications_toolbar/main.py b/pyminer2/extensions/packages/applications_toolbar/main.py index 72423453..d1d59737 100644 --- a/pyminer2/extensions/packages/applications_toolbar/main.py +++ b/pyminer2/extensions/packages/applications_toolbar/main.py @@ -55,6 +55,7 @@ class ApplicationsInterface(BaseInterface): def on_clicked(self, name: str): print('interface', name) + def add_app(self, group: str, text: str, icon_path: str, callback: Callable, hint: str = ''): ''' 添加一个绘图按钮。name表示按钮的名称,text表示按钮的文字,icon_path表示按钮的图标路径,callback表示按钮的回调函数 diff --git a/pyminer2/extensions/packages/code_editor/codeeditor/editor.py b/pyminer2/extensions/packages/code_editor/codeeditor/editor.py index d66ebb58..b431f26f 100644 --- a/pyminer2/extensions/packages/code_editor/codeeditor/editor.py +++ b/pyminer2/extensions/packages/code_editor/codeeditor/editor.py @@ -45,7 +45,7 @@ class FindDialog(QDialog): (bool, 'case_sensitive', self.tr('Case Sensitive'), True), (bool, 'whole_word', self.tr('Whole Word'), True), ] - self.settings_panel = SettingsPanel(parent=self,views=views) + self.settings_panel = SettingsPanel(parent=self, views=views) self.setLayout(QVBoxLayout()) self.layout().addWidget(self.settings_panel) self.button_up = QPushButton('up') @@ -522,7 +522,7 @@ class PMCodeEditor(QWidget, Ui_FormEditor): pos = self.textEdit.getCursorPosition() text = self.textEdit.text(pos[0]) try: - line = text[:pos[1]+1] + line = text[:pos[1] + 1] except Exception as e: logger.debug(e) line = '' @@ -759,6 +759,22 @@ class PMCodeEditor(QWidget, Ui_FormEditor): except Exception as e: logger.warning(str(e)) + def slot_run_in_terminal(self): + ''' + 在终端中运行代码 + 调用程序的进程管理接口。 + :return: + ''' + text = self.text().strip() + if not text: + return + path = self._path + try: + self._parent.run_python_file_in_terminal(path) + except Exception as e: + logger.warning(str(e)) + + def eventFilter(self, obj: 'QObject', event: 'QEvent') -> bool: # if event.type() == QEvent.MouseMove: # # 如果有错误则显示详情 diff --git a/pyminer2/extensions/packages/code_editor/codeeditor/tabwidget.py b/pyminer2/extensions/packages/code_editor/codeeditor/tabwidget.py index a831dc98..4f14993e 100644 --- a/pyminer2/extensions/packages/code_editor/codeeditor/tabwidget.py +++ b/pyminer2/extensions/packages/code_editor/codeeditor/tabwidget.py @@ -405,6 +405,17 @@ class PMCodeEditTabWidget(QTabWidget): self._index = 0 self.slot_new_script() + def slot_run_in_terminal(self): + ''' + 在终端中运行 + :return: + ''' + editor: 'PMCodeEditor' = self.currentWidget() + editor.slot_run_in_terminal() + + def run_python_file_in_terminal(self,file_path:str): + self.extension_lib.Program.run_python_file(file_path) + def _init_signals(self): # 标签页关闭信号 self.tabCloseRequested.connect(self.slot_tab_close_request) @@ -416,7 +427,7 @@ class PMCodeEditTabWidget(QTabWidget): self.extension_lib.UI.get_toolbar('toolbar_home').append_menu( 'button_new', self.tr('Script'), self.slot_new_script) self.extension_lib.UI.get_toolbar('toolbar_home').append_menu('button_open', self.tr('Script'), - self.slot_open_script) + self.slot_open_script) # 添加打开事件到文件树。 self.extension_lib.get_interface('file_tree').add_open_file_callback('.py', self.slot_new_script) # 创建新文档 @@ -452,6 +463,9 @@ class PMCodeEditTabWidget(QTabWidget): # 运行代码 PluginInterface.get_toolbar_widget('code_editor_toolbar', 'button_run_script').clicked.connect( self.slot_run_script) + + self.extension_lib.UI.get_toolbar_widget('code_editor_toolbar', 'button_run_in_terminal').clicked.connect( + self.slot_run_in_terminal) except Exception as e: logger.warning(str(e)) diff --git a/pyminer2/extensions/packages/code_editor/toolbar.py b/pyminer2/extensions/packages/code_editor/toolbar.py index d53f9306..c84811b1 100644 --- a/pyminer2/extensions/packages/code_editor/toolbar.py +++ b/pyminer2/extensions/packages/code_editor/toolbar.py @@ -53,3 +53,7 @@ class PMEditorToolbar(PMGToolBar): 'button_run_script', '运行', create_icon(':/color/source/theme/color/icons/mActionStartCheck.svg')) + + self.add_tool_button('button_run_in_terminal', + '终端中\n运行', + create_icon(':/color/source/theme/color/icons/mActionStartCheck.svg')) diff --git a/pyminer2/features/util/__init__.py b/pyminer2/features/util/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pyminer2/ui/common/openprocess.py b/pyminer2/features/util/openprocess.py similarity index 100% rename from pyminer2/ui/common/openprocess.py rename to pyminer2/features/util/openprocess.py diff --git a/pyminer2/ui/common/platformutil.py b/pyminer2/features/util/platformutil.py similarity index 80% rename from pyminer2/ui/common/platformutil.py rename to pyminer2/features/util/platformutil.py index 3b87d67a..ed779b04 100644 --- a/pyminer2/ui/common/platformutil.py +++ b/pyminer2/features/util/platformutil.py @@ -1,10 +1,10 @@ import platform import subprocess +import sys def check_platform() -> str: system = platform.system() - print(system) return system.lower() @@ -27,6 +27,12 @@ def run_command_in_terminal(cmd: str, close_mode: str = 'wait_key'): subprocess.Popen(command, shell=True) +def run_python_file_in_terminal(file_path, interpreter_path: str = None, close_mode: str = 'wait_key'): + if interpreter_path is None: + interpreter_path = sys.executable + run_command_in_terminal('%s %s' % (interpreter_path, file_path),close_mode=close_mode) + + if __name__ == '__main__': def test_run_in_terminal(): import time diff --git a/pyminer2/tests/test_features/__init__.py b/pyminer2/tests/test_features/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pyminer2/tests/test_features/python_file_to_run.py b/pyminer2/tests/test_features/python_file_to_run.py new file mode 100644 index 00000000..945afd31 --- /dev/null +++ b/pyminer2/tests/test_features/python_file_to_run.py @@ -0,0 +1,2 @@ +print('If you see this line,PyMiner can run code in terminal on this platform.') +print('如果你看到这些内容,说明PyMiner在这个平台上可以调用终端并执行代码') \ No newline at end of file diff --git a/pyminer2/tests/test_features/test_util.py b/pyminer2/tests/test_features/test_util.py new file mode 100644 index 00000000..12d737b5 --- /dev/null +++ b/pyminer2/tests/test_features/test_util.py @@ -0,0 +1,22 @@ +from pyminer2.features.util.platformutil import run_command_in_terminal,run_python_file_in_terminal + + +def test_run_command(): + import time + run_command_in_terminal('dir', close_mode='auto') + time.sleep(3) + run_command_in_terminal('dir', close_mode='wait_key') + time.sleep(3) + run_command_in_terminal('dir', close_mode='no') + time.sleep(3) + pass + + +def test_run_python_file_in_terminal(): + run_python_file_in_terminal('python_file_to_run.py',close_mode='wait_key') + pass + + +if __name__ == '__main__': + test_run_command() + test_run_python_file_in_terminal() diff --git a/pyminer2/ui/common/open_process_with_pyqt.py b/pyminer2/ui/common/open_process_with_pyqt.py index 18402a7a..ea17b6c0 100644 --- a/pyminer2/ui/common/open_process_with_pyqt.py +++ b/pyminer2/ui/common/open_process_with_pyqt.py @@ -7,13 +7,13 @@ import time import sys from PyQt5.QtCore import QThread, QObject, pyqtSignal from PyQt5.QtGui import QCloseEvent -from PyQt5.QtWidgets import QTextEdit, QWidget, QHBoxLayout, QVBoxLayout, QPushButton, QTextBrowser, QCheckBox +from PyQt5.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout, QPushButton, QTextBrowser, QCheckBox from typing import TYPE_CHECKING if TYPE_CHECKING: - from .openprocess import PMProcess + from pyminer2.features.util.openprocess import PMProcess else: - from pyminer2.ui.common.openprocess import PMProcess + from pyminer2.features.util.openprocess import PMProcess class ProcessMonitorThread(QObject):