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

Method generate_code

py/generate.py:612–694  ·  view source on GitHub ↗

Generate code for a CDP command.

(self)

Source from the content-addressed store, hash-verified

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