让 .env 文件可以在系统 config 文件夹内盛放
This commit is contained in:
26
README.md
26
README.md
@ -44,9 +44,33 @@
|
|||||||
|
|
||||||
### 2. 配置
|
### 2. 配置
|
||||||
|
|
||||||
|
API 配置从以下位置按优先级读取:
|
||||||
|
1. `scripts/.env` (本地)
|
||||||
|
2. `~/.config/phomework/.env` (全局)
|
||||||
|
|
||||||
|
**方式一:全局配置(推荐)**
|
||||||
|
```bash
|
||||||
|
mkdir -p ~/.config/phomework
|
||||||
|
cp scripts/.env.example ~/.config/phomework/.env
|
||||||
|
# 编辑 ~/.config/phomework/.env
|
||||||
|
```
|
||||||
|
|
||||||
|
**方式二:本地配置**
|
||||||
```bash
|
```bash
|
||||||
cp scripts/.env.example scripts/.env
|
cp scripts/.env.example scripts/.env
|
||||||
# 编辑 .env,填入 API 配置
|
# 编辑 scripts/.env
|
||||||
|
```
|
||||||
|
|
||||||
|
`.env` 文件内容:
|
||||||
|
```bash
|
||||||
|
# API 端点(OpenAI 兼容)
|
||||||
|
IMG2TYP_API_ENDPOINT=https://api.openai.com/v1/chat/completions
|
||||||
|
|
||||||
|
# API 密钥
|
||||||
|
IMG2TYP_API_KEY=your-api-key-here
|
||||||
|
|
||||||
|
# 模型名称(默认 qwen-vl-plus)
|
||||||
|
IMG2TYP_MODEL=qwen-vl-plus
|
||||||
```
|
```
|
||||||
|
|
||||||
### 3. 运行
|
### 3. 运行
|
||||||
|
|||||||
@ -15,7 +15,9 @@ console = Console(
|
|||||||
|
|
||||||
SCRIPT_DIR = Path(__file__).parent
|
SCRIPT_DIR = Path(__file__).parent
|
||||||
DATA_DIR = SCRIPT_DIR.parent / "data"
|
DATA_DIR = SCRIPT_DIR.parent / "data"
|
||||||
ENV_FILE = SCRIPT_DIR / ".env"
|
GLOBAL_ENV_DIR = Path.home() / ".config" / "phomework"
|
||||||
|
GLOBAL_ENV_FILE = GLOBAL_ENV_DIR / ".env"
|
||||||
|
LOCAL_ENV_FILE = SCRIPT_DIR / ".env"
|
||||||
|
|
||||||
|
|
||||||
def setup_logging(name: str, verbose: bool) -> logging.Logger:
|
def setup_logging(name: str, verbose: bool) -> logging.Logger:
|
||||||
@ -31,11 +33,23 @@ def setup_logging(name: str, verbose: bool) -> logging.Logger:
|
|||||||
|
|
||||||
|
|
||||||
def load_env() -> dict[str, str]:
|
def load_env() -> dict[str, str]:
|
||||||
"""Load environment variables from .env file."""
|
"""Load environment variables from .env file.
|
||||||
if ENV_FILE.exists():
|
|
||||||
load_dotenv(ENV_FILE)
|
Priority: scripts/.env > ~/.config/phomework/.env
|
||||||
|
"""
|
||||||
|
env_loaded = False
|
||||||
|
|
||||||
|
if LOCAL_ENV_FILE.exists():
|
||||||
|
load_dotenv(LOCAL_ENV_FILE)
|
||||||
|
env_loaded = True
|
||||||
|
elif GLOBAL_ENV_FILE.exists():
|
||||||
|
load_dotenv(GLOBAL_ENV_FILE)
|
||||||
|
env_loaded = True
|
||||||
|
console.print(f"[dim]Using config from {GLOBAL_ENV_FILE}[/dim]")
|
||||||
else:
|
else:
|
||||||
console.print(f"[yellow]Warning: .env file not found at {ENV_FILE}[/yellow]")
|
console.print(
|
||||||
|
f"[yellow]Warning: .env not found at {LOCAL_ENV_FILE} or {GLOBAL_ENV_FILE}[/yellow]"
|
||||||
|
)
|
||||||
|
|
||||||
api_endpoint = os.environ.get("IMG2TYP_API_ENDPOINT", "")
|
api_endpoint = os.environ.get("IMG2TYP_API_ENDPOINT", "")
|
||||||
api_key = os.environ.get("IMG2TYP_API_KEY", "")
|
api_key = os.environ.get("IMG2TYP_API_KEY", "")
|
||||||
|
|||||||
Reference in New Issue
Block a user