279 lines
7.0 KiB
Python
279 lines
7.0 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""
|
||
|
|
配置管理使用示例
|
||
|
|
|
||
|
|
演示如何使用配置管理模块进行各种操作
|
||
|
|
"""
|
||
|
|
|
||
|
|
import sys
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
# 添加项目路径
|
||
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
||
|
|
|
||
|
|
from src.config.settings import (
|
||
|
|
get_config,
|
||
|
|
get_settings,
|
||
|
|
SettingsManager,
|
||
|
|
AIProvider,
|
||
|
|
OCRMode,
|
||
|
|
CloudStorageType,
|
||
|
|
Theme,
|
||
|
|
ConfigError
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def example_1_basic_usage():
|
||
|
|
"""示例 1: 基本使用"""
|
||
|
|
print("=" * 60)
|
||
|
|
print("示例 1: 基本使用")
|
||
|
|
print("=" * 60)
|
||
|
|
|
||
|
|
# 获取配置对象
|
||
|
|
settings = get_settings()
|
||
|
|
|
||
|
|
print(f"AI 提供商: {settings.ai.provider}")
|
||
|
|
print(f"AI 模型: {settings.ai.model}")
|
||
|
|
print(f"OCR 模式: {settings.ocr.mode}")
|
||
|
|
print(f"界面主题: {settings.ui.theme}")
|
||
|
|
print(f"快捷键 - 截图: {settings.ui.hotkeys.screenshot}")
|
||
|
|
print()
|
||
|
|
|
||
|
|
|
||
|
|
def example_2_modify_config():
|
||
|
|
"""示例 2: 修改配置"""
|
||
|
|
print("=" * 60)
|
||
|
|
print("示例 2: 修改配置")
|
||
|
|
print("=" * 60)
|
||
|
|
|
||
|
|
settings = get_settings()
|
||
|
|
|
||
|
|
# 修改 AI 配置
|
||
|
|
settings.ai.provider = AIProvider.OPENAI
|
||
|
|
settings.ai.model = "gpt-4"
|
||
|
|
settings.ai.temperature = 0.8
|
||
|
|
|
||
|
|
# 修改界面配置
|
||
|
|
settings.ui.theme = Theme.DARK
|
||
|
|
settings.ui.window_width = 1400
|
||
|
|
|
||
|
|
print("配置已修改:")
|
||
|
|
print(f" AI 提供商: {settings.ai.provider}")
|
||
|
|
print(f" AI 模型: {settings.ai.model}")
|
||
|
|
print(f" 温度: {settings.ai.temperature}")
|
||
|
|
print(f" 主题: {settings.ui.theme}")
|
||
|
|
|
||
|
|
# 注意: 实际使用时需要调用 manager.save() 来保存
|
||
|
|
# manager.save()
|
||
|
|
|
||
|
|
print()
|
||
|
|
|
||
|
|
|
||
|
|
def example_3_manager_operations():
|
||
|
|
"""示例 3: 配置管理器操作"""
|
||
|
|
print("=" * 60)
|
||
|
|
print("示例 3: 配置管理器操作")
|
||
|
|
print("=" * 60)
|
||
|
|
|
||
|
|
manager = get_config()
|
||
|
|
|
||
|
|
# 使用 get 获取嵌套值
|
||
|
|
provider = manager.get('ai.provider')
|
||
|
|
theme = manager.get('ui.theme')
|
||
|
|
screenshot_hotkey = manager.get('ui.hotkeys.screenshot')
|
||
|
|
|
||
|
|
print("使用 manager.get() 获取配置:")
|
||
|
|
print(f" AI 提供商: {provider}")
|
||
|
|
print(f" 主题: {theme}")
|
||
|
|
print(f" 截图快捷键: {screenshot_hotkey}")
|
||
|
|
|
||
|
|
# 使用 set 设置嵌套值
|
||
|
|
manager.set('ai.temperature', 1.2)
|
||
|
|
manager.set('ui.language', 'en_US')
|
||
|
|
|
||
|
|
print("\n使用 manager.set() 修改配置:")
|
||
|
|
print(f" 温度: {manager.settings.ai.temperature}")
|
||
|
|
print(f" 语言: {manager.settings.ui.language}")
|
||
|
|
|
||
|
|
print()
|
||
|
|
|
||
|
|
|
||
|
|
def example_4_validation():
|
||
|
|
"""示例 4: 配置验证"""
|
||
|
|
print("=" * 60)
|
||
|
|
print("示例 4: 配置验证")
|
||
|
|
print("=" * 60)
|
||
|
|
|
||
|
|
settings = get_settings()
|
||
|
|
|
||
|
|
# 验证当前配置
|
||
|
|
try:
|
||
|
|
settings.validate()
|
||
|
|
print("✓ 当前配置有效")
|
||
|
|
except ConfigError as e:
|
||
|
|
print(f"✗ 配置错误: {e}")
|
||
|
|
|
||
|
|
# 尝试创建无效配置
|
||
|
|
print("\n尝试创建无效配置:")
|
||
|
|
settings.ai.provider = AIProvider.OPENAI
|
||
|
|
settings.ai.api_key = "" # 缺少 API key
|
||
|
|
|
||
|
|
try:
|
||
|
|
settings.validate()
|
||
|
|
print("✗ 应该抛出异常")
|
||
|
|
except ConfigError as e:
|
||
|
|
print(f"✓ 正确捕获错误: {e}")
|
||
|
|
|
||
|
|
print()
|
||
|
|
|
||
|
|
|
||
|
|
def example_5_custom_config_file():
|
||
|
|
"""示例 5: 使用自定义配置文件"""
|
||
|
|
print("=" * 60)
|
||
|
|
print("示例 5: 使用自定义配置文件")
|
||
|
|
print("=" * 60)
|
||
|
|
|
||
|
|
import tempfile
|
||
|
|
import shutil
|
||
|
|
|
||
|
|
# 创建临时目录
|
||
|
|
temp_dir = Path(tempfile.mkdtemp())
|
||
|
|
custom_config = temp_dir / 'custom_config.yaml'
|
||
|
|
|
||
|
|
try:
|
||
|
|
# 创建自定义配置管理器
|
||
|
|
manager = SettingsManager(custom_config)
|
||
|
|
|
||
|
|
# 加载配置(会创建默认配置)
|
||
|
|
settings = manager.load()
|
||
|
|
print(f"✓ 配置文件已创建: {custom_config}")
|
||
|
|
|
||
|
|
# 修改配置
|
||
|
|
settings.ai.provider = AIProvider.ANTHROPIC
|
||
|
|
settings.ai.api_key = "sk-ant-test123"
|
||
|
|
settings.ui.theme = Theme.LIGHT
|
||
|
|
|
||
|
|
# 保存配置
|
||
|
|
manager.save()
|
||
|
|
print("✓ 配置已保存")
|
||
|
|
|
||
|
|
# 重新加载验证
|
||
|
|
manager2 = SettingsManager(custom_config)
|
||
|
|
loaded = manager2.load()
|
||
|
|
print(f"✓ 重新加载成功: provider={loaded.ai.provider}, theme={loaded.ui.theme}")
|
||
|
|
|
||
|
|
finally:
|
||
|
|
# 清理
|
||
|
|
shutil.rmtree(temp_dir)
|
||
|
|
|
||
|
|
print()
|
||
|
|
|
||
|
|
|
||
|
|
def example_6_cloud_storage_config():
|
||
|
|
"""示例 6: 云存储配置"""
|
||
|
|
print("=" * 60)
|
||
|
|
print("示例 6: 云存储配置")
|
||
|
|
print("=" * 60)
|
||
|
|
|
||
|
|
settings = get_settings()
|
||
|
|
|
||
|
|
# 配置 S3 存储
|
||
|
|
settings.cloud_storage.type = CloudStorageType.S3
|
||
|
|
settings.cloud_storage.endpoint = "https://s3.amazonaws.com"
|
||
|
|
settings.cloud_storage.access_key = "your-access-key"
|
||
|
|
settings.cloud_storage.secret_key = "your-secret-key"
|
||
|
|
settings.cloud_storage.bucket = "my-bucket"
|
||
|
|
settings.cloud_storage.region = "us-east-1"
|
||
|
|
|
||
|
|
print("S3 存储配置:")
|
||
|
|
print(f" 类型: {settings.cloud_storage.type}")
|
||
|
|
print(f" 端点: {settings.cloud_storage.endpoint}")
|
||
|
|
print(f" 存储桶: {settings.cloud_storage.bucket}")
|
||
|
|
print(f" 区域: {settings.cloud_storage.region}")
|
||
|
|
|
||
|
|
# 验证配置
|
||
|
|
try:
|
||
|
|
settings.cloud_storage.validate()
|
||
|
|
print("✓ S3 配置有效")
|
||
|
|
except ConfigError as e:
|
||
|
|
print(f"✗ S3 配置错误: {e}")
|
||
|
|
|
||
|
|
print()
|
||
|
|
|
||
|
|
|
||
|
|
def example_7_ocr_config():
|
||
|
|
"""示例 7: OCR 配置"""
|
||
|
|
print("=" * 60)
|
||
|
|
print("示例 7: OCR 配置")
|
||
|
|
print("=" * 60)
|
||
|
|
|
||
|
|
settings = get_settings()
|
||
|
|
|
||
|
|
# 本地 OCR 配置
|
||
|
|
settings.ocr.mode = OCRMode.LOCAL
|
||
|
|
settings.ocr.use_gpu = False
|
||
|
|
settings.ocr.lang = "ch" # 中文
|
||
|
|
|
||
|
|
print("本地 OCR 配置:")
|
||
|
|
print(f" 模式: {settings.ocr.mode}")
|
||
|
|
print(f" 使用 GPU: {settings.ocr.use_gpu}")
|
||
|
|
print(f" 语言: {settings.ocr.lang}")
|
||
|
|
|
||
|
|
# 云端 OCR 配置
|
||
|
|
settings.ocr.mode = OCRMode.CLOUD
|
||
|
|
settings.ocr.api_endpoint = "https://api.example.com/ocr"
|
||
|
|
settings.ocr.api_key = "cloud-api-key"
|
||
|
|
|
||
|
|
print("\n云端 OCR 配置:")
|
||
|
|
print(f" 模式: {settings.ocr.mode}")
|
||
|
|
print(f" 端点: {settings.ocr.api_endpoint}")
|
||
|
|
|
||
|
|
print()
|
||
|
|
|
||
|
|
|
||
|
|
def example_8_advanced_config():
|
||
|
|
"""示例 8: 高级配置"""
|
||
|
|
print("=" * 60)
|
||
|
|
print("示例 8: 高级配置")
|
||
|
|
print("=" * 60)
|
||
|
|
|
||
|
|
settings = get_settings()
|
||
|
|
|
||
|
|
# 启用调试模式
|
||
|
|
settings.advanced.debug_mode = True
|
||
|
|
settings.advanced.log_level = "DEBUG"
|
||
|
|
settings.advanced.log_file = "/var/log/cutthenthink/app.log"
|
||
|
|
|
||
|
|
print("高级配置:")
|
||
|
|
print(f" 调试模式: {settings.advanced.debug_mode}")
|
||
|
|
print(f" 日志级别: {settings.advanced.log_level}")
|
||
|
|
print(f" 日志文件: {settings.advanced.log_file}")
|
||
|
|
print(f" 最大日志大小: {settings.advanced.max_log_size} MB")
|
||
|
|
print(f" 备份文件数: {settings.advanced.backup_count}")
|
||
|
|
|
||
|
|
print()
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
"""运行所有示例"""
|
||
|
|
print("\n" + "=" * 60)
|
||
|
|
print("配置管理模块使用示例")
|
||
|
|
print("=" * 60 + "\n")
|
||
|
|
|
||
|
|
example_1_basic_usage()
|
||
|
|
example_2_modify_config()
|
||
|
|
example_3_manager_operations()
|
||
|
|
example_4_validation()
|
||
|
|
example_5_custom_config_file()
|
||
|
|
example_6_cloud_storage_config()
|
||
|
|
example_7_ocr_config()
|
||
|
|
example_8_advanced_config()
|
||
|
|
|
||
|
|
print("=" * 60)
|
||
|
|
print("所有示例运行完成!")
|
||
|
|
print("=" * 60)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
main()
|