mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-31 06:16:06 +00:00
Update docs
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
@@ -59,6 +60,16 @@ namespaces_to_types = {}
|
||||
namespaces_to_constructors = {}
|
||||
namespaces_to_functions = {}
|
||||
|
||||
try:
|
||||
with open("docs.json") as f:
|
||||
docs = json.load(f)
|
||||
except FileNotFoundError:
|
||||
docs = {
|
||||
"type": {},
|
||||
"constructor": {},
|
||||
"method": {}
|
||||
}
|
||||
|
||||
|
||||
class Combinator(NamedTuple):
|
||||
section: str
|
||||
@@ -149,7 +160,7 @@ def remove_whitespaces(source: str) -> str:
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def get_docstring_arg_type(t: str, is_list: bool = False, is_pyrogram_type: bool = False):
|
||||
def get_docstring_arg_type(t: str):
|
||||
if t in CORE_TYPES:
|
||||
if t == "long":
|
||||
return "``int`` ``64-bit``"
|
||||
@@ -167,9 +178,9 @@ def get_docstring_arg_type(t: str, is_list: bool = False, is_pyrogram_type: bool
|
||||
elif t == "TLObject" or t == "X":
|
||||
return "Any object from :obj:`~pyrogram.raw.types`"
|
||||
elif t == "!X":
|
||||
return "Any method from :obj:`~pyrogram.raw.functions`"
|
||||
return "Any function from :obj:`~pyrogram.raw.functions`"
|
||||
elif t.lower().startswith("vector"):
|
||||
return "List of " + get_docstring_arg_type(t.split("<", 1)[1][:-1], True)
|
||||
return "List of " + get_docstring_arg_type(t.split("<", 1)[1][:-1])
|
||||
else:
|
||||
return f":obj:`{t} <pyrogram.raw.base.{t}>`"
|
||||
|
||||
@@ -183,10 +194,7 @@ def get_references(t: str, kind: str):
|
||||
raise ValueError("Invalid kind")
|
||||
|
||||
if t:
|
||||
return "\n ".join(
|
||||
f"- :obj:`{i} <pyrogram.raw.functions.{i}>`"
|
||||
for i in t
|
||||
), len(t)
|
||||
return "\n ".join(t), len(t)
|
||||
|
||||
return None, 0
|
||||
|
||||
@@ -315,17 +323,33 @@ def start(format: bool = False):
|
||||
|
||||
constructors = sorted(types_to_constructors[qualtype])
|
||||
constr_count = len(constructors)
|
||||
items = "\n ".join([f"- :obj:`{c} <pyrogram.raw.types.{c}>`" for c in constructors])
|
||||
items = "\n ".join([f"{c}" for c in constructors])
|
||||
|
||||
docstring = f"This base type has {constr_count} constructor{'s' if constr_count > 1 else ''} available.\n\n"
|
||||
docstring += f" Constructors:\n .. hlist::\n :columns: 2\n\n {items}"
|
||||
type_docs = docs["type"].get(qualtype, None)
|
||||
|
||||
if type_docs:
|
||||
type_docs = type_docs["desc"]
|
||||
else:
|
||||
type_docs = "Telegram API base type."
|
||||
|
||||
docstring = type_docs
|
||||
|
||||
docstring += f"\n\n Constructors:\n" \
|
||||
f" This base type has {constr_count} constructor{'s' if constr_count > 1 else ''} available.\n\n" \
|
||||
f" .. currentmodule:: pyrogram.raw.types\n\n" \
|
||||
f" .. autosummary::\n" \
|
||||
f" :nosignatures:\n\n" \
|
||||
f" {items}"
|
||||
|
||||
references, ref_count = get_references(qualtype, "types")
|
||||
|
||||
if references:
|
||||
docstring += f"\n\n See Also:\n This object can be returned by " \
|
||||
f"{ref_count} method{'s' if ref_count > 1 else ''}:" \
|
||||
f"\n\n .. hlist::\n :columns: 2\n\n " + references
|
||||
docstring += f"\n\n Functions:\n This object can be returned by " \
|
||||
f"{ref_count} function{'s' if ref_count > 1 else ''}.\n\n" \
|
||||
f" .. currentmodule:: pyrogram.raw.functions\n\n" \
|
||||
f" .. autosummary::\n" \
|
||||
f" :nosignatures:\n\n" \
|
||||
f" " + references
|
||||
|
||||
with open(dir_path / f"{snake(module)}.py", "w") as f:
|
||||
f.write(
|
||||
@@ -359,41 +383,67 @@ def start(format: bool = False):
|
||||
docstring = ""
|
||||
docstring_args = []
|
||||
|
||||
if c.section == "functions":
|
||||
combinator_docs = docs["method"]
|
||||
else:
|
||||
combinator_docs = docs["constructor"]
|
||||
|
||||
for i, arg in enumerate(sorted_args):
|
||||
arg_name, arg_type = arg
|
||||
is_optional = FLAGS_RE.match(arg_type)
|
||||
flag_number = is_optional.group(1) if is_optional else -1
|
||||
arg_type = arg_type.split("?")[-1]
|
||||
|
||||
arg_docs = combinator_docs.get(c.qualname, None)
|
||||
|
||||
if arg_docs:
|
||||
arg_docs = arg_docs["params"].get(arg_name, "N/A")
|
||||
else:
|
||||
arg_docs = "N/A"
|
||||
|
||||
docstring_args.append(
|
||||
"{}{}: {}".format(
|
||||
"{} ({}{}):\n {}\n".format(
|
||||
arg_name,
|
||||
" (optional)".format(flag_number) if is_optional else "",
|
||||
get_docstring_arg_type(arg_type, is_pyrogram_type=c.namespace == "pyrogram")
|
||||
get_docstring_arg_type(arg_type),
|
||||
", *optional*".format(flag_number) if is_optional else "",
|
||||
arg_docs
|
||||
)
|
||||
)
|
||||
|
||||
if c.section == "types":
|
||||
docstring += f"This object is a constructor of the base type :obj:`~pyrogram.raw.base.{c.qualtype}`.\n\n"
|
||||
else:
|
||||
docstring += f"Telegram API method.\n\n"
|
||||
constructor_docs = docs["constructor"].get(c.qualname, None)
|
||||
|
||||
docstring += f" Details:\n - Layer: ``{layer}``\n - ID: ``{c.id[2:].upper()}``\n\n"
|
||||
if constructor_docs:
|
||||
constructor_docs = constructor_docs["desc"]
|
||||
else:
|
||||
constructor_docs = "Telegram API type."
|
||||
|
||||
if docstring_args:
|
||||
docstring += " Parameters:\n " + "\n ".join(docstring_args)
|
||||
docstring += constructor_docs + "\n"
|
||||
docstring += f"\n Constructor of :obj:`~pyrogram.raw.base.{c.qualtype}`."
|
||||
else:
|
||||
docstring += " **No parameters required.**"
|
||||
function_docs = docs["method"].get(c.qualname, None)
|
||||
|
||||
if function_docs:
|
||||
docstring += function_docs["desc"] + "\n"
|
||||
else:
|
||||
docstring += f"Telegram API function."
|
||||
|
||||
docstring += f"\n\n Details:\n - Layer: ``{layer}``\n - ID: ``{c.id[2:].upper()}``\n\n"
|
||||
docstring += f" Parameters:\n " + \
|
||||
(f"\n ".join(docstring_args) if docstring_args else "No parameters required.\n")
|
||||
|
||||
if c.section == "functions":
|
||||
docstring += "\n\n Returns:\n " + get_docstring_arg_type(c.qualtype)
|
||||
docstring += "\n Returns:\n " + get_docstring_arg_type(c.qualtype)
|
||||
else:
|
||||
references, count = get_references(c.qualname, "constructors")
|
||||
|
||||
if references:
|
||||
docstring += f"\n\n See Also:\n This object can be returned by " \
|
||||
f"{count} method{'s' if count > 1 else ''}:" \
|
||||
f"\n\n .. hlist::\n :columns: 2\n\n " + references
|
||||
docstring += f"\n Functions:\n This object can be returned by " \
|
||||
f"{count} function{'s' if count > 1 else ''}.\n\n" \
|
||||
f" .. currentmodule:: pyrogram.raw.functions\n\n" \
|
||||
f" .. autosummary::\n" \
|
||||
f" :nosignatures:\n\n" \
|
||||
f" " + references
|
||||
|
||||
write_types = read_types = "" if c.has_flags else "# No flags\n "
|
||||
|
||||
|
Reference in New Issue
Block a user