Просмотр исходного кода

fix(process): Configuration fails to be modified due to incorrect configuration

runhey 3 лет назад
Родитель
Сommit
351c1e7fea

+ 5 - 1
module/base/log_highlighter.py

@@ -4,6 +4,10 @@
 import re
 from html import escape
 
+def replace_newline_with_br(input_text):
+    replaced_text = input_text.replace('\n', '<br>')
+    return replaced_text
+
 def highlight_text(input_text):
     color_mapping = {
         r'\d{2}:\d{2}:\d{2}\.\d{3}': 'cyan',
@@ -19,7 +23,7 @@ def highlight_text(input_text):
     for pattern, color in color_mapping.items():
         input_text = re.sub(pattern, f'<span style="color: {color}">\\g<0></span>', input_text)
 
-    return input_text + '<br>'
+    return replace_newline_with_br(input_text)
 
 # 测试
 # input_string = 'The time is 13:20:36.411. Is it True or False? DEBUG: This is a debug message. INFO: This is an info message. WARNING: This is a warning message. ERROR: This is an error message. CRITICAL: This is a critical message.'

+ 68 - 0
module/config/config_modify.py

@@ -0,0 +1,68 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+from module.config.config import Config
+from module.config.utils import convert_to_underscore
+from module.config.config_model import ConfigModel
+
+from module.logger import logger
+from pydantic import BaseModel, ValidationError
+
+
+
+class ConfigModify(Config):
+    """
+    这个类的出现是为了修补一个架构问题:
+    不同于Alas,我默认用户在GUI界面点击的时候就启动了脚本进程,初始化会直接同时初始化一个config和一个device
+    如果用户配置不对这个进程直接挂掉了,用户甚至没有修改config的机会,即时重启了也会由于没有修改config而再次挂掉
+
+    因此这个类的出现是为了在脚本进程挂掉的时候,用户可以修改config,然后再次启动脚本进程
+
+
+    """
+
+    def __init__(self, config: str) -> None:
+        super().__init__(config)
+
+
+    def gui_args(self, task: str) -> str:
+        """
+        获取给gui显示的参数
+        :return:
+        """
+        return super().gui_args(task=task)
+
+    def gui_task(self, task: str) -> str:
+        """
+        获取给gui显示的任务 的参数的具体值
+        :return:
+        """
+        return self.model.gui_task(task=task)
+
+    def gui_set_task(self, task: str, group: str, argument: str, value) -> bool:
+        """
+        设置给gui显示的任务 的参数的具体值
+        :return:
+        """
+        task = convert_to_underscore(task)
+        group = convert_to_underscore(group)
+        argument = convert_to_underscore(argument)
+
+        path = f'{task}.{group}.{argument}'
+        task_object = getattr(self.model, task, None)
+        group_object = getattr(task_object, group, None)
+        argument_object = getattr(group_object, argument, None)
+
+        if argument_object is None:
+            logger.error(f'gui_set_task {task}.{group}.{argument}.{value} failed')
+            return False
+
+        try:
+            setattr(group_object, argument, value)
+            argument_object = getattr(group_object, argument, None)
+            logger.info(f'gui_set_task {task}.{group}.{argument}.{argument_object}')
+            super().save()  # 我是没有想到什么方法可以使得属性改变自动保存的
+            return True
+        except ValidationError as e:
+            logger.error(e)
+            return False

+ 21 - 4
module/gui/context/process_manager.py

@@ -24,6 +24,7 @@ from threading import Thread
 from module.base.log_highlighter import highlight_text
 from module.gui.process.script_process import ScriptProcess
 from module.config.config_menu import ConfigMenu
+from module.config.config_modify import ConfigModify
 from module.gui.context.add import Add
 from module.logger import logger
 
@@ -63,6 +64,7 @@ class ProcessManager(QObject):
         """
         super().__init__()
         self.processes: Dict[str, ScriptProcess] = {}  # 持有所有的进程
+        self.configs: Dict[str, ConfigModify] = {}  # 持有所有的配置, 权宜之计
         self.ports: Dict[str, int] = {}  # 持有所有的端口
         self.clients = {}  # zerorpc连接的客户端
 
@@ -145,7 +147,7 @@ class ProcessManager(QObject):
         """
         if config in self.processes:
             if not self.processes[config].is_alive():  # 如果进程已经死亡,那么就重新启动
-                logger.info(f'restart script {config}')
+                logger.info(f'{config} process is dead, restart it')
 
 
             self.processes[config].terminate()  # 强制结束进程
@@ -208,6 +210,21 @@ class ProcessManager(QObject):
         menu = ConfigMenu()
         return menu.gui_menu
 
+    def check_script(self, config: str):
+        """
+        检查脚本是否存在
+        :param config:
+        :return:
+        """
+        if config in self.processes and self.processes[config].is_alive():  # 如果脚本进程存在
+            if config in self.configs:
+                del self.configs[config]
+            return self.clients[config]
+        else:
+            if config not in self.configs:
+                self.configs[config] = ConfigModify(config)  # 初始化一个
+            return self.configs[config]
+
     @Slot(str, str, result="QString")
     def gui_args(self, config: str, task: str) -> str:
         """
@@ -218,7 +235,7 @@ class ProcessManager(QObject):
         """
         if config in self.clients:
             logger.info(f'gui get args of {config} {task}')
-            return self.clients[config].gui_args(task)
+            return self.check_script(config).gui_args(task)
         else:
             logger.info(f'script {config} is not running')
             return None
@@ -233,7 +250,7 @@ class ProcessManager(QObject):
         """
         if config in self.clients:
             logger.info(f'gui get value of {config} {task}')
-            return self.clients[config].gui_task(task)
+            return self.check_script(config).gui_task(task)
         else:
             logger.info(f'script {config} is not running')
             return None
@@ -251,7 +268,7 @@ class ProcessManager(QObject):
         """
         if config in self.clients:
             logger.info(f'gui set value of {config} {task}')
-            if self.clients[config].gui_set_task(task, group, arg, value):
+            if self.check_script(config).gui_set_task(task, group, arg, value):
                 return True
             else:
                 return False

+ 1 - 1
module/gui/qml/Component/Overview.qml

@@ -296,7 +296,7 @@ Item {
             anchors.fill: parent
         FluText{
             id: textLog
-            width: parent.width
+            width: parent.width-10
             padding: 10
             clip: true
             wrapMode: Text.WordWrap // 设置自动换行模式