forked from TelegramPlayground/PyroTGFork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.py
More file actions
845 lines (769 loc) · 23.8 KB
/
Copy pathcompiler.py
File metadata and controls
845 lines (769 loc) · 23.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# 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 ast
import os
import re
import shutil
HOME = "compiler/docs"
DESTINATION = "docs/source/telegram"
PYROGRAM_API_DEST = "docs/source/api"
FUNCTIONS_PATH = "pyrogram/raw/functions"
TYPES_PATH = "pyrogram/raw/types"
BASE_PATH = "pyrogram/raw/base"
FUNCTIONS_BASE = "functions"
TYPES_BASE = "types"
BASE_BASE = "base"
def snek(s: str):
s = re.sub(r"(.)([A-Z][a-z]+)", r"\1_\2", s)
return re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", s).lower()
def generate(source_path, base):
all_entities = {}
def build(path, level=0):
last = path.split("/")[-1]
for i in os.listdir(path):
try:
if not i.startswith("__"):
build("/".join([path, i]), level=level + 1)
except NotADirectoryError:
with open(path + "/" + i, encoding="utf-8") as f:
p = ast.parse(f.read())
for node in ast.walk(p):
if isinstance(node, ast.ClassDef):
name = node.name
break
else:
continue
full_path = os.path.basename(path) + "/" + snek(name).replace("_", "-") + ".rst"
if level:
full_path = base + "/" + full_path
namespace = path.split("/")[-1]
if namespace in ["base", "types", "functions"]:
namespace = ""
full_name = f"{(namespace + '.') if namespace else ''}{name}"
os.makedirs(os.path.dirname(DESTINATION + "/" + full_path), exist_ok=True)
with open(DESTINATION + "/" + full_path, "w", encoding="utf-8") as f:
f.write(
page_template.format(
title=full_name,
title_markup="=" * len(full_name),
full_class_path="pyrogram.raw.{}".format(
".".join(full_path.split("/")[:-1]) + "." + name
)
)
)
if last not in all_entities:
all_entities[last] = []
all_entities[last].append(name)
build(source_path)
for k, v in sorted(all_entities.items()):
v = sorted(v)
entities = []
for i in v:
entities.append(f'{i} <{snek(i).replace("_", "-")}>')
if k != base:
inner_path = base + "/" + k + "/index" + ".rst"
module = "pyrogram.raw.{}.{}".format(base, k)
else:
for i in sorted(list(all_entities), reverse=True):
if i != base:
entities.insert(0, "{0}/index".format(i))
inner_path = base + "/index" + ".rst"
module = "pyrogram.raw.{}".format(base)
with open(DESTINATION + "/" + inner_path, "w", encoding="utf-8") as f:
if k == base:
f.write(":tocdepth: 1\n\n")
k = "Raw " + k
f.write(
toctree.format(
title=k.title(),
title_markup="=" * len(k),
module=module,
entities="\n ".join(entities)
)
)
f.write("\n")
def pyrogram_api():
def get_title_list(s: str) -> list:
return [i.strip() for i in [j.strip() for j in s.split("\n") if j] if i]
# Methods
categories = dict(
utilities="""
Utilities
run
start
stop
restart
export_session_string
add_handler
remove_handler
stop_transmission
set_parse_mode
""",
authorization="""
Authorization
initialize
sign_up
accept_terms_of_service
sign_in
sign_in_bot
connect
send_code
resend_code
recover_password
send_recovery_code
get_password_hint
check_password
log_out
disconnect
terminate
get_me
get_active_sessions
terminate_session
terminate_all_other_sessions
""",
messages="""
Messages
send_message
forward_messages
copy_message
send_photo
send_audio
send_document
send_video
send_animation
send_voice
send_video_note
send_cached_media
send_paid_media
send_media_group
get_media_group
copy_media_group
send_location
send_venue
send_contact
send_poll
send_dice
send_chat_action
set_reaction
download_media
stream_media
edit_message_text
edit_inline_text
edit_message_caption
edit_inline_caption
edit_message_media
edit_inline_media
edit_message_reply_markup
edit_inline_reply_markup
edit_cached_media
stop_poll
delete_messages
get_chat_sponsored_messages
get_chat_history
get_chat_history_count
read_chat_history
get_messages
view_messages
get_discussion_message
get_discussion_replies
get_discussion_replies_count
search_global
search_global_count
search_messages
search_messages_count
search_public_messages_by_tag
count_public_messages_by_tag
vote_poll
retract_vote
translate_text
translate_message_text
""",
chats="""
Chats
ban_chat_member
unban_chat_member
restrict_chat_member
promote_chat_member
set_administrator_title
set_chat_permissions
set_chat_photo
delete_chat_photo
set_chat_title
set_chat_description
pin_chat_message
unpin_chat_message
unpin_all_chat_messages
search_chats
join_chat
leave_chat
get_chat
get_chat_members
get_chat_members_count
get_chat_member
get_dialogs
get_dialogs_count
set_chat_username
get_nearby_chats
archive_chats
unarchive_chats
add_chat_members
create_channel
create_group
create_supergroup
delete_channel
delete_supergroup
delete_user_history
set_slow_mode
set_chat_message_auto_delete_time
mark_chat_unread
get_chat_event_log
get_chat_online_count
get_send_as_chats
set_send_as_chat
set_chat_protected_content
get_created_chats
transfer_chat_ownership
""",
invite_links="""
Invite Links
export_chat_invite_link
create_chat_invite_link
edit_chat_invite_link
revoke_chat_invite_link
get_chat_admin_invite_links
get_chat_admin_invite_links_count
get_chat_admins_with_invite_links
get_chat_invite_link
get_chat_invite_link_joiners
get_chat_invite_link_joiners_count
delete_chat_invite_link
delete_chat_admin_invite_links
get_chat_join_requests
approve_chat_join_request
approve_all_chat_join_requests
decline_chat_join_request
decline_all_chat_join_requests
""",
chat_topics="""
Chat Forum Topics
get_forum_topic_icon_stickers
create_forum_topic
edit_forum_topic
close_forum_topic
reopen_forum_topic
delete_forum_topic
hide_forum_topic
unhide_forum_topic
get_forum_topics
get_forum_topic
""",
users="""
Users
get_chat_photos
get_chat_photos_count
get_users
set_profile_photo
delete_profile_photos
set_username
update_profile
block_user
unblock_user
get_common_chats
get_default_emoji_statuses
set_emoji_status
set_birthdate
set_personal_chat
delete_account
update_status
""",
contacts="""
Contacts
add_contact
delete_contacts
import_contacts
get_contacts
get_contacts_count
""",
password="""
Password
enable_cloud_password
change_cloud_password
remove_cloud_password
""",
bots="""
Bots
answer_callback_query
request_callback_answer
set_bot_commands
delete_bot_commands
get_bot_commands
set_bot_name
get_bot_name
set_bot_info_description
get_bot_info_description
set_bot_info_short_description
get_bot_info_short_description
set_chat_menu_button
get_chat_menu_button
set_bot_default_privileges
get_bot_default_privileges
send_game
set_game_score
get_game_high_scores
answer_inline_query
get_inline_bot_results
send_inline_bot_result
answer_web_app_query
send_web_app_custom_request
""",
phone="""
Phone
create_video_chat
discard_group_call
get_video_chat_rtmp_url
invite_group_call_participants
load_group_call_participants
""",
stickers="""
Stickers
send_sticker
get_custom_emoji_stickers
get_message_effects
get_stickers
""",
stories="""
Stories
get_stories
""",
payments="""
Payments
send_invoice
create_invoice_link
answer_shipping_query
answer_pre_checkout_query
refund_star_payment
get_business_connection
get_collectible_item_info
""",
advanced="""
Advanced
invoke
resolve_peer
get_file
save_file
"""
)
root = PYROGRAM_API_DEST + "/methods"
shutil.rmtree(root, ignore_errors=True)
os.mkdir(root)
with open(HOME + "/template/methods.rst") as f:
template = f.read()
with open(root + "/index.rst", "w") as f:
fmt_keys = {}
for k, v in categories.items():
name, *methods = get_title_list(v)
fmt_keys.update({k: "\n ".join("{0} <{0}>".format(m) for m in methods)})
for method in methods:
with open(root + "/{}.rst".format(method), "w") as f2:
title = "{}()".format(method)
f2.write(title + "\n" + "=" * len(title) + "\n\n")
f2.write(".. automethod:: pyrogram.Client.{}()".format(method))
functions = ["idle", "compose"]
for func in functions:
with open(root + "/{}.rst".format(func), "w") as f2:
title = "{}()".format(func)
f2.write(title + "\n" + "=" * len(title) + "\n\n")
f2.write(".. autofunction:: pyrogram.{}()".format(func))
f.write(template.format(**fmt_keys))
# Types
categories = dict(
users_chats="""
Users & Chats
Birthdate
User
Chat
Username
ChatShared
UsersShared
ChatAdminWithInviteLinks
ChatColor
ChatEvent
ChatEventFilter
ChatInviteLink
ChatJoiner
ChatJoinRequest
ChatMember
ChatMemberUpdated
ChatPermissions
ChatPhoto
ChatPrivileges
ChatReactions
VideoChatScheduled
VideoChatStarted
VideoChatEnded
VideoChatParticipantsInvited
Dialog
EmojiStatus
GroupCallParticipant
InviteLinkImporter
Restriction
RtmpUrl
""",
messages_media="""
Messages & Media
Message
MessageEntity
TextQuote
ExternalReplyInfo
ReplyParameters
MessageOrigin
MessageOriginUser
MessageOriginHiddenUser
MessageOriginChat
MessageOriginChannel
MessageImportInfo
Photo
Animation
Audio
Document
Story
Video
VideoNote
Voice
PaidMediaInfo
PaidMedia
PaidMediaPreview
PaidMediaPhoto
PaidMediaVideo
Contact
Dice
PollOption
InputPollOption
Poll
Location
Venue
WebAppData
MessageAutoDeleteTimerChanged
ChatBoostAdded
ChatBackground
Game
GiftCode
GiftedPremium
GiftedStars
Giveaway
GiveawayCompleted
GiveawayWinners
MessageEffect
MessageReactionCountUpdated
MessageReactionUpdated
MessageReactions
Reaction
ReactionCount
ReactionType
ReactionTypeEmoji
ReactionTypeCustomEmoji
Thumbnail
TranslatedText
StrippedThumbnail
SponsoredMessage
Sticker
WebPage
""",
chat_topics="""
Chat Forum Topics
ForumTopic
ForumTopicCreated
ForumTopicClosed
ForumTopicEdited
ForumTopicReopened
GeneralForumTopicHidden
GeneralForumTopicUnhidden
""",
bot_commands="""
Bot Commands
BotCommand
BotCommandScope
BotCommandScopeAllChatAdministrators
BotCommandScopeAllGroupChats
BotCommandScopeAllPrivateChats
BotCommandScopeChat
BotCommandScopeChatAdministrators
BotCommandScopeChatMember
BotCommandScopeDefault
""",
bot_keyboards="""
Bot keyboards
CallbackGame
CallbackQuery
ForceReply
GameHighScore
InlineKeyboardButton
InlineKeyboardMarkup
KeyboardButton
KeyboardButtonPollType
KeyboardButtonPollTypeRegular
KeyboardButtonPollTypeQuiz
KeyboardButtonRequestChat
KeyboardButtonRequestUsers
ReplyKeyboardMarkup
ReplyKeyboardRemove
LoginUrl
WebAppInfo
MenuButton
MenuButtonCommands
MenuButtonWebApp
MenuButtonDefault
SentWebAppMessage
SwitchInlineQueryChosenChat
""",
inline_mode="""
Inline Mode
ChosenInlineResult
InlineQuery
InlineQueryResult
InlineQueryResultCachedAnimation
InlineQueryResultCachedAudio
InlineQueryResultCachedDocument
InlineQueryResultCachedPhoto
InlineQueryResultCachedSticker
InlineQueryResultCachedVideo
InlineQueryResultCachedVoice
InlineQueryResultAnimation
InlineQueryResultAudio
InlineQueryResultDocument
InlineQueryResultPhoto
InlineQueryResultVideo
InlineQueryResultVoice
InlineQueryResultArticle
InlineQueryResultContact
InlineQueryResultLocation
InlineQueryResultVenue
""",
authorization="""
Authorization
ActiveSession
ActiveSessions
SentCode
TermsOfService
""",
input_media="""
Input Media
InputMedia
InputMediaPhoto
InputMediaVideo
InputMediaAudio
InputMediaAnimation
InputMediaDocument
InputPhoneContact
LinkPreviewOptions
""",
input_paid_media="""
Input Paid Media
InputPaidMedia
InputPaidMediaPhoto
InputPaidMediaVideo
""",
input_message_content="""
InputMessageContent
InputMessageContent
InputTextMessageContent
InputLocationMessageContent
InputVenueMessageContent
InputContactMessageContent
InputInvoiceMessageContent
""",
payments="""
Payments
BusinessConnection
BusinessIntro
BusinessLocation
BusinessOpeningHours
BusinessOpeningHoursInterval
CollectibleItemInfo
LabeledPrice
Invoice
ShippingAddress
OrderInfo
ShippingOption
SuccessfulPayment
RefundedPayment
ShippingQuery
PreCheckoutQuery
"""
)
root = PYROGRAM_API_DEST + "/types"
shutil.rmtree(root, ignore_errors=True)
os.mkdir(root)
with open(HOME + "/template/types.rst") as f:
template = f.read()
with open(root + "/index.rst", "w") as f:
fmt_keys = {}
for k, v in categories.items():
name, *types = get_title_list(v)
fmt_keys.update({k: "\n ".join(types)})
# noinspection PyShadowingBuiltins
for type in types:
with open(root + "/{}.rst".format(type), "w") as f2:
title = "{}".format(type)
f2.write(title + "\n" + "=" * len(title) + "\n\n")
f2.write(".. autoclass:: pyrogram.types.{}()\n".format(type))
f.write(template.format(**fmt_keys))
# Bound Methods
categories = dict(
message="""
Message
Message.click
Message.delete
Message.download
Message.forward
Message.copy
Message.pin
Message.unpin
Message.edit
Message.edit_text
Message.edit_cached_media
Message.edit_caption
Message.edit_media
Message.edit_reply_markup
Message.reply
Message.reply_text
Message.reply_animation
Message.reply_audio
Message.reply_cached_media
Message.reply_chat_action
Message.reply_contact
Message.reply_document
Message.reply_game
Message.reply_inline_bot_result
Message.reply_location
Message.reply_media_group
Message.reply_photo
Message.reply_poll
Message.reply_sticker
Message.reply_venue
Message.reply_video
Message.reply_video_note
Message.reply_voice
Message.get_media_group
Message.react
Message.read
Message.view
Message.translate
""",
chat="""
Chat
Chat.archive
Chat.unarchive
Chat.set_title
Chat.set_description
Chat.set_photo
Chat.ban_member
Chat.unban_member
Chat.restrict_member
Chat.promote_member
Chat.get_member
Chat.get_members
Chat.add_members
Chat.join
Chat.leave
Chat.mark_unread
Chat.set_protected_content
Chat.unpin_all_messages
Chat.set_message_auto_delete_time
""",
user="""
User
User.archive
User.unarchive
User.block
User.unblock
""",
callback_query="""
Callback Query
CallbackQuery.answer
CallbackQuery.edit_message_text
CallbackQuery.edit_message_caption
CallbackQuery.edit_message_media
CallbackQuery.edit_message_reply_markup
""",
inline_query="""
InlineQuery
InlineQuery.answer
""",
pre_checkout_query="""
PreCheckoutQuery
PreCheckoutQuery.answer
""",
shipping_query="""
ShippingQuery
ShippingQuery.answer
""",
chat_join_request="""
ChatJoinRequest
ChatJoinRequest.approve
ChatJoinRequest.decline
""",
story="""
Story
Story.react
Story.download
""",
active_session="""
ActiveSession
ActiveSession.terminate
""",
)
root = PYROGRAM_API_DEST + "/bound-methods"
shutil.rmtree(root, ignore_errors=True)
os.mkdir(root)
with open(HOME + "/template/bound-methods.rst") as f:
template = f.read()
with open(root + "/index.rst", "w") as f:
fmt_keys = {}
for k, v in categories.items():
name, *bound_methods = get_title_list(v)
fmt_keys.update({"{}_hlist".format(k): "\n ".join("- :meth:`~{}`".format(bm) for bm in bound_methods)})
fmt_keys.update(
{"{}_toctree".format(k): "\n ".join("{} <{}>".format(bm.split(".")[1], bm) for bm in bound_methods)})
# noinspection PyShadowingBuiltins
for bm in bound_methods:
with open(root + "/{}.rst".format(bm), "w") as f2:
title = "{}()".format(bm)
f2.write(title + "\n" + "=" * len(title) + "\n\n")
f2.write(".. automethod:: pyrogram.types.{}()".format(bm))
f.write(template.format(**fmt_keys))
def start():
global page_template
global toctree
shutil.rmtree(DESTINATION, ignore_errors=True)
with open(HOME + "/template/page.txt", encoding="utf-8") as f:
page_template = f.read()
with open(HOME + "/template/toctree.txt", encoding="utf-8") as f:
toctree = f.read()
generate(TYPES_PATH, TYPES_BASE)
generate(FUNCTIONS_PATH, FUNCTIONS_BASE)
generate(BASE_PATH, BASE_BASE)
pyrogram_api()
if "__main__" == __name__:
FUNCTIONS_PATH = "../../pyrogram/raw/functions"
TYPES_PATH = "../../pyrogram/raw/types"
BASE_PATH = "../../pyrogram/raw/base"
HOME = "."
DESTINATION = "../../docs/source/telegram"
PYROGRAM_API_DEST = "../../docs/source/api"
start()