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

Method to_python_method

py/generate_bidi.py:204–387  ·  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

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

Callers 1

generate_codeMethod · 0.80

Calls 7

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

Tested by

no test coverage detected