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

Method to_python_method

py/generate_bidi.py:194–377  ·  view source on GitHub ↗

Generate Python method code for this command. Args: enhancements: Dictionary with enhancement rules for this method

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

Source from the content-addressed store, hash-verified

192 description: str = ""
193
194 def to_python_method(self, enhancements: dict[str, Any] | None = None) -> str:
195 """Generate Python method code for this command.
196
197 Args:
198 enhancements: Dictionary with enhancement rules for this method
199 """
200 enhancements = enhancements or {}
201 method_name = self._camel_to_snake(self.name)
202
203 # Build parameter list with type hints
204 # Check if there's a params_override for user-friendly named arguments
205 params_to_use = self.params
206 if "params_override" in enhancements:
207 params_to_use = enhancements["params_override"]
208
209 param_strs = []
210 param_names = [] # Keep track of parameter names for later use
211 for param_name, param_type in params_to_use.items():
212 if param_type in ["bool", "str", "int"]:
213 python_type = param_type
214 else:
215 python_type = CddlType.get_annotation(param_type)
216 snake_param = self._camel_to_snake(param_name)
217 param_names.append((param_name, snake_param))
218 param_strs.append(f"{snake_param}: {python_type} | None = None")
219
220 if param_strs:
221 # Check if full signature would exceed line length limit (120 chars)
222 single_line_signature = f" def {method_name}(self, {', '.join(param_strs)}):"
223 if len(single_line_signature) > 120:
224 # Format parameters on multiple lines
225 body = f" def {method_name}(\n"
226 body += " self,\n"
227 for i, param_str in enumerate(param_strs):
228 if i < len(param_strs) - 1:
229 body += f" {param_str},\n"
230 else:
231 body += f" {param_str},\n"
232 body += " ):\n"
233 else:
234 param_list = "self, " + ", ".join(param_strs)
235 body = f" def {method_name}({param_list}):\n"
236 else:
237 body = f" def {method_name}(self):\n"
238 docstring = enhancements.get("docstring") or self.description or f"Execute {self.module}.{self.name}."
239 body += _emit_docstring(docstring, 8)
240
241 # Add automatic validation for required parameters
242 # (This is used unless there's no required_params, in which case all params are optional)
243 if self.required_params:
244 method_snake = self._camel_to_snake(self.name)
245 for param_name, snake_param in param_names:
246 if param_name in self.required_params:
247 body += f" if {snake_param} is None:\n"
248 msg = f"{method_snake}() missing required argument:"
249 error_message = f"{msg} {snake_param!r}"
250 body += f" raise TypeError({error_message!r})\n"
251 body += "\n"

Callers 1

generate_codeMethod · 0.80

Calls 7

_camel_to_snakeMethod · 0.95
_generate_field_argsMethod · 0.95
_emit_docstringFunction · 0.85
joinMethod · 0.80
getMethod · 0.65
get_annotationMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected