Răsfoiți Sursa

Merge pull request #1011 from BluettDream/dev_nav_script

feat: add rename config and delete config interface
huangrunheng 1 an în urmă
părinte
comite
f30c225f3c
2 a modificat fișierele cu 81 adăugiri și 2 ștergeri
  1. 43 1
      module/server/config_manager.py
  2. 38 1
      module/server/script_router.py

+ 43 - 1
module/server/config_manager.py

@@ -90,5 +90,47 @@ class ConfigManager:
         new_script_number = script_numbers[-1] + 1
         new_script_number = script_numbers[-1] + 1
         return f'oas{new_script_number}'
         return f'oas{new_script_number}'
 
 
+    @staticmethod
+    def rename(old_name: str, new_name: str) -> bool:
+        """
+        重命名一个配置文件
+        :param old_name: 旧的配置文件名称
+        :param new_name: 新的配置文件名称
+        :return: True or False
+        """
+        config_path = Path.cwd() / 'config'
+        old_path = config_path / f'{old_name}.json'
+        new_path = config_path / f'{new_name}.json'
+        if not old_path.exists():
+            logger.error(f'{old_path} is not exists')
+            return False
+        if new_path.exists():
+            logger.error(f'{new_path} is exists')
+            return False
+        try:
+            old_path.rename(new_path)
+            logger.info(f'rename {old_path} to {new_path}')
+            return True
+        except Exception as e:
+            logger.error(f'rename {old_path} to {new_path} failed: {e}')
+            return False
 
 
-
+    @staticmethod
+    def delete(file: str) -> bool:
+        """
+        删除一个配置文件
+        :param file:  不带json后缀
+        :return: True or False
+        """
+        config_path = Path.cwd() / 'config'
+        file_path = config_path / f'{file}.json'
+        if not file_path.exists():
+            logger.error(f'{file_path} is not exists')
+            return False
+        try:
+            file_path.unlink()
+            logger.info(f'delete {file_path}')
+            return True
+        except Exception as e:
+            logger.error(f'delete {file_path} failed: {e}')
+            return False

+ 38 - 1
module/server/script_router.py

@@ -10,7 +10,7 @@ from module.config.config import Config
 
 
 from module.logger import logger
 from module.logger import logger
 from module.server.main_manager import MainManager
 from module.server.main_manager import MainManager
-from module.server.script_process import ScriptProcess
+from module.server.script_process import ScriptProcess, ScriptState
 
 
 from tasks.Component.config_base import TimeDelta
 from tasks.Component.config_base import TimeDelta
 
 
@@ -44,6 +44,43 @@ async def config_all():
     return mm.all_json_file()
     return mm.all_json_file()
 
 
 
 
+@script_app.put('/config')
+async def config_rename(old_name: str = '', new_name: str = ''):
+    """
+    update config name
+    :param old_name: old config name
+    :param new_name: new config name
+    :return: True or False
+    """
+    if old_name == new_name or new_name == '':
+        return False
+    if old_name in mm.script_process:
+        if mm.script_process[old_name].state != ScriptState.INACTIVE:
+            mm.script_process[old_name].stop()
+        del mm.script_process[old_name]
+    if not mm.rename(old_name, new_name):
+        raise HTTPException(status_code=400, detail='Rename failed')
+    return True
+
+
+@script_app.delete('/config')
+async def config_delete(name: str = ''):
+    """
+    delete config file
+    :param name: config name
+    :return: True or False
+    """
+    if name == '' or name == 'template':
+        raise HTTPException(status_code=400, detail='Delete failed')
+    if name in mm.script_process:
+        if mm.script_process[name].state != ScriptState.INACTIVE:
+            mm.script_process[name].stop()
+        del mm.script_process[name]
+    if not mm.delete(name):
+        raise HTTPException(status_code=400, detail='Delete failed')
+    return True
+
+
 # ---------------------------------   脚本实例管理   ----------------------------------
 # ---------------------------------   脚本实例管理   ----------------------------------
 @script_app.get('/{script_name}/start')
 @script_app.get('/{script_name}/start')
 async def script_start(script_name: str):
 async def script_start(script_name: str):