From d87acfbf7b4f69a8adfe52dcd6217400453fbfbb Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Thu, 29 Jan 2026 22:28:12 +0000 Subject: [PATCH] Initial upload: TermDiagram v0.1.0 --- src/termdiagram/utils/config.py | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/termdiagram/utils/config.py diff --git a/src/termdiagram/utils/config.py b/src/termdiagram/utils/config.py new file mode 100644 index 0000000..a0ad2b9 --- /dev/null +++ b/src/termdiagram/utils/config.py @@ -0,0 +1,49 @@ +from typing import Dict, Any, Optional +from pathlib import Path +import json +import yaml + + +class Config: + def __init__(self, config_path: Optional[str] = None): + self.config: Dict[str, Any] = { + "output_format": "ascii", + "extensions": [], + "exclude": [], + "max_methods": 5, + "show_imports": True, + } + + if config_path: + self.load(config_path) + + def load(self, config_path: str): + path = Path(config_path) + + if path.suffix == ".json": + self._load_json(path) + elif path.suffix in (".yaml", ".yml"): + self._load_yaml(path) + + def _load_json(self, path: Path): + try: + with open(path, "r") as f: + data = json.load(f) + self.config.update(data) + except (IOError, json.JSONDecodeError): + pass + + def _load_yaml(self, path: Path): + try: + with open(path, "r") as f: + data = yaml.safe_load(f) + if data: + self.config.update(data) + except (IOError, yaml.YAMLError): + pass + + def get(self, key: str, default: Any = None) -> Any: + return self.config.get(key, default) + + def set(self, key: str, value: Any): + self.config[key] = value