Represents a CDDL module (e.g., script, network, browsing_context).
| 604 | |
| 605 | @dataclass |
| 606 | class CddlModule: |
| 607 | """Represents a CDDL module (e.g., script, network, browsing_context).""" |
| 608 | |
| 609 | name: str |
| 610 | commands: list[CddlCommand] = field(default_factory=list) |
| 611 | types: list[CddlTypeDefinition] = field(default_factory=list) |
| 612 | enums: list[CddlEnum] = field(default_factory=list) |
| 613 | events: list[CddlEvent] = field(default_factory=list) |
| 614 | |
| 615 | @staticmethod |
| 616 | def _convert_method_to_event_name(method_suffix: str) -> str: |
| 617 | """Convert BiDi method suffix to friendly event name. |
| 618 | |
| 619 | Examples: |
| 620 | "contextCreated" -> "context_created" |
| 621 | "navigationStarted" -> "navigation_started" |
| 622 | "userPromptOpened" -> "user_prompt_opened" |
| 623 | """ |
| 624 | # Convert camelCase to snake_case |
| 625 | s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", method_suffix) |
| 626 | return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower() |
| 627 | |
| 628 | def generate_code(self, enhancements: dict[str, Any] | None = None) -> str: |
| 629 | """Generate Python code for this module. |
| 630 | |
| 631 | Args: |
| 632 | enhancements: Dictionary with module-level enhancements |
| 633 | """ |
| 634 | enhancements = enhancements or {} |
| 635 | module_docstring = enhancements.get("module_docstring", "") |
| 636 | code = _MODULE_HEADER_COMMENTS.format(self.name) |
| 637 | if module_docstring: |
| 638 | code += _emit_docstring(module_docstring, 0) + "\n" |
| 639 | code += _MODULE_HEADER_IMPORTS |
| 640 | |
| 641 | # Collect needed imports to avoid duplicates |
| 642 | needs_command_builder = bool(self.commands) |
| 643 | needs_dataclass = self.commands or self.types or self.events |
| 644 | needs_callable = self.events |
| 645 | |
| 646 | stdlib_imports = [] |
| 647 | local_imports = [] |
| 648 | |
| 649 | # Add imports (field import will be added conditionally after code generation) |
| 650 | if needs_callable: |
| 651 | stdlib_imports.append("from collections.abc import Callable") |
| 652 | if needs_dataclass: |
| 653 | stdlib_imports.append("from dataclasses import dataclass") |
| 654 | stdlib_imports.append("from typing import Any") |
| 655 | |
| 656 | if needs_command_builder: |
| 657 | local_imports.append("from selenium.webdriver.common.bidi.common import command_builder") |
| 658 | if self.events: |
| 659 | local_imports.append( |
| 660 | "from selenium.webdriver.common.bidi._event_manager import EventConfig, _EventWrapper, _EventManager" |
| 661 | ) |
| 662 | |
| 663 | code += "\n".join(stdlib_imports) + "\n" |
no test coverage detected