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

Method to_python_method

py/generate_bidi.py:200–383  ·  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

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

Callers 1

generate_codeMethod · 0.80

Calls 6

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

Tested by

no test coverage detected