MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / CddlModule

Class CddlModule

py/generate_bidi.py:616–1028  ·  view source on GitHub ↗

Represents a CDDL module (e.g., script, network, browsing_context).

Source from the content-addressed store, hash-verified

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

Callers 4

parseMethod · 0.85
_extract_typesMethod · 0.85
_extract_eventsMethod · 0.85
_extract_commandsMethod · 0.85

Calls 1

fieldFunction · 0.50

Tested by

no test coverage detected