Files

82 lines
2.5 KiB
Python
Raw Permalink Normal View History

# -*- coding: utf-8 -*-
"""
应用配置中心 - 集中管理所有配置项
"""
import os
from dataclasses import dataclass, field
from typing import List, Optional
@dataclass
class AppConfig:
"""应用配置中心"""
# 分析配置
max_rounds: int = field(default=20)
force_max_rounds: bool = field(default=False)
default_output_dir: str = field(default="outputs")
# 数据处理配置
max_file_size_mb: int = field(default=500) # 最大文件大小MB
chunk_size: int = field(default=100000) # 分块读取大小
data_cache_enabled: bool = field(default=True)
cache_dir: str = field(default=".cache/data")
# LLM配置
llm_cache_enabled: bool = field(default=True)
llm_cache_dir: str = field(default=".cache/llm")
llm_stream_enabled: bool = field(default=False)
# 代码执行配置
code_timeout: int = field(default=300) # 代码执行超时(秒)
allowed_imports: List[str] = field(default_factory=lambda: [
'pandas', 'numpy', 'matplotlib', 'seaborn', 'plotly',
'scipy', 'sklearn', 'duckdb', 'datetime', 'json',
'os', 're', 'pathlib', 'glob', 'typing', 'collections',
'itertools', 'functools', 'warnings'
])
# Web配置
web_host: str = field(default="0.0.0.0")
web_port: int = field(default=8000)
upload_dir: str = field(default="uploads")
# 日志配置
log_filename: str = field(default="log.txt")
enable_code_logging: bool = field(default=False) # 是否记录生成的代码
@classmethod
def from_env(cls) -> 'AppConfig':
"""从环境变量创建配置"""
config = cls()
# 从环境变量覆盖配置
if max_rounds := os.getenv("APP_MAX_ROUNDS"):
config.max_rounds = int(max_rounds)
if chunk_size := os.getenv("APP_CHUNK_SIZE"):
config.chunk_size = int(chunk_size)
if cache_enabled := os.getenv("APP_CACHE_ENABLED"):
config.data_cache_enabled = cache_enabled.lower() == "true"
return config
def validate(self) -> bool:
"""验证配置"""
if self.max_rounds <= 0:
raise ValueError("max_rounds must be positive")
if self.chunk_size <= 0:
raise ValueError("chunk_size must be positive")
if self.code_timeout <= 0:
raise ValueError("code_timeout must be positive")
return True
# 全局配置实例
app_config = AppConfig.from_env()