MCPcopy Index your code
hub / github.com/SeleniumHQ/selenium / generate_code

Method generate_code

py/generate_bidi.py:628–1018  ·  view source on GitHub ↗

Generate Python code for this module. Args: enhancements: Dictionary with module-level enhancements

(self, enhancements: dict[str, Any] | None = None)

Source from the content-addressed store, hash-verified

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"
664 if local_imports:
665 code += "\n" + "\n".join(local_imports) + "\n"
666
667 code += "\n"
668
669 # Add helper function definitions from enhancements
670 # Collect all referenced helper functions (validate, transform)
671 helper_funcs_to_add = set()
672 for cmd in self.commands:
673 method_name_snake = cmd._camel_to_snake(cmd.name)
674 method_enhancements = enhancements.get(method_name_snake, {})
675 if "validate" in method_enhancements:
676 helper_funcs_to_add.add(("validate", method_enhancements["validate"]))
677 if "transform" in method_enhancements and isinstance(method_enhancements["transform"], dict):
678 transform_spec = method_enhancements["transform"]
679 if "func" in transform_spec:
680 helper_funcs_to_add.add(("transform", transform_spec["func"]))
681
682 # Generate helper functions if needed
683 if helper_funcs_to_add:
684 for func_type, func_name in sorted(helper_funcs_to_add):
685 if func_type == "validate" and func_name == "validate_download_behavior":

Callers 1

mainFunction · 0.45

Calls 15

_emit_docstringFunction · 0.85
setFunction · 0.85
_docstring_textFunction · 0.85
joinMethod · 0.80
to_python_classMethod · 0.80
splitMethod · 0.80
to_python_methodMethod · 0.80
getMethod · 0.65
addMethod · 0.65
formatMethod · 0.45

Tested by

no test coverage detected