准备创建mysqlv1分支的提交
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import Tuple
|
||||
from typing import Tuple, Optional
|
||||
|
||||
import yaml
|
||||
|
||||
@@ -13,8 +13,25 @@ class GlobalConfig:
|
||||
|
||||
@dataclass
|
||||
class DatabaseConfig:
|
||||
path: str
|
||||
pool_size: int
|
||||
# SQLite的配置字段保留,用于兼容
|
||||
path: Optional[str] = None
|
||||
pool_size: int = 10
|
||||
# MySQL配置
|
||||
host: str = "localhost"
|
||||
port: int = 3306
|
||||
username: str = "auto_cursor_reg"
|
||||
password: str = "this_password_jiaqiao"
|
||||
database: str = "auto_cursor_reg"
|
||||
# Redis配置
|
||||
use_redis: bool = False
|
||||
|
||||
|
||||
@dataclass
|
||||
class RedisConfig:
|
||||
host: str
|
||||
port: int
|
||||
password: str = ""
|
||||
db: int = 0
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -61,29 +78,47 @@ class CaptchaConfig:
|
||||
class Config:
|
||||
global_config: GlobalConfig
|
||||
database_config: DatabaseConfig
|
||||
proxy_config: ProxyConfig
|
||||
register_config: RegisterConfig
|
||||
email_config: EmailConfig
|
||||
captcha_config: CaptchaConfig
|
||||
redis_config: Optional[RedisConfig] = None
|
||||
proxy_config: ProxyConfig = None
|
||||
register_config: RegisterConfig = None
|
||||
email_config: EmailConfig = None
|
||||
captcha_config: CaptchaConfig = None
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, path: str = "config.yaml"):
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
data = yaml.safe_load(f)
|
||||
|
||||
# 创建 database 配置对象
|
||||
db_config = DatabaseConfig(**data.get('database', {}))
|
||||
|
||||
# 创建 redis 配置对象(如果有)
|
||||
redis_config = None
|
||||
if 'redis' in data and db_config.use_redis:
|
||||
redis_config = RedisConfig(**data['redis'])
|
||||
|
||||
# 创建 captcha 配置对象
|
||||
captcha_data = data['captcha']
|
||||
captcha_config = CaptchaConfig(
|
||||
provider=captcha_data['provider'],
|
||||
capsolver=CapsolverConfig(**captcha_data['capsolver']),
|
||||
yescaptcha=YesCaptchaConfig(**captcha_data['yescaptcha'])
|
||||
)
|
||||
captcha_data = data.get('captcha', {})
|
||||
captcha_config = None
|
||||
if captcha_data:
|
||||
captcha_config = CaptchaConfig(
|
||||
provider=captcha_data.get('provider', 'capsolver'),
|
||||
capsolver=CapsolverConfig(**captcha_data.get('capsolver', {})),
|
||||
yescaptcha=YesCaptchaConfig(**captcha_data.get('yescaptcha', {}))
|
||||
)
|
||||
|
||||
# 创建其他配置对象
|
||||
global_config = GlobalConfig(**data.get('global', {}))
|
||||
proxy_config = ProxyConfig(**data.get('proxy', {})) if 'proxy' in data else None
|
||||
register_config = RegisterConfig(**data.get('register', {})) if 'register' in data else None
|
||||
email_config = EmailConfig(**data.get('email', {})) if 'email' in data else None
|
||||
|
||||
return cls(
|
||||
global_config=GlobalConfig(**data['global']),
|
||||
database_config=DatabaseConfig(**data['database']),
|
||||
proxy_config=ProxyConfig(**data['proxy']),
|
||||
register_config=RegisterConfig(**data['register']),
|
||||
email_config=EmailConfig(**data['email']),
|
||||
global_config=global_config,
|
||||
database_config=db_config,
|
||||
redis_config=redis_config,
|
||||
proxy_config=proxy_config,
|
||||
register_config=register_config,
|
||||
email_config=email_config,
|
||||
captcha_config=captcha_config
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user