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

Method generate_code

py/generate_bidi.py:634–1024  ·  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

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

Callers 1

mainFunction · 0.45

Calls 14

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

Tested by

no test coverage detected