Initial upload with CI/CD workflow
This commit is contained in:
115
codesnap/utils/config.py
Normal file
115
codesnap/utils/config.py
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
"""Configuration module for CodeSnap."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class OutputConfig:
|
||||||
|
"""Output configuration settings."""
|
||||||
|
|
||||||
|
show_content: bool = False
|
||||||
|
max_function_lines: int = 50
|
||||||
|
include_metrics: bool = True
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ComplexityConfig:
|
||||||
|
"""Complexity configuration settings."""
|
||||||
|
|
||||||
|
low: int = 10
|
||||||
|
medium: int = 20
|
||||||
|
high: int = 50
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class LoggingConfig:
|
||||||
|
"""Logging configuration settings."""
|
||||||
|
|
||||||
|
level: str = "INFO"
|
||||||
|
format: str = "json"
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Config:
|
||||||
|
"""Main configuration class for CodeSnap."""
|
||||||
|
|
||||||
|
max_files: int = 1000
|
||||||
|
max_tokens: int = 8000
|
||||||
|
grammar_cache: str = "~/.cache/codesnap/grammars"
|
||||||
|
include_patterns: list[str] = field(default_factory=lambda: [
|
||||||
|
"**/*.py",
|
||||||
|
"**/*.js",
|
||||||
|
"**/*.ts",
|
||||||
|
"**/*.go",
|
||||||
|
"**/*.rs",
|
||||||
|
"**/*.java",
|
||||||
|
])
|
||||||
|
exclude_patterns: list[str] = field(default_factory=lambda: [
|
||||||
|
"**/node_modules/**",
|
||||||
|
"**/.git/**",
|
||||||
|
"**/venv/**",
|
||||||
|
"**/__pycache__/**",
|
||||||
|
])
|
||||||
|
output: OutputConfig = field(default_factory=OutputConfig)
|
||||||
|
complexity: ComplexityConfig = field(default_factory=ComplexityConfig)
|
||||||
|
logging: LoggingConfig = field(default_factory=LoggingConfig)
|
||||||
|
|
||||||
|
|
||||||
|
def load_config(config_path: Optional[Path] = None) -> Config:
|
||||||
|
"""Load configuration from file or use defaults."""
|
||||||
|
config = Config()
|
||||||
|
|
||||||
|
if config_path is None:
|
||||||
|
config_path = Path.cwd() / ".codesnaprc"
|
||||||
|
if not config_path.exists():
|
||||||
|
return config
|
||||||
|
|
||||||
|
if not config_path.exists():
|
||||||
|
return config
|
||||||
|
|
||||||
|
try:
|
||||||
|
import yaml
|
||||||
|
content = config_path.read_text(encoding="utf-8")
|
||||||
|
data = yaml.safe_load(content) or {}
|
||||||
|
|
||||||
|
if "max_files" in data:
|
||||||
|
config.max_files = int(data["max_files"])
|
||||||
|
if "max_tokens" in data:
|
||||||
|
config.max_tokens = int(data["max_tokens"])
|
||||||
|
if "grammar_cache" in data:
|
||||||
|
config.grammar_cache = str(data["grammar_cache"])
|
||||||
|
if "include_patterns" in data:
|
||||||
|
config.include_patterns = data["include_patterns"]
|
||||||
|
if "exclude_patterns" in data:
|
||||||
|
config.exclude_patterns = data["exclude_patterns"]
|
||||||
|
|
||||||
|
if "output" in data:
|
||||||
|
output_data = data["output"]
|
||||||
|
config.output.show_content = output_data.get("show_content", False)
|
||||||
|
config.output.max_function_lines = output_data.get("max_function_lines", 50)
|
||||||
|
config.output.include_metrics = output_data.get("include_metrics", True)
|
||||||
|
|
||||||
|
if "complexity" in data:
|
||||||
|
complex_data = data["complexity"]
|
||||||
|
config.complexity.low = complex_data.get("low", 10)
|
||||||
|
config.complexity.medium = complex_data.get("medium", 20)
|
||||||
|
config.complexity.high = complex_data.get("high", 50)
|
||||||
|
|
||||||
|
if "logging" in data:
|
||||||
|
log_data = data["logging"]
|
||||||
|
config.logging.level = log_data.get("level", "INFO")
|
||||||
|
config.logging.format = log_data.get("format", "json")
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
def get_env_override(key: str, default: str) -> str:
|
||||||
|
"""Get environment variable override."""
|
||||||
|
env_key = f"CODSNAP_{key.upper()}"
|
||||||
|
return os.environ.get(env_key, default)
|
||||||
Reference in New Issue
Block a user