1717# You should have received a copy of the GNU Lesser General Public License
1818# along with Hydrogram. If not, see <http://www.gnu.org/licenses/>.
1919
20+ import contextlib
2021import json
2122import os
2223import re
2324import shutil
2425from functools import partial
2526from pathlib import Path
26- from typing import NamedTuple , List , Tuple
27+ from typing import List , NamedTuple , Tuple
2728
2829# from autoflake import fix_code
2930# from black import format_str, FileMode
3435
3536SECTION_RE = re .compile (r"---(\w+)---" )
3637LAYER_RE = re .compile (r"//\sLAYER\s(\d+)" )
37- COMBINATOR_RE = re .compile (
38- r"^([\w.]+)#([0-9a-f]+)\s(?:.*)=\s([\w<>.]+);$" , re .MULTILINE
39- )
38+ COMBINATOR_RE = re .compile (r"^([\w.]+)#([0-9a-f]+)\s(?:.*)=\s([\w<>.]+);$" , re .MULTILINE )
4039ARGS_RE = re .compile (r"[^{](\w+):([\w?!.<>#]+)" )
4140FLAGS_RE = re .compile (r"flags(\d?)\.(\d+)\?" )
4241FLAGS_RE_2 = re .compile (r"flags(\d?)\.(\d+)\?([\w<>.]+)" )
@@ -138,7 +137,7 @@ def get_type_hint(type: str) -> str:
138137 return f"Optional[{ type } ] = None" if is_flag else type
139138 else :
140139 ns , name = type .split ("." ) if "." in type else ("" , type )
141- type = f '"raw.base.' + "." .join ([ns , name ]).strip ("." ) + '"'
140+ type = '"raw.base.' + "." .join ([ns , name ]).strip ("." ) + '"'
142141
143142 return f'{ type } { " = None" if is_flag else "" } '
144143
@@ -264,7 +263,7 @@ def start(format: bool = False):
264263 qualtype = "." .join ([typespace , type ]).lstrip ("." )
265264
266265 # Pingu!
267- has_flags = not not FLAGS_RE_3 .findall (line )
266+ has_flags = bool ( FLAGS_RE_3 .findall (line ) )
268267
269268 args = ARGS_RE .findall (line )
270269
@@ -312,10 +311,8 @@ def start(format: bool = False):
312311
313312 for k , v in types_to_constructors .items ():
314313 for i in v :
315- try :
314+ with contextlib . suppress ( KeyError ) :
316315 constructors_to_functions [i ] = types_to_functions [k ]
317- except KeyError :
318- pass
319316
320317 # import json
321318 # print(json.dumps(namespaces_to_types, indent=2))
@@ -337,10 +334,7 @@ def start(format: bool = False):
337334
338335 type_docs = docs ["type" ].get (qualtype , None )
339336
340- if type_docs :
341- type_docs = type_docs ["desc" ]
342- else :
343- type_docs = "Telegram API base type."
337+ type_docs = type_docs ["desc" ] if type_docs else "Telegram API base type."
344338
345339 docstring = type_docs
346340
@@ -373,7 +367,7 @@ def start(format: bool = False):
373367 docstring = docstring ,
374368 name = type ,
375369 qualname = qualtype ,
376- types = ", " .join ([f"raw.types.{ c } " for c in constructors ]),
370+ types = ", " .join ([f' "raw.types.{ c } "' for c in constructors ]),
377371 doc_name = snake (type ).replace ("_" , "-" ),
378372 )
379373 )
@@ -396,29 +390,22 @@ def start(format: bool = False):
396390 docstring = ""
397391 docstring_args = []
398392
399- if c .section == "functions" :
400- combinator_docs = docs ["method" ]
401- else :
402- combinator_docs = docs ["constructor" ]
393+ combinator_docs = docs ["method" ] if c .section == "functions" else docs ["constructor" ]
403394
404395 for i , arg in enumerate (sorted_args ):
405396 arg_name , arg_type = arg
406397 is_optional = FLAGS_RE .match (arg_type )
407- flag_number = is_optional .group (1 ) if is_optional else - 1
408398 arg_type = arg_type .split ("?" )[- 1 ]
409399
410400 arg_docs = combinator_docs .get (c .qualname , None )
411401
412- if arg_docs :
413- arg_docs = arg_docs ["params" ].get (arg_name , "N/A" )
414- else :
415- arg_docs = "N/A"
402+ arg_docs = arg_docs ["params" ].get (arg_name , "N/A" ) if arg_docs else "N/A"
416403
417404 docstring_args .append (
418405 "{} ({}{}):\n {}\n " .format (
419406 arg_name ,
420407 get_docstring_arg_type (arg_type ),
421- ", *optional*" . format ( flag_number ) if is_optional else "" ,
408+ ", *optional*" if is_optional else "" ,
422409 arg_docs ,
423410 )
424411 )
@@ -432,22 +419,18 @@ def start(format: bool = False):
432419 constructor_docs = "Telegram API type."
433420
434421 docstring += constructor_docs + "\n "
435- docstring += (
436- f"\n Constructor of :obj:`~hydrogram.raw.base.{ c .qualtype } `."
437- )
422+ docstring += f"\n Constructor of :obj:`~hydrogram.raw.base.{ c .qualtype } `."
438423 else :
439424 function_docs = docs ["method" ].get (c .qualname , None )
440425
441426 if function_docs :
442427 docstring += function_docs ["desc" ] + "\n "
443428 else :
444- docstring += f "Telegram API function."
429+ docstring += "Telegram API function."
445430
446431 docstring += f"\n \n Details:\n - Layer: ``{ layer } ``\n - ID: ``{ c .id [2 :].upper ()} ``\n \n "
447- docstring += f" Parameters:\n " + (
448- f"\n " .join (docstring_args )
449- if docstring_args
450- else "No parameters required.\n "
432+ docstring += " Parameters:\n " + (
433+ "\n " .join (docstring_args ) if docstring_args else "No parameters required.\n "
451434 )
452435
453436 if c .section == "functions" :
@@ -480,9 +463,7 @@ def start(format: bool = False):
480463 if arg_name != f"flags{ flag .group (1 )} " :
481464 continue
482465
483- if flag .group (3 ) == "true" or flag .group (3 ).startswith (
484- "Vector"
485- ):
466+ if flag .group (3 ) == "true" or flag .group (3 ).startswith ("Vector" ):
486467 write_flags .append (
487468 f"{ arg_name } |= (1 << { flag .group (2 )} ) if self.{ i [0 ]} else 0"
488469 )
@@ -513,9 +494,7 @@ def start(format: bool = False):
513494 elif flag_type in CORE_TYPES :
514495 write_types += "\n "
515496 write_types += f"if self.{ arg_name } is not None:\n "
516- write_types += (
517- f"b.write({ flag_type .title ()} (self.{ arg_name } ))\n "
518- )
497+ write_types += f"b.write({ flag_type .title ()} (self.{ arg_name } ))\n "
519498
520499 read_types += "\n "
521500 read_types += f"{ arg_name } = { flag_type .title ()} .read(b) if flags{ number } & (1 << { index } ) else None"
@@ -530,11 +509,13 @@ def start(format: bool = False):
530509 )
531510
532511 read_types += "\n "
533- read_types += "{} = TLObject.read(b{}) if flags{} & (1 << {}) else []\n " .format (
534- arg_name ,
535- f", { sub_type .title ()} " if sub_type in CORE_TYPES else "" ,
536- number ,
537- index ,
512+ read_types += (
513+ "{} = TLObject.read(b{}) if flags{} & (1 << {}) else []\n " .format (
514+ arg_name ,
515+ f", { sub_type .title ()} " if sub_type in CORE_TYPES else "" ,
516+ number ,
517+ index ,
518+ )
538519 )
539520 else :
540521 write_types += "\n "
@@ -546,9 +527,7 @@ def start(format: bool = False):
546527 else :
547528 if arg_type in CORE_TYPES :
548529 write_types += "\n "
549- write_types += (
550- f"b.write({ arg_type .title ()} (self.{ arg_name } ))\n "
551- )
530+ write_types += f"b.write({ arg_type .title ()} (self.{ arg_name } ))\n "
552531
553532 read_types += "\n "
554533 read_types += f"{ arg_name } = { arg_type .title ()} .read(b)\n "
@@ -605,11 +584,7 @@ def start(format: bool = False):
605584 with open (dir_path / f"{ snake (module )} .py" , "w" ) as f :
606585 f .write (compiled_combinator )
607586
608- d = (
609- namespaces_to_constructors
610- if c .section == "types"
611- else namespaces_to_functions
612- )
587+ d = namespaces_to_constructors if c .section == "types" else namespaces_to_functions
613588
614589 if c .namespace not in d :
615590 d [c .namespace ] = []
@@ -646,9 +621,7 @@ def start(format: bool = False):
646621 f .write (f"from .{ snake (module )} import { t } \n " )
647622
648623 if not namespace :
649- f .write (
650- f"from . import { ', ' .join (filter (bool , namespaces_to_constructors ))} \n "
651- )
624+ f .write (f"from . import { ', ' .join (filter (bool , namespaces_to_constructors ))} \n " )
652625
653626 for namespace , types in namespaces_to_functions .items ():
654627 with open (DESTINATION_PATH / "functions" / namespace / "__init__.py" , "w" ) as f :
@@ -664,9 +637,7 @@ def start(format: bool = False):
664637 f .write (f"from .{ snake (module )} import { t } \n " )
665638
666639 if not namespace :
667- f .write (
668- f"from . import { ', ' .join (filter (bool , namespaces_to_functions ))} "
669- )
640+ f .write (f"from . import { ', ' .join (filter (bool , namespaces_to_functions ))} " )
670641
671642 with open (DESTINATION_PATH / "all.py" , "w" , encoding = "utf-8" ) as f :
672643 f .write (notice + "\n \n " )
@@ -689,8 +660,8 @@ def start(format: bool = False):
689660 f .write ("\n }\n " )
690661
691662
692- if "__main__" == __name__ :
693- HOME_PATH = Path ("." )
663+ if __name__ == "__main__" :
664+ HOME_PATH = Path ()
694665 DESTINATION_PATH = Path ("../../hydrogram/raw" )
695666 NOTICE_PATH = Path ("../../NOTICE" )
696667
0 commit comments