33 lines
848 B
Python
33 lines
848 B
Python
import json
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
from bedit.core.model import GraphDocument
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class LibraryDocument:
|
|
name: str
|
|
document: GraphDocument
|
|
source_path: str
|
|
|
|
|
|
def bundled_library_path() -> Path:
|
|
return Path(__file__).resolve().parents[1] / "data" / "libraries" / "default.json"
|
|
|
|
|
|
def default_library_paths() -> list[str]:
|
|
return [str(bundled_library_path())]
|
|
|
|
|
|
def load_library_file(path: Path) -> LibraryDocument:
|
|
with path.open(encoding="utf-8") as file:
|
|
data = json.load(file)
|
|
document = GraphDocument.from_dict(data)
|
|
name = str(document.metadata.get("name") or path.stem)
|
|
return LibraryDocument(name, document, str(path))
|
|
|
|
|
|
def library_candidates(path: Path) -> list[Path]:
|
|
return sorted(path.glob("*.json")) if path.is_dir() else [path]
|