增加获取项目根目录和apps路径的通用函数
This commit is contained in:
parent
6c344f776f
commit
db4cb24048
@ -1,6 +1,6 @@
|
||||
from pydantic_settings import BaseSettings
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
from jingrow.utils.path import get_root_path
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
@ -49,7 +49,7 @@ class Settings(BaseSettings):
|
||||
watch: bool = True
|
||||
|
||||
class Config:
|
||||
env_file = str(Path(__file__).resolve().parents[3] / '.env')
|
||||
env_file = str(get_root_path() / '.env')
|
||||
env_file_encoding = 'utf-8'
|
||||
case_sensitive = False
|
||||
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
import os
|
||||
|
||||
from jingrow.config import Config
|
||||
from jingrow.utils.path import get_apps_path
|
||||
|
||||
|
||||
def main() -> None:
|
||||
@ -22,9 +23,7 @@ def main() -> None:
|
||||
]
|
||||
# 仅从 Config.watch 控制是否启用 watch,目录固定为 apps 根目录
|
||||
if bool(getattr(Config, "watch", False)):
|
||||
current_dir = os.path.dirname(__file__)
|
||||
apps_dir = os.path.abspath(os.path.join(current_dir, "..", ".."))
|
||||
args.extend(["--watch", apps_dir])
|
||||
args.extend(["--watch", str(get_apps_path())])
|
||||
os.execvp("dramatiq", args)
|
||||
|
||||
|
||||
|
||||
@ -19,6 +19,7 @@ from jingrow.config import Config
|
||||
from jingrow.utils.jingrow_api import get_jingrow_api_headers
|
||||
from jingrow.utils.jingrow_api import get_record_id, create_record
|
||||
from jingrow.utils.app_manager import update_apps_txt
|
||||
from jingrow.utils.path import get_root_path, get_apps_path
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -36,8 +37,7 @@ def handle_errors(func):
|
||||
|
||||
def get_app_directories():
|
||||
"""获取应用目录路径"""
|
||||
project_root = Path(__file__).resolve().parents[4]
|
||||
apps_dir = project_root / "apps"
|
||||
apps_dir = get_apps_path()
|
||||
apps_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
return apps_dir, apps_dir
|
||||
@ -47,8 +47,7 @@ def get_app_directories():
|
||||
def extract_package(zip_path: str) -> Dict[str, Any]:
|
||||
"""解压安装包 - 支持 ZIP 和 TAR.GZ"""
|
||||
# 获取项目根目录
|
||||
project_root = Path(__file__).resolve().parents[4]
|
||||
tmp_dir = project_root / "tmp"
|
||||
tmp_dir = get_root_path() / "tmp"
|
||||
tmp_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# 创建唯一临时目录
|
||||
|
||||
@ -9,6 +9,7 @@ Jingrow App 生命周期管理
|
||||
import sys
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from jingrow.utils.path import get_root_path
|
||||
from typing import Set
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@ -19,7 +20,7 @@ _loaded_app_paths: Set[str] = set()
|
||||
|
||||
def get_project_root() -> Path:
|
||||
"""获取项目根目录路径"""
|
||||
return Path(__file__).resolve().parents[4]
|
||||
return get_root_path()
|
||||
|
||||
|
||||
def get_apps_dir() -> Path:
|
||||
|
||||
18
apps/jingrow/jingrow/utils/path.py
Normal file
18
apps/jingrow/jingrow/utils/path.py
Normal file
@ -0,0 +1,18 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def get_root_path() -> Path:
|
||||
"""返回项目根目录:从 utils/path.py 回退到仓库根 (../../..)。"""
|
||||
return Path(__file__).resolve().parents[3]
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def get_apps_path() -> Path:
|
||||
"""返回 apps 目录路径。"""
|
||||
return get_root_path() / "apps"
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user