From 0133e874c70cd2e2727b8f2150de33b78bd3f221 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Thu, 29 Jan 2026 13:53:53 +0000 Subject: [PATCH] Initial upload: API Mock CLI v0.1.0 --- src/utils/file.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/utils/file.py diff --git a/src/utils/file.py b/src/utils/file.py new file mode 100644 index 0000000..91725db --- /dev/null +++ b/src/utils/file.py @@ -0,0 +1,60 @@ +import json +import yaml +from pathlib import Path +from typing import Any, Dict, List + + +class FileUtils: + @staticmethod + def read_file(path: str) -> str: + with open(path, "r") as f: + return f.read() + + @staticmethod + def write_file(path: str, content: str) -> None: + Path(path).parent.mkdir(parents=True, exist_ok=True) + with open(path, "w") as f: + f.write(content) + + @staticmethod + def load_json(path: str) -> Dict[str, Any]: + with open(path, "r") as f: + return json.load(f) + + @staticmethod + def write_json(path: str, data: Any, indent: int = 2) -> None: + Path(path).parent.mkdir(parents=True, exist_ok=True) + with open(path, "w") as f: + json.dump(data, f, indent=indent) + + @staticmethod + def load_yaml(path: str) -> Dict[str, Any]: + with open(path, "r") as f: + return yaml.safe_load(f) or {} + + @staticmethod + def write_yaml(path: str, data: Any) -> None: + Path(path).parent.mkdir(parents=True, exist_ok=True) + with open(path, "w") as f: + yaml.dump(data, f, default_flow_style=False) + + @staticmethod + def ensure_dir(path: str) -> None: + Path(path).mkdir(parents=True, exist_ok=True) + + @staticmethod + def file_exists(path: str) -> bool: + return Path(path).exists() + + @staticmethod + def list_files(path: str, pattern: str = "*") -> List[str]: + return [str(p) for p in Path(path).glob(pattern)] + + @staticmethod + def read_yaml_or_json(path: str) -> Dict[str, Any]: + if path.endswith(".yaml") or path.endswith(".yml"): + return FileUtils.load_yaml(path) + elif path.endswith(".json"): + return FileUtils.load_json(path) + else: + raise ValueError(f"Unsupported file format: {path}")