增加了在终端中运行代码的接口

将平台有关操作移动到features/util包中

添加测试用例
This commit is contained in:
侯展意 2020-09-24 23:47:44 +08:00
parent 4f382b0c07
commit 98890d4748
12 changed files with 85 additions and 16 deletions

View File

@ -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:

View File

@ -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表示按钮的回调函数

View File

@ -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:
# # 如果有错误则显示详情

View File

@ -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))

View File

@ -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'))

View File

View File

@ -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

View File

View File

@ -0,0 +1,2 @@
print('If you see this line,PyMiner can run code in terminal on this platform.')
print('如果你看到这些内容说明PyMiner在这个平台上可以调用终端并执行代码')

View File

@ -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()

View File

@ -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):