|
1 | 1 | """Parser for a TOML spec file containing definitions of all supported reStructuredText |
2 | | - directives and roles, and what types of data each should expect.""" |
| 2 | +directives and roles, and what types of data each should expect.""" |
3 | 3 |
|
4 | 4 | from __future__ import annotations |
5 | 5 |
|
6 | 6 | import dataclasses |
| 7 | +import logging |
7 | 8 | from dataclasses import dataclass, field |
8 | 9 | from datetime import datetime |
9 | 10 | from enum import Enum |
| 11 | +from pathlib import Path |
10 | 12 | from typing import ( |
11 | 13 | Any, |
12 | 14 | Callable, |
|
18 | 20 | Optional, |
19 | 21 | Sequence, |
20 | 22 | Set, |
| 23 | + Tuple, |
21 | 24 | TypeVar, |
22 | 25 | Union, |
23 | 26 | ) |
24 | 27 |
|
25 | 28 | import tomli |
26 | 29 | from typing_extensions import Protocol |
27 | 30 |
|
| 31 | +from snooty.diagnostics import Diagnostic, UnknownOptionId |
| 32 | + |
28 | 33 | from . import tinydocutils, util |
29 | 34 | from .flutter import check_type, checked |
30 | 35 |
|
|
70 | 75 | PrimitiveType.linenos: util.option_string, |
71 | 76 | } |
72 | 77 |
|
| 78 | +logger = logging.getLogger(__name__) |
| 79 | + |
73 | 80 | #: Option types can be a primitive type (PrimitiveType), an enum |
74 | 81 | #: defined in the spec, or a union of those. |
75 | 82 | ArgumentType = Union[List[Union[PrimitiveType, str]], PrimitiveType, str, None] |
@@ -308,6 +315,7 @@ class Spec: |
308 | 315 | wayfinding: Dict[str, List[WayfindingOption]] = field(default_factory=dict) |
309 | 316 | data_fields: List[str] = field(default_factory=list) |
310 | 317 | composables: List[Composable] = field(default_factory=list) |
| 318 | + merged: bool = False |
311 | 319 |
|
312 | 320 | SPEC: ClassVar[Optional[Spec]] = None |
313 | 321 |
|
@@ -439,15 +447,131 @@ def resolve_value(key: str, inheritable: _T) -> _T: |
439 | 447 | resolve_value(key, inheritable) |
440 | 448 |
|
441 | 449 | @classmethod |
442 | | - def initialize(cls, text: str) -> None: |
| 450 | + def _merge_composables( |
| 451 | + cls, spec: Spec, custom_composables: List[Dict[str, Any]] |
| 452 | + ) -> Tuple[Spec, List[Diagnostic]]: |
| 453 | + res: List[Composable] = [] |
| 454 | + diagnostics: List[Diagnostic] = [] |
| 455 | + |
| 456 | + custom_composable_by_id = { |
| 457 | + composable["id"]: composable for composable in custom_composables |
| 458 | + } |
| 459 | + |
| 460 | + for defined_composable in spec.composables: |
| 461 | + custom_composable = custom_composable_by_id.pop(defined_composable.id, None) |
| 462 | + if not custom_composable: |
| 463 | + res.append(defined_composable) |
| 464 | + continue |
| 465 | + merged_title = custom_composable["title"] |
| 466 | + |
| 467 | + # merge all the options |
| 468 | + defined_options = { |
| 469 | + option.id: option for option in defined_composable.options |
| 470 | + } |
| 471 | + custom_options = { |
| 472 | + option["id"]: option for option in custom_composable["options"] |
| 473 | + } |
| 474 | + |
| 475 | + merged_options = [] |
| 476 | + for option_id in set(defined_options.keys()) | set(custom_options.keys()): |
| 477 | + if option_id in custom_options: |
| 478 | + custom_option = custom_options[option_id] |
| 479 | + merged_options.append( |
| 480 | + TabDefinition(custom_option["id"], custom_option["title"]) |
| 481 | + ) |
| 482 | + else: |
| 483 | + merged_options.append(defined_options[option_id]) |
| 484 | + |
| 485 | + merged_dependencies = ( |
| 486 | + custom_composable["dependencies"] |
| 487 | + if "dependencies" in custom_composable |
| 488 | + else defined_composable.dependencies |
| 489 | + ) |
| 490 | + |
| 491 | + merged_default = ( |
| 492 | + custom_composable["default"] |
| 493 | + if "default" in custom_composable |
| 494 | + else defined_composable.default |
| 495 | + ) |
| 496 | + default_option = next( |
| 497 | + ( |
| 498 | + option |
| 499 | + for option in merged_options |
| 500 | + if merged_default and option.id == merged_default |
| 501 | + ), |
| 502 | + None, |
| 503 | + ) |
| 504 | + if merged_default and not default_option: |
| 505 | + diagnostics.append( |
| 506 | + UnknownOptionId( |
| 507 | + "Spec composables default", |
| 508 | + merged_default, |
| 509 | + [option.title for option in merged_options], |
| 510 | + 0, |
| 511 | + ) |
| 512 | + ) |
| 513 | + res.append( |
| 514 | + Composable( |
| 515 | + defined_composable.id, |
| 516 | + merged_title, |
| 517 | + merged_default, |
| 518 | + merged_dependencies, |
| 519 | + merged_options, |
| 520 | + ) |
| 521 | + ) |
| 522 | + |
| 523 | + for composable_obj in custom_composable_by_id.values(): |
| 524 | + res.append( |
| 525 | + Composable( |
| 526 | + composable_obj["id"], |
| 527 | + composable_obj["title"], |
| 528 | + composable_obj["default"] if "default" in composable_obj else None, |
| 529 | + ( |
| 530 | + composable_obj["dependencies"] |
| 531 | + if "dependencies" in composable_obj |
| 532 | + else None |
| 533 | + ), |
| 534 | + list( |
| 535 | + map( |
| 536 | + lambda option: TabDefinition(option["id"], option["title"]), |
| 537 | + composable_obj["options"], |
| 538 | + ) |
| 539 | + ), |
| 540 | + ) |
| 541 | + ) |
| 542 | + |
| 543 | + spec.composables = res |
| 544 | + return (spec, diagnostics) |
| 545 | + |
| 546 | + @classmethod |
| 547 | + def initialize(cls, text: str, configPath: Optional[Path]) -> "Spec": |
443 | 548 | cls.SPEC = Spec.loads(text) |
| 549 | + if configPath: |
| 550 | + project_config = tomli.loads(configPath.read_text(encoding="utf-8")) |
| 551 | + # NOTE: would like to check_type but circular imports |
| 552 | + # this is already verified earlier in the process |
| 553 | + spec, diagnostics = cls._merge_composables( |
| 554 | + cls.SPEC, |
| 555 | + ( |
| 556 | + project_config["composables"] |
| 557 | + if "composables" in project_config |
| 558 | + else [] |
| 559 | + ), |
| 560 | + ) |
| 561 | + spec.merged = True |
| 562 | + cls.SPEC = spec |
| 563 | + for diagnostic in diagnostics: |
| 564 | + logger.error(diagnostic) |
| 565 | + return cls.SPEC |
444 | 566 |
|
445 | 567 | @classmethod |
446 | | - def get(cls) -> "Spec": |
447 | | - if cls.SPEC is None: |
448 | | - path = util.PACKAGE_ROOT.joinpath("rstspec.toml") |
449 | | - cls.initialize(path.read_text(encoding="utf-8")) |
450 | | - |
451 | | - spec = cls.SPEC |
452 | | - assert spec is not None |
453 | | - return spec |
| 568 | + def get(cls, configPath: Optional[Path] = None) -> "Spec": |
| 569 | + if cls.SPEC and cls.SPEC.merged: |
| 570 | + return cls.SPEC |
| 571 | + |
| 572 | + path = util.PACKAGE_ROOT.joinpath("rstspec.toml") |
| 573 | + spec = cls.initialize(path.read_text(encoding="utf-8"), configPath) |
| 574 | + |
| 575 | + cls.SPEC = spec |
| 576 | + assert cls.SPEC is not None |
| 577 | + return cls.SPEC |
0 commit comments