Delete utils.py
Browse files
utils.py
DELETED
|
@@ -1,53 +0,0 @@
|
|
| 1 |
-
"""公共工具函数"""
|
| 2 |
-
import os
|
| 3 |
-
import importlib.util
|
| 4 |
-
import httpx
|
| 5 |
-
from pathlib import Path
|
| 6 |
-
from typing import Dict, Optional
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
def get_proxies() -> Optional[Dict[str, str]]:
|
| 10 |
-
"""
|
| 11 |
-
从环境变量获取代理配置
|
| 12 |
-
读取 HTTP_PROXY 环境变量并返回代理字典
|
| 13 |
-
"""
|
| 14 |
-
proxy = os.getenv("HTTP_PROXY", "").strip()
|
| 15 |
-
if proxy:
|
| 16 |
-
return {"http": proxy, "https": proxy}
|
| 17 |
-
return None
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def load_module(module_name: str, file_name: str):
|
| 21 |
-
"""
|
| 22 |
-
动态加载指定模块
|
| 23 |
-
|
| 24 |
-
Args:
|
| 25 |
-
module_name: 模块名称
|
| 26 |
-
file_name: 文件名(相对于当前目录)
|
| 27 |
-
|
| 28 |
-
Returns:
|
| 29 |
-
加载的模块对象
|
| 30 |
-
"""
|
| 31 |
-
base_dir = Path(__file__).resolve().parent
|
| 32 |
-
spec = importlib.util.spec_from_file_location(module_name, str(base_dir / file_name))
|
| 33 |
-
module = importlib.util.module_from_spec(spec)
|
| 34 |
-
spec.loader.exec_module(module)
|
| 35 |
-
return module
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
def create_proxy_mounts() -> Optional[Dict[str, httpx.AsyncHTTPTransport]]:
|
| 39 |
-
"""
|
| 40 |
-
创建代理传输层配置
|
| 41 |
-
|
| 42 |
-
Returns:
|
| 43 |
-
代理挂载配置字典,如果没有配置代理则返回 None
|
| 44 |
-
"""
|
| 45 |
-
proxies = get_proxies()
|
| 46 |
-
if proxies:
|
| 47 |
-
proxy_url = proxies.get("https") or proxies.get("http")
|
| 48 |
-
if proxy_url:
|
| 49 |
-
return {
|
| 50 |
-
"https://": httpx.AsyncHTTPTransport(proxy=proxy_url),
|
| 51 |
-
"http://": httpx.AsyncHTTPTransport(proxy=proxy_url),
|
| 52 |
-
}
|
| 53 |
-
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|