Add source code files
This commit is contained in:
54
src/codeguard/utils/config.py
Normal file
54
src/codeguard/utils/config.py
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
"""Configuration loader for CodeGuard."""
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Optional
|
||||||
|
from codeguard.core.models import Config, Language, Severity
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
|
||||||
|
class ConfigLoader:
|
||||||
|
@staticmethod
|
||||||
|
def load(config_path: str = "codeguard.yaml") -> Config:
|
||||||
|
path = Path(config_path)
|
||||||
|
|
||||||
|
if not path.exists():
|
||||||
|
return Config()
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(path, "r") as f:
|
||||||
|
data = yaml.safe_load(f) or {}
|
||||||
|
|
||||||
|
languages = []
|
||||||
|
for lang in data.get("languages", []):
|
||||||
|
detected = LanguageDetector.get_language_by_name(lang)
|
||||||
|
if detected:
|
||||||
|
languages.append(detected)
|
||||||
|
|
||||||
|
severity = Severity.INFO
|
||||||
|
if "severity_threshold" in data:
|
||||||
|
try:
|
||||||
|
severity = Severity(data["severity_threshold"])
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return Config(
|
||||||
|
model=data.get("model", "codellama"),
|
||||||
|
languages=languages or [Language.PYTHON],
|
||||||
|
severity_threshold=severity,
|
||||||
|
fail_on_critical=data.get("fail_on_critical", False),
|
||||||
|
max_file_size=data.get("max_file_size", 100000),
|
||||||
|
chunk_size=data.get("chunk_size", 8000),
|
||||||
|
custom_rules=data.get("custom_rules", []),
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
return Config()
|
||||||
|
|
||||||
|
|
||||||
|
class LanguageDetector:
|
||||||
|
@staticmethod
|
||||||
|
def get_language_by_name(name: str) -> Optional[Language]:
|
||||||
|
name = name.lower()
|
||||||
|
for lang in Language:
|
||||||
|
if lang.value == name:
|
||||||
|
return lang
|
||||||
|
return None
|
||||||
Reference in New Issue
Block a user