1616# You should have received a copy of the GNU Lesser General Public License
1717# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818
19+ import json
1920import os
2021import re
2122import shutil
5960namespaces_to_constructors = {}
6061namespaces_to_functions = {}
6162
63+ try :
64+ with open ("docs.json" ) as f :
65+ docs = json .load (f )
66+ except FileNotFoundError :
67+ docs = {
68+ "type" : {},
69+ "constructor" : {},
70+ "method" : {}
71+ }
72+
6273
6374class Combinator (NamedTuple ):
6475 section : str
@@ -149,7 +160,7 @@ def remove_whitespaces(source: str) -> str:
149160 return "\n " .join (lines )
150161
151162
152- def get_docstring_arg_type (t : str , is_list : bool = False , is_pyrogram_type : bool = False ):
163+ def get_docstring_arg_type (t : str ):
153164 if t in CORE_TYPES :
154165 if t == "long" :
155166 return "``int`` ``64-bit``"
@@ -167,9 +178,9 @@ def get_docstring_arg_type(t: str, is_list: bool = False, is_pyrogram_type: bool
167178 elif t == "TLObject" or t == "X" :
168179 return "Any object from :obj:`~pyrogram.raw.types`"
169180 elif t == "!X" :
170- return "Any method from :obj:`~pyrogram.raw.functions`"
181+ return "Any function from :obj:`~pyrogram.raw.functions`"
171182 elif t .lower ().startswith ("vector" ):
172- return "List of " + get_docstring_arg_type (t .split ("<" , 1 )[1 ][:- 1 ], True )
183+ return "List of " + get_docstring_arg_type (t .split ("<" , 1 )[1 ][:- 1 ])
173184 else :
174185 return f":obj:`{ t } <pyrogram.raw.base.{ t } >`"
175186
@@ -183,10 +194,7 @@ def get_references(t: str, kind: str):
183194 raise ValueError ("Invalid kind" )
184195
185196 if t :
186- return "\n " .join (
187- f"- :obj:`{ i } <pyrogram.raw.functions.{ i } >`"
188- for i in t
189- ), len (t )
197+ return "\n " .join (t ), len (t )
190198
191199 return None , 0
192200
@@ -315,17 +323,33 @@ def start(format: bool = False):
315323
316324 constructors = sorted (types_to_constructors [qualtype ])
317325 constr_count = len (constructors )
318- items = "\n " .join ([f"- :obj:`{ c } <pyrogram.raw.types.{ c } >`" for c in constructors ])
326+ items = "\n " .join ([f"{ c } " for c in constructors ])
327+
328+ type_docs = docs ["type" ].get (qualtype , None )
329+
330+ if type_docs :
331+ type_docs = type_docs ["desc" ]
332+ else :
333+ type_docs = "Telegram API base type."
319334
320- docstring = f"This base type has { constr_count } constructor{ 's' if constr_count > 1 else '' } available.\n \n "
321- docstring += f" Constructors:\n .. hlist::\n :columns: 2\n \n { items } "
335+ docstring = type_docs
336+
337+ docstring += f"\n \n Constructors:\n " \
338+ f" This base type has { constr_count } constructor{ 's' if constr_count > 1 else '' } available.\n \n " \
339+ f" .. currentmodule:: pyrogram.raw.types\n \n " \
340+ f" .. autosummary::\n " \
341+ f" :nosignatures:\n \n " \
342+ f" { items } "
322343
323344 references , ref_count = get_references (qualtype , "types" )
324345
325346 if references :
326- docstring += f"\n \n See Also:\n This object can be returned by " \
327- f"{ ref_count } method{ 's' if ref_count > 1 else '' } :" \
328- f"\n \n .. hlist::\n :columns: 2\n \n " + references
347+ docstring += f"\n \n Functions:\n This object can be returned by " \
348+ f"{ ref_count } function{ 's' if ref_count > 1 else '' } .\n \n " \
349+ f" .. currentmodule:: pyrogram.raw.functions\n \n " \
350+ f" .. autosummary::\n " \
351+ f" :nosignatures:\n \n " \
352+ f" " + references
329353
330354 with open (dir_path / f"{ snake (module )} .py" , "w" ) as f :
331355 f .write (
@@ -359,41 +383,67 @@ def start(format: bool = False):
359383 docstring = ""
360384 docstring_args = []
361385
386+ if c .section == "functions" :
387+ combinator_docs = docs ["method" ]
388+ else :
389+ combinator_docs = docs ["constructor" ]
390+
362391 for i , arg in enumerate (sorted_args ):
363392 arg_name , arg_type = arg
364393 is_optional = FLAGS_RE .match (arg_type )
365394 flag_number = is_optional .group (1 ) if is_optional else - 1
366395 arg_type = arg_type .split ("?" )[- 1 ]
367396
397+ arg_docs = combinator_docs .get (c .qualname , None )
398+
399+ if arg_docs :
400+ arg_docs = arg_docs ["params" ].get (arg_name , "N/A" )
401+ else :
402+ arg_docs = "N/A"
403+
368404 docstring_args .append (
369- "{}{}: {} " .format (
405+ "{} ({}{}): \n {} \n " .format (
370406 arg_name ,
371- " (optional)" .format (flag_number ) if is_optional else "" ,
372- get_docstring_arg_type (arg_type , is_pyrogram_type = c .namespace == "pyrogram" )
407+ get_docstring_arg_type (arg_type ),
408+ ", *optional*" .format (flag_number ) if is_optional else "" ,
409+ arg_docs
373410 )
374411 )
375412
376413 if c .section == "types" :
377- docstring += f"This object is a constructor of the base type :obj:`~pyrogram.raw.base.{ c .qualtype } `.\n \n "
378- else :
379- docstring += f"Telegram API method.\n \n "
414+ constructor_docs = docs ["constructor" ].get (c .qualname , None )
380415
381- docstring += f" Details:\n - Layer: ``{ layer } ``\n - ID: ``{ c .id [2 :].upper ()} ``\n \n "
416+ if constructor_docs :
417+ constructor_docs = constructor_docs ["desc" ]
418+ else :
419+ constructor_docs = "Telegram API type."
382420
383- if docstring_args :
384- docstring += " Parameters: \n " + " \n " . join ( docstring_args )
421+ docstring += constructor_docs + " \n "
422+ docstring += f" \n Constructor of :obj:`~pyrogram.raw.base. { c . qualtype } `."
385423 else :
386- docstring += " **No parameters required.**"
424+ function_docs = docs ["method" ].get (c .qualname , None )
425+
426+ if function_docs :
427+ docstring += function_docs ["desc" ] + "\n "
428+ else :
429+ docstring += f"Telegram API function."
430+
431+ docstring += f"\n \n Details:\n - Layer: ``{ layer } ``\n - ID: ``{ c .id [2 :].upper ()} ``\n \n "
432+ docstring += f" Parameters:\n " + \
433+ (f"\n " .join (docstring_args ) if docstring_args else "No parameters required.\n " )
387434
388435 if c .section == "functions" :
389- docstring += "\n \n Returns:\n " + get_docstring_arg_type (c .qualtype )
436+ docstring += "\n Returns:\n " + get_docstring_arg_type (c .qualtype )
390437 else :
391438 references , count = get_references (c .qualname , "constructors" )
392439
393440 if references :
394- docstring += f"\n \n See Also:\n This object can be returned by " \
395- f"{ count } method{ 's' if count > 1 else '' } :" \
396- f"\n \n .. hlist::\n :columns: 2\n \n " + references
441+ docstring += f"\n Functions:\n This object can be returned by " \
442+ f"{ count } function{ 's' if count > 1 else '' } .\n \n " \
443+ f" .. currentmodule:: pyrogram.raw.functions\n \n " \
444+ f" .. autosummary::\n " \
445+ f" :nosignatures:\n \n " \
446+ f" " + references
397447
398448 write_types = read_types = "" if c .has_flags else "# No flags\n "
399449
0 commit comments