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

Method generate_code

py/generate.py:611–693  ·  view source on GitHub ↗

Generate code for a CDP command.

(self)

Source from the content-addressed store, hash-verified

609 )
610
611 def generate_code(self):
612 """Generate code for a CDP command."""
613 global current_version
614 # Generate the function header
615 if len(self.returns) == 0:
616 ret_type = "None"
617 elif len(self.returns) == 1:
618 ret_type = self.returns[0].py_annotation
619 else:
620 nested_types = ", ".join(r.py_annotation for r in self.returns)
621 ret_type = f"typing.Tuple[{nested_types}]"
622 ret_type = f"typing.Generator[T_JSON_DICT,T_JSON_DICT,{ret_type}]"
623
624 code = ""
625
626 code += f"def {self.py_name}("
627 ret = f") -> {ret_type}:\n"
628 if self.parameters:
629 params = [p.generate_code() for p in self.parameters]
630 optional = False
631 clean_params = []
632 for para in params:
633 if "= None" in para:
634 optional = True
635 if optional and "= None" not in para:
636 para += " = None"
637 clean_params.append(para)
638 code += "\n"
639 code += indent(",\n".join(clean_params), 8)
640 code += "\n"
641 code += indent(ret, 4)
642 else:
643 code += ret
644
645 # Generate the docstring
646 doc = ""
647 if self.description:
648 doc = self.description
649 if self.experimental:
650 doc += "\n\n**EXPERIMENTAL**"
651 if self.parameters and doc:
652 doc += "\n\n"
653 elif not self.parameters and self.returns:
654 doc += "\n"
655 doc += "\n".join(p.generate_doc() for p in self.parameters)
656 if len(self.returns) == 1:
657 doc += "\n"
658 ret_doc = self.returns[0].generate_doc()
659 doc += f":returns: {ret_doc}"
660 elif len(self.returns) > 1:
661 doc += "\n"
662 doc += ":returns: A tuple with the following items:\n\n"
663 ret_docs = "\n".join(f"{i}. **{r.name}** - {r.generate_doc()}" for i, r in enumerate(self.returns))
664 doc += indent(ret_docs, 4)
665 if doc:
666 code += indent(docstring(doc), 4)
667
668 # Generate the function body

Callers

nothing calls this directly

Calls 7

docstringFunction · 0.85
generate_to_jsonMethod · 0.80
generate_returnMethod · 0.80
indentFunction · 0.70
generate_codeMethod · 0.45
appendMethod · 0.45
generate_docMethod · 0.45

Tested by

no test coverage detected