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

Class CddlModule

py/generate_bidi.py:612–1024  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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