|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from dataclasses import dataclass, field |
| 5 | +from pathlib import Path |
| 6 | +from typing import Any |
| 7 | +import json |
| 8 | +import yaml |
| 9 | + |
| 10 | +DEFAULT_EXCLUDE = [ |
| 11 | + "**/__pycache__/**", |
| 12 | + "**/*.pyc", |
| 13 | + ".git/**", |
| 14 | + "venv/**", |
| 15 | + ".venv/**", |
| 16 | +] |
| 17 | + |
| 18 | +DEFAULT_INCLUDE = ["**/*.py"] |
| 19 | + |
| 20 | + |
| 21 | +@dataclass |
| 22 | +class BCASLConfig: |
| 23 | + file_patterns: list[str] = field(default_factory=lambda: list(DEFAULT_INCLUDE)) |
| 24 | + exclude_patterns: list[str] = field(default_factory=lambda: list(DEFAULT_EXCLUDE)) |
| 25 | + options: dict[str, Any] = field( |
| 26 | + default_factory=lambda: { |
| 27 | + "plugin_timeout_s": 0.0, |
| 28 | + "sandbox": True, |
| 29 | + "plugin_parallelism": 0, |
| 30 | + "iter_files_cache": True, |
| 31 | + "env": {}, |
| 32 | + } |
| 33 | + ) |
| 34 | + plugins: dict[str, Any] = field(default_factory=dict) |
| 35 | + plugin_order: list[str] = field(default_factory=list) |
| 36 | + |
| 37 | + def to_dict(self) -> dict[str, Any]: |
| 38 | + d = { |
| 39 | + "file_patterns": list(self.file_patterns), |
| 40 | + "exclude_patterns": list(self.exclude_patterns), |
| 41 | + "options": dict(self.options), |
| 42 | + "plugins": dict(self.plugins), |
| 43 | + "plugin_order": list(self.plugin_order), |
| 44 | + } |
| 45 | + # Never persist global 'enabled' flag; only per-plugin enabled |
| 46 | + try: |
| 47 | + d["options"].pop("enabled", None) |
| 48 | + except Exception: |
| 49 | + pass |
| 50 | + return d |
| 51 | + |
| 52 | + |
| 53 | +def read_yaml(path: Path) -> dict[str, Any]: |
| 54 | + try: |
| 55 | + return yaml.safe_load(path.read_text(encoding="utf-8")) or {} |
| 56 | + except Exception: |
| 57 | + return {} |
| 58 | + |
| 59 | + |
| 60 | +def write_yaml(path: Path, data: dict[str, Any]) -> None: |
| 61 | + path.write_text(yaml.safe_dump(data, sort_keys=False, allow_unicode=True), encoding="utf-8") |
| 62 | + |
| 63 | + |
| 64 | +def read_json(path: Path) -> dict[str, Any]: |
| 65 | + try: |
| 66 | + return json.loads(path.read_text(encoding="utf-8")) |
| 67 | + except Exception: |
| 68 | + return {} |
| 69 | + |
| 70 | + |
| 71 | +def load_config(workspace: Path) -> BCASLConfig: |
| 72 | + """Load bcasl.yml if present; migrate legacy JSON to YAML; otherwise return defaults.""" |
| 73 | + yml = workspace / "bcasl.yml" |
| 74 | + legacy = [workspace / "bcasl.json", workspace / ".bcasl.json"] |
| 75 | + data: dict[str, Any] = {} |
| 76 | + if yml.is_file(): |
| 77 | + data = read_yaml(yml) |
| 78 | + else: |
| 79 | + # migrate JSON if exists |
| 80 | + for p in legacy: |
| 81 | + if p.is_file(): |
| 82 | + data = read_json(p) |
| 83 | + break |
| 84 | + if data: |
| 85 | + # persist migration |
| 86 | + try: |
| 87 | + write_yaml(yml, data) |
| 88 | + except Exception: |
| 89 | + pass |
| 90 | + cfg = BCASLConfig() |
| 91 | + if isinstance(data, dict) and data: |
| 92 | + cfg.file_patterns = list(data.get("file_patterns", cfg.file_patterns)) |
| 93 | + cfg.exclude_patterns = list(data.get("exclude_patterns", cfg.exclude_patterns)) |
| 94 | + opts = dict(data.get("options", {})) if isinstance(data.get("options"), dict) else {} |
| 95 | + # ensure no persisted global enabled |
| 96 | + opts.pop("enabled", None) |
| 97 | + cfg.options.update(opts) |
| 98 | + cfg.plugins = dict(data.get("plugins", {})) |
| 99 | + cfg.plugin_order = list(data.get("plugin_order", [])) |
| 100 | + return cfg |
| 101 | + |
| 102 | + |
| 103 | +def save_config(workspace: Path, cfg: BCASLConfig) -> None: |
| 104 | + yml = workspace / "bcasl.yml" |
| 105 | + write_yaml(yml, cfg.to_dict()) |
0 commit comments