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

Method generate_code

py/generate_bidi.py:638–1028  ·  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

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"
674 if local_imports:
675 code += "\n" + "\n".join(local_imports) + "\n"
676
677 code += "\n"
678
679 # Add helper function definitions from enhancements
680 # Collect all referenced helper functions (validate, transform)
681 helper_funcs_to_add = set()
682 for cmd in self.commands:
683 method_name_snake = cmd._camel_to_snake(cmd.name)
684 method_enhancements = enhancements.get(method_name_snake, {})
685 if "validate" in method_enhancements:
686 helper_funcs_to_add.add(("validate", method_enhancements["validate"]))
687 if "transform" in method_enhancements and isinstance(method_enhancements["transform"], dict):
688 transform_spec = method_enhancements["transform"]
689 if "func" in transform_spec:
690 helper_funcs_to_add.add(("transform", transform_spec["func"]))
691
692 # Generate helper functions if needed
693 if helper_funcs_to_add:
694 for func_type, func_name in sorted(helper_funcs_to_add):
695 if func_type == "validate" and func_name == "validate_download_behavior":

Callers 1

mainFunction · 0.45

Calls 15

_emit_docstringFunction · 0.85
_docstring_textFunction · 0.85
to_python_classMethod · 0.80
itemsMethod · 0.80
splitMethod · 0.80
to_python_methodMethod · 0.80
getMethod · 0.65
addMethod · 0.65
formatMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected