feat(shared): add config and filesystem utilities
This commit is contained in:
parent
2edbd306c8
commit
e76f0fc649
|
|
@ -0,0 +1,9 @@
|
|||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@dataclass
|
||||
class Settings:
|
||||
registry_path: Path = field(
|
||||
default_factory=lambda: Path.home() / ".arch-design-dashboard" / "projects.json"
|
||||
)
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
from pathlib import Path
|
||||
|
||||
from app.shared.kernel.exceptions import FileSystemError
|
||||
|
||||
|
||||
def read_text(path: Path) -> str:
|
||||
try:
|
||||
return path.read_text(encoding="utf-8")
|
||||
except OSError as e:
|
||||
raise FileSystemError(str(path), str(e)) from e
|
||||
|
||||
|
||||
def write_text(path: Path, content: str) -> None:
|
||||
try:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(content, encoding="utf-8")
|
||||
except OSError as e:
|
||||
raise FileSystemError(str(path), str(e)) from e
|
||||
|
||||
|
||||
def list_files(directory: Path, extensions: list[str] | None = None) -> list[Path]:
|
||||
if not directory.is_dir():
|
||||
raise FileSystemError(str(directory), "Not a directory")
|
||||
files: list[Path] = []
|
||||
for p in sorted(directory.rglob("*")):
|
||||
if p.is_file():
|
||||
if extensions is None or p.suffix in extensions:
|
||||
files.append(p)
|
||||
return files
|
||||
|
||||
|
||||
def file_exists(path: Path) -> bool:
|
||||
return path.is_file()
|
||||
Loading…
Reference in New Issue
Block a user