From 310adbb76f5a28f5e30b89a0d31d45296eea68b8 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 3 Jan 2018 17:40:38 +0100 Subject: [PATCH] Add docstrings for generated code --- compiler/api/compiler.py | 53 +++++++++++++++++++++++++++++++++ compiler/api/template/class.txt | 3 ++ 2 files changed, 56 insertions(+) diff --git a/compiler/api/compiler.py b/compiler/api/compiler.py index e0412cb1..e1f35e8f 100644 --- a/compiler/api/compiler.py +++ b/compiler/api/compiler.py @@ -178,6 +178,58 @@ class Compiler: ["self.{0} = {0} # {1}".format(i[0], i[1]) for i in args] ) if args else "pass" + docstring_args = [] + + for i, arg in enumerate(sorted_args): + arg_name, arg_type = arg + is_optional = arg_type.startswith("flags.") + arg_type = arg_type.split("?")[-1] + + if arg_type in core_types: + if "int" in arg_type or arg_type == "long": + arg_type = "``int``" + elif arg_type == "double": + arg_type = "``float``" + else: + arg_type = "``{}``".format(arg_type.lower()) + elif arg_type == "true": + arg_type = "``bool``" + elif arg_type == "!X": + arg_type = "A function from :class:`pyrogram.api.functions`" + else: + if arg_type.startswith("Vector"): + sub_type = arg_type.split("<")[1][:-1] + + if sub_type in core_types: + arg_type = "List of ``{}``".format(self.caml(sub_type)) + else: + arg_type = "List of :class:`pyrogram.api.types.{}`".format( + ".".join( + arg_type.split(".")[:-1] + + [self.caml(arg_type.split(".")[-1])] + ) + ) + else: + arg_type = ":class:`pyrogram.api.types.{}`".format( + ".".join( + arg_type.split(".")[:-1] + + [self.caml(arg_type.split(".")[-1])] + ) + ) + + docstring_args.append( + "{}: {}{}".format( + arg_name, + arg_type, + " (optional)" if is_optional else "" + ) + ) + + if docstring_args: + docstring_args = "Args:\n " + "\n ".join(docstring_args) + else: + docstring_args = "No parameters required." + if has_flags: write_flags = [] for i in args: @@ -268,6 +320,7 @@ class Compiler: self.template.format( notice=self.notice, class_name=self.caml(name), + docstring_args=docstring_args, object_id=object_id, arguments=arguments, fields=fields, diff --git a/compiler/api/template/class.txt b/compiler/api/template/class.txt index 8fa495c6..2cba4bb9 100644 --- a/compiler/api/template/class.txt +++ b/compiler/api/template/class.txt @@ -6,6 +6,9 @@ from pyrogram.api.core import * class {class_name}(Object): + """ + {docstring_args} + """ ID = {object_id} def __init__(self{arguments}, **kwargs):