|
28 | 28 | ) |
29 | 29 |
|
30 | 30 | if TYPE_CHECKING: |
| 31 | + from collections.abc import Callable |
31 | 32 | from pathlib import Path |
32 | 33 |
|
33 | 34 | __author__ = "Sergey Vartanov" |
@@ -339,6 +340,66 @@ def get_priority(self, tags: Tags) -> float: |
339 | 340 | return 1000.0 * layer + self.priority |
340 | 341 |
|
341 | 342 |
|
| 343 | +def _merge_contents( |
| 344 | + base: dict[str, Any], overlay: dict[str, Any] |
| 345 | +) -> dict[str, Any]: |
| 346 | + """Merge two scheme content dicts. |
| 347 | +
|
| 348 | + Lists are concatenated, dicts are merged (overlay wins on key |
| 349 | + conflicts), and scalar values are overridden by the overlay. |
| 350 | + """ |
| 351 | + merged: dict[str, Any] = {} |
| 352 | + for key in set(base) | set(overlay): |
| 353 | + if key not in overlay: |
| 354 | + merged[key] = base[key] |
| 355 | + elif key not in base: |
| 356 | + merged[key] = overlay[key] |
| 357 | + elif isinstance(base[key], list) and isinstance(overlay[key], list): |
| 358 | + merged[key] = base[key] + overlay[key] |
| 359 | + elif isinstance(base[key], dict) and isinstance(overlay[key], dict): |
| 360 | + merged[key] = {**base[key], **overlay[key]} |
| 361 | + else: |
| 362 | + merged[key] = overlay[key] |
| 363 | + return merged |
| 364 | + |
| 365 | + |
| 366 | +def _load_with_includes( |
| 367 | + file_name: Path, |
| 368 | + find_scheme_path: Callable[[str], Path | None] | None = None, |
| 369 | +) -> dict[str, Any]: |
| 370 | + """Load a scheme YAML file, recursively resolving `include`. |
| 371 | +
|
| 372 | + :param file_name: path to the YAML scheme file. |
| 373 | + :param find_scheme_path: callback that resolves a scheme identifier |
| 374 | + (e.g. `"default"`) to a file path. |
| 375 | + """ |
| 376 | + with file_name.open(encoding="utf-8") as input_file: |
| 377 | + content: dict[str, Any] = yaml.safe_load(input_file.read()) |
| 378 | + if not content: |
| 379 | + message: str = f"Scheme file {file_name} is empty." |
| 380 | + raise ValueError(message) |
| 381 | + |
| 382 | + includes: list[str] = content.pop("include", []) |
| 383 | + if isinstance(includes, str): |
| 384 | + includes = [includes] |
| 385 | + |
| 386 | + base: dict[str, Any] = {} |
| 387 | + for identifier in includes: |
| 388 | + path: Path | None = None |
| 389 | + if find_scheme_path is not None: |
| 390 | + path = find_scheme_path(identifier) |
| 391 | + if path is None: |
| 392 | + path = file_name.parent / f"{identifier}.yml" |
| 393 | + if not path.is_file(): |
| 394 | + message = f"Included scheme `{identifier}` not found." |
| 395 | + raise FileNotFoundError(message) |
| 396 | + base = _merge_contents( |
| 397 | + base, _load_with_includes(path, find_scheme_path) |
| 398 | + ) |
| 399 | + |
| 400 | + return _merge_contents(base, content) |
| 401 | + |
| 402 | + |
342 | 403 | def _yaml_str(value: bool | str | float) -> str: # noqa: FBT001 |
343 | 404 | """Convert a YAML value to string. |
344 | 405 |
|
@@ -420,18 +481,21 @@ def __init__(self, content: dict[str, Any]) -> None: |
420 | 481 | self.cache: dict[str, tuple[IconSet, int]] = {} |
421 | 482 |
|
422 | 483 | @classmethod |
423 | | - def from_file(cls, file_name: Path) -> Scheme: |
| 484 | + def from_file( |
| 485 | + cls, |
| 486 | + file_name: Path, |
| 487 | + find_scheme_path: Callable[[str], Path | None] | None = None, |
| 488 | + ) -> Scheme: |
424 | 489 | """Get scheme from file. |
425 | 490 |
|
426 | | - :param file_name: name of the scheme file with tags, colors, and tag key |
427 | | - specification |
| 491 | + :param file_name: name of the scheme file with tags, colors, and |
| 492 | + tag key specification |
| 493 | + :param find_scheme_path: optional callback that resolves a scheme |
| 494 | + identifier to a file path (used to resolve `include` |
| 495 | + entries) |
428 | 496 | """ |
429 | | - with file_name.open(encoding="utf-8") as input_file: |
430 | | - content: dict[str, Any] = yaml.safe_load(input_file.read()) |
431 | | - if not content: |
432 | | - message: str = "Scheme file is empty." |
433 | | - raise ValueError(message) |
434 | | - return cls(content) |
| 497 | + content = _load_with_includes(file_name, find_scheme_path) |
| 498 | + return cls(content) |
435 | 499 |
|
436 | 500 | def get_color(self, color_string: str) -> Color: |
437 | 501 | """Get any color. |
|
0 commit comments