43 lines
1.4 KiB
Python
43 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
from bedit_core.models import Document
|
|
from bedit_gui.services import document_files
|
|
from bedit_gui.services.application_logging import get_logger
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class LoadedLibrary:
|
|
path: Path
|
|
document: Document
|
|
|
|
|
|
def list_library_files(library_paths: list[str]) -> list[Path]:
|
|
files = []
|
|
seen = set()
|
|
for configured_path in library_paths:
|
|
path = Path(configured_path).expanduser()
|
|
candidates = [path] if path.is_file() else sorted(path.rglob("*"), key=lambda candidate: str(candidate).casefold()) if path.is_dir() else []
|
|
for candidate in candidates:
|
|
if not candidate.is_file() or candidate.suffix.lower() not in (".beb", ".json"):
|
|
continue
|
|
resolved = candidate.resolve()
|
|
if resolved not in seen:
|
|
seen.add(resolved)
|
|
files.append(resolved)
|
|
return files
|
|
|
|
|
|
def load_library_documents(library_paths: list[str]) -> list[LoadedLibrary]:
|
|
libraries = []
|
|
for path in list_library_files(library_paths):
|
|
try:
|
|
libraries.append(LoadedLibrary(path, document_files.load(path)))
|
|
except (KeyError, OSError, TypeError, ValueError) as exc:
|
|
logger.warning("Could not load library %s: %s", path, exc)
|
|
return libraries
|