44import os
55import re
66import shutil
7+ from pathlib import Path
78
89HOME = "compiler/docs"
910DESTINATION = "docs/source/telegram"
1011PYROGRAM_API_DEST = "docs/source/api"
11-
1212FUNCTIONS_PATH = "pyrogram/raw/functions"
1313TYPES_PATH = "pyrogram/raw/types"
1414BASE_PATH = "pyrogram/raw/base"
15-
1615FUNCTIONS_BASE = "functions"
1716TYPES_BASE = "types"
1817BASE_BASE = "base"
1918
2019
21- def snek (s : str ):
20+ def snake (s : str ):
2221 s = re .sub (r"(.)([A-Z][a-z]+)" , r"\1_\2" , s )
2322 return re .sub (r"([a-z0-9])([A-Z])" , r"\1_\2" , s ).lower ()
2423
@@ -34,7 +33,7 @@ def build(path, level=0):
3433 if not i .startswith ("__" ):
3534 build ("/" .join ([path , i ]), level = level + 1 )
3635 except NotADirectoryError :
37- with open (path + "/" + i , encoding = "utf-8" ) as f :
36+ with Path (path , i ). open ( encoding = "utf-8" ) as f :
3837 p = ast .parse (f .read ())
3938
4039 for node in ast .walk (p ):
@@ -45,10 +44,7 @@ def build(path, level=0):
4544 continue
4645
4746 full_path = (
48- os .path .basename (path )
49- + "/"
50- + snek (name ).replace ("_" , "-" )
51- + ".rst"
47+ Path (path ).name + "/" + snake (name ).replace ("_" , "-" ) + ".rst"
5248 )
5349
5450 if level :
@@ -60,11 +56,11 @@ def build(path, level=0):
6056
6157 full_name = f"{ (namespace + '.' ) if namespace else '' } { name } "
6258
63- os . makedirs (
64- os . path . dirname ( DESTINATION + "/" + full_path ) , exist_ok = True
59+ Path ( DESTINATION , full_path ). parent . mkdir (
60+ parents = True , exist_ok = True
6561 )
6662
67- with open (DESTINATION + "/" + full_path , "w" , encoding = "utf-8" ) as f :
63+ with Path (DESTINATION , full_path ). open ( "w" , encoding = "utf-8" ) as f :
6864 f .write (
6965 page_template .format (
7066 title = full_name ,
@@ -84,10 +80,7 @@ def build(path, level=0):
8480
8581 for k , v in sorted (all_entities .items ()):
8682 v = sorted (v )
87- entities = []
88-
89- for i in v :
90- entities .append (f'{ i } <{ snek (i ).replace ("_" , "-" )} >' )
83+ entities = [f'{ i } <{ snake (i ).replace ("_" , "-" )} >' for i in v ]
9184
9285 if k != base :
9386 inner_path = base + "/" + k + "/index" + ".rst"
@@ -100,7 +93,7 @@ def build(path, level=0):
10093 inner_path = base + "/index" + ".rst"
10194 module = f"pyrogram.raw.{ base } "
10295
103- with open (DESTINATION + "/" + inner_path , "w" , encoding = "utf-8" ) as f :
96+ with Path (DESTINATION , inner_path ). open ( "w" , encoding = "utf-8" ) as f :
10497 if k == base :
10598 f .write (":tocdepth: 1\n \n " )
10699 k = "Raw " + k
@@ -414,20 +407,20 @@ def get_title_list(s: str) -> list:
414407 root = PYROGRAM_API_DEST + "/methods"
415408
416409 shutil .rmtree (root , ignore_errors = True )
417- os .mkdir (root )
410+ Path ( root ) .mkdir ()
418411
419- with open (HOME + "/ template/methods.rst" ) as f :
412+ with Path (HOME , " template/methods.rst"). open ( ) as f :
420413 template = f .read ()
421414
422- with open (root + "/ index.rst", "w" ) as f :
415+ with Path (root , " index.rst"). open ( "w" ) as f :
423416 fmt_keys = {}
424417
425418 for k , v in categories .items ():
426419 name , * methods = get_title_list (v )
427420 fmt_keys .update ({k : "\n " .join (f"{ m } <{ m } >" for m in methods )})
428421
429422 for method in methods :
430- with open (root + f"/ { method } .rst", "w" ) as f2 :
423+ with Path (root , f" { method } .rst"). open ( "w" ) as f2 :
431424 title = f"{ method } ()"
432425
433426 f2 .write (title + "\n " + "=" * len (title ) + "\n \n " )
@@ -436,7 +429,7 @@ def get_title_list(s: str) -> list:
436429 functions = ["idle" , "compose" ]
437430
438431 for func in functions :
439- with open (root + f"/ { func } .rst", "w" ) as f2 :
432+ with Path (root , f" { func } .rst"). open ( "w" ) as f2 :
440433 title = f"{ func } ()"
441434
442435 f2 .write (title + "\n " + "=" * len (title ) + "\n \n " )
@@ -503,6 +496,8 @@ def get_title_list(s: str) -> list:
503496 Venue
504497 Sticker
505498 StickerSet
499+ ContactRegistered
500+ ScreenshotTaken
506501 Game
507502 GiftedPremium
508503 Giveaway
@@ -691,12 +686,12 @@ def get_title_list(s: str) -> list:
691686 root = PYROGRAM_API_DEST + "/types"
692687
693688 shutil .rmtree (root , ignore_errors = True )
694- os .mkdir (root )
689+ Path ( root ) .mkdir ()
695690
696- with open (HOME + "/ template/types.rst" ) as f :
691+ with Path (HOME , " template/types.rst"). open ( ) as f :
697692 template = f .read ()
698693
699- with open (root + "/ index.rst", "w" ) as f :
694+ with Path (root , " index.rst"). open ( "w" ) as f :
700695 fmt_keys = {}
701696
702697 for k , v in categories .items ():
@@ -705,7 +700,7 @@ def get_title_list(s: str) -> list:
705700 fmt_keys .update ({k : "\n " .join (types )})
706701
707702 for type in types :
708- with open (root + f"/ { type } .rst", "w" ) as f2 :
703+ with Path (root , f" { type } .rst"). open ( "w" ) as f2 :
709704 title = f"{ type } "
710705
711706 f2 .write (title + "\n " + "=" * len (title ) + "\n \n " )
@@ -836,17 +831,21 @@ def get_title_list(s: str) -> list:
836831 ChatJoinRequest.approve
837832 ChatJoinRequest.decline
838833 """ ,
834+ "active_session" : """
835+ ActiveSession
836+ ActiveSession.reset
837+ """ ,
839838 }
840839
841840 root = PYROGRAM_API_DEST + "/bound-methods"
842841
843842 shutil .rmtree (root , ignore_errors = True )
844- os .mkdir (root )
843+ Path ( root ) .mkdir ()
845844
846- with open (HOME + "/ template/bound-methods.rst" ) as f :
845+ with Path (HOME , " template/bound-methods.rst"). open ( ) as f :
847846 template = f .read ()
848847
849- with open (root + "/ index.rst", "w" ) as f :
848+ with Path (root , " index.rst"). open ( "w" ) as f :
850849 fmt_keys = {}
851850
852851 for k , v in categories .items ():
@@ -869,9 +868,8 @@ def get_title_list(s: str) -> list:
869868 }
870869 )
871870
872- # noinspection PyShadowingBuiltins
873871 for bm in bound_methods :
874- with open (root + f"/ { bm } .rst", "w" ) as f2 :
872+ with Path (root , f" { bm } .rst"). open ( "w" ) as f2 :
875873 title = f"{ bm } ()"
876874
877875 f2 .write (title + "\n " + "=" * len (title ) + "\n \n " )
@@ -881,15 +879,14 @@ def get_title_list(s: str) -> list:
881879
882880
883881def start ():
884- global page_template
885- global toctree
882+ global page_template , toctree # noqa: PLW0603
886883
887884 shutil .rmtree (DESTINATION , ignore_errors = True )
888885
889- with open (HOME + "/ template/page.txt", encoding = "utf-8" ) as f :
886+ with Path (HOME , " template/page.txt"). open ( encoding = "utf-8" ) as f :
890887 page_template = f .read ()
891888
892- with open (HOME + "/ template/toctree.txt", encoding = "utf-8" ) as f :
889+ with Path (HOME , " template/toctree.txt"). open ( encoding = "utf-8" ) as f :
893890 toctree = f .read ()
894891
895892 generate (TYPES_PATH , TYPES_BASE )
0 commit comments