mirror of
https://github.com/yuruotong1/autoMate.git
synced 2026-03-22 13:07:17 +08:00
重构代码
This commit is contained in:
@@ -14,7 +14,11 @@ class ActionBase(BaseModel):
|
||||
args: Type[BaseModel]
|
||||
action_pos: int = -1
|
||||
action_level: int = -1
|
||||
__config_ui: ClassVar
|
||||
config_ui: ClassVar = QtUtil.load_ui("config_page.ui")
|
||||
|
||||
def __init__(self, **data: Any):
|
||||
super().__init__(**data)
|
||||
self.__ui_name_and_line_edit = {}
|
||||
|
||||
def run(self, *args, **kwargs):
|
||||
raise TypeError("Not realize run function")
|
||||
@@ -30,55 +34,49 @@ class ActionBase(BaseModel):
|
||||
args_schema=self.args
|
||||
)
|
||||
|
||||
@classmethod
|
||||
# 设置配置界面的布局
|
||||
def config_page_ui(cls):
|
||||
cls.__config_ui = QtUtil.load_ui("config_page.ui")
|
||||
def config_page_ui(self, the_insert_row):
|
||||
v_box_layout = QVBoxLayout()
|
||||
model_fields = cls.model_fields["args"].model_fields
|
||||
model_fields = self.model_fields["args"].annotation.model_fields
|
||||
for field in model_fields:
|
||||
# 水平布局
|
||||
h_box_layout = QHBoxLayout()
|
||||
label = QLabel(cls.__config_ui)
|
||||
label = QLabel(self.config_ui)
|
||||
label.setText(model_fields[field].title)
|
||||
line_edit = QLineEdit(cls.__config_ui)
|
||||
line_edit = QLineEdit(self.config_ui)
|
||||
h_box_layout.addWidget(label)
|
||||
h_box_layout.addWidget(line_edit)
|
||||
# 将输入内容填入参数列表
|
||||
cls.__ui_name_and_line_edit[field] = line_edit
|
||||
self.__ui_name_and_line_edit[field] = line_edit
|
||||
v_box_layout.addLayout(h_box_layout)
|
||||
save_button: QPushButton = cls.__config_ui.saveButton
|
||||
save_button.clicked.connect(cls.__save_button_clicked)
|
||||
|
||||
cancel_button: QPushButton = cls.__config_ui.cancelButton
|
||||
cancel_button.clicked.connect(cls.__cancel_button_clicked)
|
||||
container_widget = QWidget(cls.__config_ui)
|
||||
save_button: QPushButton = self.config_ui.saveButton
|
||||
save_button.clicked.__getattribute__("connect")(self.__save_button_clicked, the_insert_row)
|
||||
cancel_button: QPushButton = self.config_ui.cancelButton
|
||||
cancel_button.clicked.__getattribute__("connect")(self.__cancel_button_clicked
|
||||
)
|
||||
container_widget = QWidget(self.config_ui)
|
||||
container_widget.setLayout(v_box_layout)
|
||||
cls.__config_ui.config_list.addWidget(container_widget)
|
||||
self.config_ui.config_list.addWidget(container_widget)
|
||||
|
||||
@classmethod
|
||||
def __cancel_button_clicked(cls):
|
||||
cls.__config_ui.hide()
|
||||
def __cancel_button_clicked(self):
|
||||
self.config_ui.hide()
|
||||
|
||||
@classmethod
|
||||
def __save_button_clicked(cls):
|
||||
def __save_button_clicked(self, the_insert_row):
|
||||
args = {}
|
||||
from pages.edit_page import GlobalUtil
|
||||
for arg_name in cls.__ui_name_and_line_edit:
|
||||
cls.action_arg[arg_name] = cls.__ui_name_and_line_edit[arg_name].text()
|
||||
# 如果双击应用列表打开的配置页面,保存后向应用列表最后插入
|
||||
if cls.action_pos is None:
|
||||
cls.action_pos = GlobalUtil.current_page.action_list.count()
|
||||
cls.action_level = 0
|
||||
for arg_name in self.__ui_name_and_line_edit:
|
||||
args[arg_name] = self.__ui_name_and_line_edit[arg_name].text()
|
||||
self.action_pos = the_insert_row
|
||||
self.action_level = 0
|
||||
# 向新位置增加元素
|
||||
from pages.edit_action_list_view import ActionList
|
||||
ActionList.insert_item(GlobalUtil.current_page.action_list, cls.action_pos, cls)
|
||||
cls.__config_ui.hide()
|
||||
ActionList.insert_item(GlobalUtil.current_page.action_list, self.action_pos, self)
|
||||
self.config_ui.hide()
|
||||
|
||||
@classmethod
|
||||
def config_page_show(cls):
|
||||
cls.config_page_ui()
|
||||
if cls.__config_ui is None:
|
||||
def config_page_show(self, the_insert_row):
|
||||
self.config_page_ui(the_insert_row)
|
||||
if self.config_ui is None:
|
||||
raise TypeError("config_ui not config")
|
||||
# 居上对齐
|
||||
cls.__config_ui.config_list.layout().setAlignment(Qt.AlignmentFlag.AlignTop)
|
||||
cls.__config_ui.show()
|
||||
self.config_ui.config_list.layout().setAlignment(Qt.AlignmentFlag.AlignTop)
|
||||
self.config_ui.show()
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import subprocess
|
||||
from typing import Type
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
@@ -7,7 +6,7 @@ from actions.action_base import ActionBase
|
||||
|
||||
|
||||
class OpenApplicationInput(BaseModel):
|
||||
path: str = Field(description="要查询的关键词", title="应用路径")
|
||||
path: str = Field(description="要查询的关键词", title="应用路径", default="")
|
||||
|
||||
|
||||
class OpenApplicationAction(ActionBase):
|
||||
@@ -17,3 +16,9 @@ class OpenApplicationAction(ActionBase):
|
||||
|
||||
def run(self, path):
|
||||
subprocess.Popen(path)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
action = OpenApplicationAction(args={})
|
||||
action.args.path = "ccc"
|
||||
print(action)
|
||||
|
||||
@@ -5,7 +5,7 @@ from utils.selenium_util import SeleniumUtil
|
||||
|
||||
|
||||
class OpenBrowserUrlInput(BaseModel):
|
||||
url: str = Field(description="要访问的网址", title="网址")
|
||||
url: str = Field(description="要访问的网址", title="网址", default="")
|
||||
|
||||
|
||||
class OpenBrowserUrlAction(ActionBase):
|
||||
|
||||
@@ -5,7 +5,7 @@ from utils.selenium_util import SeleniumUtil
|
||||
|
||||
|
||||
class SearchInput(BaseModel):
|
||||
key: str = Field(description="要查询的关键词")
|
||||
key: str = Field(description="要查询的关键词", default="")
|
||||
|
||||
|
||||
# 利用搜索引擎搜索关键词
|
||||
|
||||
18
main.py
18
main.py
@@ -6,7 +6,8 @@ import traceback
|
||||
import leancloud
|
||||
from PyQt6.QtWidgets import QApplication
|
||||
|
||||
from pages.edit_page import GlobalUtil
|
||||
from pages.edit_action_list_view import ActionList
|
||||
from pages.edit_page import GlobalUtil, EditPage
|
||||
from pages.func_list_page import FuncListPage
|
||||
from pages.login_page import LoginPage
|
||||
from utils.config import Config
|
||||
@@ -44,10 +45,23 @@ def excepthook(exc_type, exc_value, exc_tb):
|
||||
print("catch exception:", tb)
|
||||
|
||||
|
||||
def load():
|
||||
edit_pages_json = GlobalUtil.load_data()
|
||||
for edit_page_json in edit_pages_json:
|
||||
edit_page = EditPage(
|
||||
func_status=edit_page_json["func_status"],
|
||||
func_list_pos_row=edit_page_json["func_list_pos_row"],
|
||||
func_list_pos_column=edit_page_json["func_list_pos_column"],
|
||||
action_list=ActionList.load(edit_page_json["action_list"]))
|
||||
edit_page.func_name = edit_page_json["func_name"]
|
||||
edit_page.func_description = edit_page_json["func_description"]
|
||||
GlobalUtil.edit_page_global.append(edit_page)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.excepthook = excepthook
|
||||
app = QApplication(sys.argv)
|
||||
GlobalUtil.init()
|
||||
load()
|
||||
page = FuncListPage()
|
||||
page.show()
|
||||
sys.exit(app.exec())
|
||||
|
||||
@@ -26,7 +26,7 @@ class ActionListItem(QListWidgetItem):
|
||||
raise ValueError("data must have a key named 'name'")
|
||||
|
||||
def dump(self):
|
||||
return self.action.dict
|
||||
return self.action.dict()
|
||||
|
||||
|
||||
class ActionList(QListWidget):
|
||||
@@ -73,7 +73,7 @@ class ActionList(QListWidget):
|
||||
item = self.item(i)
|
||||
if not isinstance(item, ActionBase):
|
||||
raise TypeError("item must be an instance of ActionListItem")
|
||||
res.append(item.dict)
|
||||
res.append(item.dict())
|
||||
return res
|
||||
|
||||
def init(self):
|
||||
@@ -206,8 +206,8 @@ class ActionList(QListWidget):
|
||||
from actions.action_util import ActionUtil
|
||||
action = ActionUtil.get_action_by_name(source_data)
|
||||
# 打开配置页面
|
||||
self.drop_down_action = action
|
||||
self.drop_down_action.config_page_show()
|
||||
self.drop_down_action = action(args={})
|
||||
self.drop_down_action.config_page_show(self.the_insert_row)
|
||||
else:
|
||||
drag_action_item = ActionListItem.load(source_data)
|
||||
drag_action_item.action.action_pos = self.the_insert_row
|
||||
|
||||
@@ -6,6 +6,7 @@ from PyQt6.QtWidgets import QAbstractItemView, QListWidget, QListWidgetItem
|
||||
|
||||
from actions.action_util import ActionUtil
|
||||
from pages.edit_action_list_view import ActionListItem
|
||||
from pages.global_util import GlobalUtil
|
||||
|
||||
|
||||
class FunctionListView(QListWidget):
|
||||
@@ -27,7 +28,7 @@ class FunctionListView(QListWidget):
|
||||
if not isinstance(item, ActionListItem):
|
||||
return
|
||||
# 打开配置页面
|
||||
item.action.config_page_show()
|
||||
item.action.config_page_show(GlobalUtil.current_page.action_list.count())
|
||||
|
||||
# 记录拖拽初始位置
|
||||
def mousePressEvent(self, e):
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import os
|
||||
import pickle
|
||||
|
||||
from pages.bse_page import BasePage
|
||||
from pages.edit_action_list_view import ActionList
|
||||
from pages.edit_function_page import FunctionListView
|
||||
from pages.global_util import GlobalUtil
|
||||
from utils.qt_util import QtUtil
|
||||
|
||||
|
||||
@@ -30,17 +28,6 @@ class EditPage(BasePage):
|
||||
"action_list": self.action_list.dump()
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def load(cls, edit_page_dict):
|
||||
edit_page = EditPage(
|
||||
func_status=edit_page_dict["func_status"],
|
||||
func_list_pos_row=edit_page_dict["func_list_pos_row"],
|
||||
func_list_pos_column=edit_page_dict["func_list_pos_column"],
|
||||
action_list=ActionList.load(edit_page_dict["action_list"]))
|
||||
edit_page.func_name = edit_page_dict["func_name"]
|
||||
edit_page.func_description = edit_page_dict["func_description"]
|
||||
return edit_page
|
||||
|
||||
def setup_up(self):
|
||||
self.ui = QtUtil.load_ui("edit_page.ui")
|
||||
self.ui.func_name_edit.setText(self.func_name)
|
||||
@@ -77,53 +64,3 @@ class EditPage(BasePage):
|
||||
func = self.action_list.item(index)
|
||||
res = func.__getattribute__("get_action")().run_with_out_arg()
|
||||
print("执行结果:", res)
|
||||
|
||||
|
||||
class GlobalUtil:
|
||||
edit_page_global: list[EditPage] = []
|
||||
current_page: EditPage = None
|
||||
|
||||
@classmethod
|
||||
def get_edit_page_by_position(cls, func_status, row, column):
|
||||
for i in cls.edit_page_global:
|
||||
if i.func_list_pos_row == row and i.func_list_pos_column == column \
|
||||
and i.func_status == func_status:
|
||||
return i
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def delete_edit_page(cls, edit_page):
|
||||
cls.edit_page_global.remove(edit_page)
|
||||
|
||||
@classmethod
|
||||
def read_from_local(cls):
|
||||
# 判断文件是否存在
|
||||
if not os.path.exists("./cache"):
|
||||
return []
|
||||
|
||||
with open("./cache", "rb") as file:
|
||||
data = pickle.load(file).get("action_list_global")
|
||||
if not data:
|
||||
data = []
|
||||
return data
|
||||
|
||||
@classmethod
|
||||
def save_to_local(cls):
|
||||
with open("./cache", "wb") as file:
|
||||
edit_page_dump = [i.dump() for i in cls.edit_page_global]
|
||||
pickle.dump({"action_list_global": edit_page_dump}, file)
|
||||
|
||||
@classmethod
|
||||
def init(cls):
|
||||
# 根据配置文件的配置,从本地文件中或者网上读取
|
||||
from utils.config import Config
|
||||
config = Config()
|
||||
cls.edit_page_global = []
|
||||
if config.DATA_POSITION == "local":
|
||||
edit_pages_json = cls.read_from_local()
|
||||
elif config.DATA_POSITION == "remote":
|
||||
edit_pages_json = []
|
||||
else:
|
||||
edit_pages_json = []
|
||||
for edit_page_json in edit_pages_json:
|
||||
cls.edit_page_global.append(EditPage.load(edit_page_json))
|
||||
|
||||
51
pages/global_util.py
Normal file
51
pages/global_util.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import os
|
||||
import pickle
|
||||
|
||||
|
||||
class GlobalUtil:
|
||||
edit_page_global = []
|
||||
current_page = None
|
||||
|
||||
@classmethod
|
||||
def get_edit_page_by_position(cls, func_status, row, column):
|
||||
for i in cls.edit_page_global:
|
||||
if i.func_list_pos_row == row and i.func_list_pos_column == column \
|
||||
and i.func_status == func_status:
|
||||
return i
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def delete_edit_page(cls, edit_page):
|
||||
cls.edit_page_global.remove(edit_page)
|
||||
|
||||
@classmethod
|
||||
def read_from_local(cls):
|
||||
# 判断文件是否存在
|
||||
if not os.path.exists("./cache"):
|
||||
return []
|
||||
|
||||
with open("./cache", "rb") as file:
|
||||
data = pickle.load(file).get("action_list_global")
|
||||
if not data:
|
||||
data = []
|
||||
return data
|
||||
|
||||
@classmethod
|
||||
def save_to_local(cls):
|
||||
with open("./cache", "wb") as file:
|
||||
edit_page_dump = [i.dump() for i in cls.edit_page_global]
|
||||
pickle.dump({"action_list_global": edit_page_dump}, file)
|
||||
|
||||
@classmethod
|
||||
def load_data(cls):
|
||||
# 根据配置文件的配置,从本地文件中或者网上读取
|
||||
from utils.config import Config
|
||||
config = Config()
|
||||
cls.edit_page_global = []
|
||||
if config.DATA_POSITION == "local":
|
||||
edit_pages_json = cls.read_from_local()
|
||||
elif config.DATA_POSITION == "remote":
|
||||
edit_pages_json = []
|
||||
else:
|
||||
edit_pages_json = []
|
||||
return edit_pages_json
|
||||
13
tests/test_base_model.py
Normal file
13
tests/test_base_model.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from typing import ClassVar
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class TmpBaseModel(BaseModel):
|
||||
name: ClassVar[str] = "abc"
|
||||
description: str = "hello"
|
||||
|
||||
|
||||
def test_model():
|
||||
t = TmpBaseModel(description="ddd")
|
||||
print(t.dict)
|
||||
Reference in New Issue
Block a user