From 82881f58822c90266bc080f6a90a1ede9c3b97f9 Mon Sep 17 00:00:00 2001 From: wznln <18435084+Xuwznln@users.noreply.github.com> Date: Sun, 20 Apr 2025 18:11:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81local=5Fconfig?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=20add:=20=E5=A2=9E=E5=8A=A0=E5=AF=B9crt=20pa?= =?UTF-8?q?th=E7=9A=84=E8=AF=B4=E6=98=8E=EF=BC=8C=E4=B8=BA=E4=BC=A0?= =?UTF-8?q?=E5=85=A5config.py=E7=9A=84=E7=9B=B8=E5=AF=B9=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=20move:=20web=20component?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- MANIFEST.in | 2 ++ docs/user_guide/configuration.md | 8 +++++--- unilabos/app/main.py | 11 +++++++---- unilabos/{ => app}/web/__init__.py | 8 ++++---- unilabos/{ => app}/web/api.py | 2 +- unilabos/{ => app}/web/client.py | 0 unilabos/{ => app}/web/pages.py | 6 +++--- unilabos/{ => app}/web/server.py | 4 ++-- unilabos/{ => app}/web/static/styles.css | 0 unilabos/{ => app}/web/templates/base.html | 0 unilabos/{ => app}/web/templates/home.html | 0 unilabos/{ => app}/web/templates/status.html | 0 unilabos/{ => app}/web/utils/__init__.py | 0 unilabos/{ => app}/web/utils/action_utils.py | 0 unilabos/{ => app}/web/utils/device_utils.py | 0 unilabos/{ => app}/web/utils/host_utils.py | 2 +- unilabos/{ => app}/web/utils/ros_utils.py | 2 +- unilabos/config/config.py | 17 ++++++----------- 19 files changed, 33 insertions(+), 31 deletions(-) rename unilabos/{ => app}/web/__init__.py (56%) rename unilabos/{ => app}/web/api.py (98%) rename unilabos/{ => app}/web/client.py (100%) rename unilabos/{ => app}/web/pages.py (96%) rename unilabos/{ => app}/web/server.py (96%) rename unilabos/{ => app}/web/static/styles.css (100%) rename unilabos/{ => app}/web/templates/base.html (100%) rename unilabos/{ => app}/web/templates/home.html (100%) rename unilabos/{ => app}/web/templates/status.html (100%) rename unilabos/{ => app}/web/utils/__init__.py (100%) rename unilabos/{ => app}/web/utils/action_utils.py (100%) rename unilabos/{ => app}/web/utils/device_utils.py (100%) rename unilabos/{ => app}/web/utils/host_utils.py (97%) rename unilabos/{ => app}/web/utils/ros_utils.py (97%) diff --git a/.gitignore b/.gitignore index 5ff8b0f7..333df5bf 100644 --- a/.gitignore +++ b/.gitignore @@ -229,6 +229,6 @@ CATKIN_IGNORE .DS_Store -local_config.py +/**/local_config.py *.graphml \ No newline at end of file diff --git a/MANIFEST.in b/MANIFEST.in index df3ec4b8..036215b8 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1 +1,3 @@ recursive-include unilabos/registry *.yaml +recursive-include unilabos/app/web *.html +recursive-include unilabos/app/web *.css diff --git a/docs/user_guide/configuration.md b/docs/user_guide/configuration.md index 9a8d67cd..049f59ac 100644 --- a/docs/user_guide/configuration.md +++ b/docs/user_guide/configuration.md @@ -46,9 +46,9 @@ class MQConfig: port: int = 8883 # 可以直接提供证书文件路径 - ca_file: str = "/path/to/ca.pem" - cert_file: str = "/path/to/cert.pem" - key_file: str = "/path/to/key.pem" + ca_file: str = "/path/to/ca.pem" # 相对config.py所在目录的路径 + cert_file: str = "/path/to/cert.pem" # 相对config.py所在目录的路径 + key_file: str = "/path/to/key.pem" # 相对config.py所在目录的路径 # 或者直接提供证书内容 ca_content: str = "" @@ -102,6 +102,8 @@ class ROSConfig: unilab --config path/to/your/config.py ``` +如果您不涉及多环境开发,可以在unilabos的安装路径中手动添加local_config.py的文件 + # 启动Uni-Lab python -m unilabos.app.main --config path/to/your/config.py ``` diff --git a/unilabos/app/main.py b/unilabos/app/main.py index 11d790b3..d418c5c1 100644 --- a/unilabos/app/main.py +++ b/unilabos/app/main.py @@ -69,14 +69,17 @@ def main(): args_dict = vars(args) # 加载配置文件 - 这里保持最先加载配置的逻辑 - if args_dict.get("config"): - config_path = args_dict["config"] + config_path = args_dict.get("config") + if config_path: if not os.path.exists(config_path): print_status(f"配置文件 {config_path} 不存在", "error") elif not config_path.endswith(".py"): print_status(f"配置文件 {config_path} 不是Python文件,必须以.py结尾", "error") else: load_config(config_path) + else: + print_status(f"启动 UniLab-OS时,配置文件参数未正确传入 --config '{config_path}' 尝试本地配置...", "warning") + load_config(config_path) # 设置BasicConfig参数 BasicConfig.is_host_mode = not args_dict.get("without_host", False) @@ -92,8 +95,8 @@ def main(): from unilabos.app.mq import mqtt_client from unilabos.registry.registry import build_registry from unilabos.app.backend import start_backend - from unilabos.web import http_client - from unilabos.web import start_server + from unilabos.app.web import http_client + from unilabos.app.web import start_server # 显示启动横幅 print_unilab_banner(args_dict) diff --git a/unilabos/web/__init__.py b/unilabos/app/web/__init__.py similarity index 56% rename from unilabos/web/__init__.py rename to unilabos/app/web/__init__.py index 7872ee0f..3fccdd7f 100644 --- a/unilabos/web/__init__.py +++ b/unilabos/app/web/__init__.py @@ -4,10 +4,10 @@ Web UI 模块 提供了UniLab系统的Web界面功能 """ -from unilabos.web.pages import setup_web_pages -from unilabos.web.server import setup_server, start_server -from unilabos.web.client import http_client -from unilabos.web.api import setup_api_routes +from unilabos.app.web.pages import setup_web_pages +from unilabos.app.web.server import setup_server, start_server +from unilabos.app.web.client import http_client +from unilabos.app.web.api import setup_api_routes __all__ = [ "setup_web_pages", # 设置Web页面 diff --git a/unilabos/web/api.py b/unilabos/app/web/api.py similarity index 98% rename from unilabos/web/api.py rename to unilabos/app/web/api.py index 84169a45..eb18b4dc 100644 --- a/unilabos/web/api.py +++ b/unilabos/app/web/api.py @@ -18,7 +18,7 @@ from unilabos.app.model import ( JobPreintakeFinishReq, JobFinishReq, ) -from unilabos.web.utils.host_utils import get_host_node_info +from unilabos.app.web.utils.host_utils import get_host_node_info # 创建API路由器 api = APIRouter() diff --git a/unilabos/web/client.py b/unilabos/app/web/client.py similarity index 100% rename from unilabos/web/client.py rename to unilabos/app/web/client.py diff --git a/unilabos/web/pages.py b/unilabos/app/web/pages.py similarity index 96% rename from unilabos/web/pages.py rename to unilabos/app/web/pages.py index ecbe84f2..7216a66c 100644 --- a/unilabos/web/pages.py +++ b/unilabos/app/web/pages.py @@ -20,9 +20,9 @@ from unilabos.app.mq import mqtt_client from unilabos.ros.msgs.message_converter import msg_converter_manager from unilabos.utils.log import error from unilabos.utils.type_check import TypeEncoder -from unilabos.web.utils.device_utils import get_registry_info -from unilabos.web.utils.host_utils import get_host_node_info -from unilabos.web.utils.ros_utils import get_ros_node_info, update_ros_node_info +from unilabos.app.web.utils.device_utils import get_registry_info +from unilabos.app.web.utils.host_utils import get_host_node_info +from unilabos.app.web.utils.ros_utils import get_ros_node_info, update_ros_node_info # 设置Jinja2模板环境 template_dir = Path(__file__).parent / "templates" diff --git a/unilabos/web/server.py b/unilabos/app/web/server.py similarity index 96% rename from unilabos/web/server.py rename to unilabos/app/web/server.py index 723db9b4..2a85d107 100644 --- a/unilabos/web/server.py +++ b/unilabos/app/web/server.py @@ -13,8 +13,8 @@ from starlette.responses import Response from unilabos.utils.fastapi.log_adapter import setup_fastapi_logging from unilabos.utils.log import info, error -from unilabos.web.api import setup_api_routes -from unilabos.web.pages import setup_web_pages +from unilabos.app.web.api import setup_api_routes +from unilabos.app.web.pages import setup_web_pages # 创建FastAPI应用 app = FastAPI( diff --git a/unilabos/web/static/styles.css b/unilabos/app/web/static/styles.css similarity index 100% rename from unilabos/web/static/styles.css rename to unilabos/app/web/static/styles.css diff --git a/unilabos/web/templates/base.html b/unilabos/app/web/templates/base.html similarity index 100% rename from unilabos/web/templates/base.html rename to unilabos/app/web/templates/base.html diff --git a/unilabos/web/templates/home.html b/unilabos/app/web/templates/home.html similarity index 100% rename from unilabos/web/templates/home.html rename to unilabos/app/web/templates/home.html diff --git a/unilabos/web/templates/status.html b/unilabos/app/web/templates/status.html similarity index 100% rename from unilabos/web/templates/status.html rename to unilabos/app/web/templates/status.html diff --git a/unilabos/web/utils/__init__.py b/unilabos/app/web/utils/__init__.py similarity index 100% rename from unilabos/web/utils/__init__.py rename to unilabos/app/web/utils/__init__.py diff --git a/unilabos/web/utils/action_utils.py b/unilabos/app/web/utils/action_utils.py similarity index 100% rename from unilabos/web/utils/action_utils.py rename to unilabos/app/web/utils/action_utils.py diff --git a/unilabos/web/utils/device_utils.py b/unilabos/app/web/utils/device_utils.py similarity index 100% rename from unilabos/web/utils/device_utils.py rename to unilabos/app/web/utils/device_utils.py diff --git a/unilabos/web/utils/host_utils.py b/unilabos/app/web/utils/host_utils.py similarity index 97% rename from unilabos/web/utils/host_utils.py rename to unilabos/app/web/utils/host_utils.py index 7d241ae0..0df5e816 100644 --- a/unilabos/web/utils/host_utils.py +++ b/unilabos/app/web/utils/host_utils.py @@ -9,7 +9,7 @@ from typing import Dict, Any from unilabos.config.config import BasicConfig from unilabos.ros.nodes.presets.host_node import HostNode -from unilabos.web.utils.action_utils import get_action_info +from unilabos.app.web.utils.action_utils import get_action_info def get_host_node_info() -> Dict[str, Any]: diff --git a/unilabos/web/utils/ros_utils.py b/unilabos/app/web/utils/ros_utils.py similarity index 97% rename from unilabos/web/utils/ros_utils.py rename to unilabos/app/web/utils/ros_utils.py index 713820fb..01733510 100644 --- a/unilabos/web/utils/ros_utils.py +++ b/unilabos/app/web/utils/ros_utils.py @@ -7,7 +7,7 @@ ROS 工具函数模块 import traceback from typing import Dict, Any -from unilabos.web.utils.action_utils import get_action_info +from unilabos.app.web.utils.action_utils import get_action_info # 存储 ROS 节点信息的全局变量 ros_node_info = {"online_devices": {}, "device_topics": {}, "device_actions": {}} diff --git a/unilabos/config/config.py b/unilabos/config/config.py index 28d787a2..5f117a74 100644 --- a/unilabos/config/config.py +++ b/unilabos/config/config.py @@ -28,9 +28,9 @@ class MQConfig: key_content = "" # 指定 - ca_file = "" - cert_file = "" - key_file = "" + ca_file = "" # 相对config.py所在目录的路径 + cert_file = "" # 相对config.py所在目录的路径 + key_file = "" # 相对config.py所在目录的路径 # OSS上传配置 @@ -97,7 +97,7 @@ def load_config(config_path=None): BasicConfig.config_path = os.path.abspath(os.path.dirname(config_path)) if not os.path.exists(config_path): logger.error(f"配置文件 {config_path} 不存在") - return + exit(1) try: module_name = "lab_" + os.path.basename(config_path).replace(".py", "") @@ -114,10 +114,5 @@ def load_config(config_path=None): traceback.print_exc() exit(1) else: - try: - import unilabos.config.local_config as local_config # type: ignore - - _update_config_from_module(local_config) - logger.info("已加载默认配置 unilabos.config.local_config") - except ImportError: - pass + config_path = os.path.join(os.path.dirname(__file__), "local_config.py") + load_config(config_path)