From f604e8ac999929429cb2ff9f0f05e0ee0490f5a6 Mon Sep 17 00:00:00 2001 From: li-xiaochen <397721316@qq.com> Date: Sat, 14 Dec 2024 12:37:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9E=84=E5=BB=BA=EF=BC=9A=E6=89=93=E5=8C=85?= =?UTF-8?q?=E5=92=8C=E5=BC=80=E5=8F=91=E6=97=B6=E4=BD=BF=E7=94=A8=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E7=9A=84=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- launcher.py | 5 +-- launcher/sys_config/__init__.py | 54 +++++++++++++++++++++++++++ launcher/sys_config/config_dist.yaml | 2 + launcher/sys_config/config_local.yaml | 2 + log.py | 4 +- 6 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 launcher/sys_config/__init__.py create mode 100644 launcher/sys_config/config_dist.yaml create mode 100644 launcher/sys_config/config_local.yaml diff --git a/README.md b/README.md index 5f04d6a..20aec4e 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ 前端运行 `npm run build` 生成 `ui/dist`,之后安装 PyInstaller,运行 ```bash -pyinstaller -w --add-data ui/dist:ui/dist launcher.py +pyinstaller -w --add-data "ui/dist;ui/dist" --add-data "launcher/sys_config/config_dist.yaml" launcher.py ``` 在dist文件夹生成launcher文件夹,切到dist文件夹下运行 diff --git a/launcher.py b/launcher.py index 59d6059..8d89033 100644 --- a/launcher.py +++ b/launcher.py @@ -16,6 +16,7 @@ import webview from py7zr.callbacks import ExtractCallback from launcher import config +from launcher.sys_config import sys_config from log import logger mimetypes.add_type("text/html", ".html") @@ -344,9 +345,7 @@ class Api: if Path(upgrade_script_name).exists(): os.remove(upgrade_script_name) -# url = "ui/dist/index.html" -url = "http://localhost:5173/" window = webview.create_window( - f"mower-ng launcher {version}", url, js_api=Api() + f"mower-ng launcher {version}", sys_config.get('url'), js_api=Api() ) webview.start() diff --git a/launcher/sys_config/__init__.py b/launcher/sys_config/__init__.py new file mode 100644 index 0000000..124991a --- /dev/null +++ b/launcher/sys_config/__init__.py @@ -0,0 +1,54 @@ +import sys +import os + +import yaml + + +class SysConfig: + """ + 读取系统配置文件 + """ + # ui路径 + url: str + # 日志输出级别 + log_level: str + + def __init__(self): + self.config = {} + self.config_path = self.get_config_path() + self.load_config() + + def get_config_path(self): + if getattr(sys, 'frozen', False): + # logger.error("打包配置") + # 如果是打包后的可执行文件 + base_path = sys._MEIPASS + config_subdir = 'launcher/sys_config' # 添加子目录 + config_filename = 'config_dist.yaml' + else: + # logger.error("本地配置") + # 如果是本地开发环境 + base_path = os.path.dirname(__file__) + config_subdir = '' # 本地开发环境不需要子目录 + config_filename = 'config_local.yaml' + + config_path = os.path.join(base_path, config_subdir, config_filename) + return config_path + + def load_config(self): + try: + with open(self.config_path, 'r', encoding='utf-8') as file: + self.config = yaml.safe_load(file) + except FileNotFoundError: + pass + # logger.error(f"配置文件未找到: {self.config_path}") + except Exception as e: + pass + # logger.error(f"加载配置文件时出错: {str(e)}") + + def get(self, key): + return self.config.get(key) + + +# 创建一个全局配置实例 +sys_config = SysConfig() diff --git a/launcher/sys_config/config_dist.yaml b/launcher/sys_config/config_dist.yaml new file mode 100644 index 0000000..5b429a8 --- /dev/null +++ b/launcher/sys_config/config_dist.yaml @@ -0,0 +1,2 @@ +url: "ui/dist/index.html" +log_level: "ERROR" \ No newline at end of file diff --git a/launcher/sys_config/config_local.yaml b/launcher/sys_config/config_local.yaml new file mode 100644 index 0000000..8e6b8e3 --- /dev/null +++ b/launcher/sys_config/config_local.yaml @@ -0,0 +1,2 @@ +url: "http://localhost:5173/" +log_level: "INFO" \ No newline at end of file diff --git a/log.py b/log.py index fe747c0..efe83fc 100644 --- a/log.py +++ b/log.py @@ -3,11 +3,13 @@ import logging import sys from logging.handlers import RotatingFileHandler -log_level = logging.INFO +from launcher.sys_config import sys_config # 配置日志 def setup_logger(): + log_level = sys_config.get('log_level') + logger = logging.getLogger("launcher.log") logger.setLevel(log_level)