Skip to content

Commit 928ce5d

Browse files
committed
Merge develop -> asyncio
2 parents 8f0b8ba + a015f99 commit 928ce5d

23 files changed

Lines changed: 141 additions & 91 deletions

pyrogram/client/client.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
from .storage import Storage, FileStorage, MemoryStorage
4949
from .types import User, SentCode, TermsOfService
5050

51+
log = logging.getLogger(__name__)
52+
5153

5254
class Client(Methods, BaseClient):
5355
"""Pyrogram Client, the main means for interacting with Telegram.
@@ -340,7 +342,7 @@ async def terminate(self):
340342

341343
if self.takeout_id:
342344
await self.send(functions.account.FinishTakeoutSession())
343-
logging.warning("Takeout session {} finished".format(self.takeout_id))
345+
log.warning("Takeout session {} finished".format(self.takeout_id))
344346

345347
await Syncer.remove(self)
346348
await self.dispatcher.stop()
@@ -733,7 +735,7 @@ async def authorize(self) -> User:
733735
print(e.MESSAGE.format(x=e.x))
734736
time.sleep(e.x)
735737
except Exception as e:
736-
logging.error(e, exc_info=True)
738+
log.error(e, exc_info=True)
737739
raise
738740
else:
739741
self.password = None
@@ -833,7 +835,7 @@ async def start(self):
833835

834836
if not self.storage.is_bot and self.takeout:
835837
self.takeout_id = (await self.send(functions.account.InitTakeoutSession())).id
836-
logging.warning("Takeout session {} initiated".format(self.takeout_id))
838+
log.warning("Takeout session {} initiated".format(self.takeout_id))
837839

838840
await self.send(functions.updates.GetState())
839841
except (Exception, KeyboardInterrupt):
@@ -1273,7 +1275,7 @@ async def download_worker(self):
12731275
os.makedirs(directory, exist_ok=True)
12741276
shutil.move(temp_file_path, final_file_path)
12751277
except Exception as e:
1276-
logging.error(e, exc_info=True)
1278+
log.error(e, exc_info=True)
12771279

12781280
try:
12791281
os.remove(temp_file_path)
@@ -1314,7 +1316,7 @@ async def updates_worker(self):
13141316
pts_count = getattr(update, "pts_count", None)
13151317

13161318
if isinstance(update, types.UpdateChannelTooLong):
1317-
logging.warning(update)
1319+
log.warning(update)
13181320

13191321
if isinstance(update, types.UpdateNewChannelMessage) and is_min:
13201322
message = update.message
@@ -1366,9 +1368,9 @@ async def updates_worker(self):
13661368
elif isinstance(updates, types.UpdateShort):
13671369
self.dispatcher.updates_queue.put_nowait((updates.update, {}, {}))
13681370
elif isinstance(updates, types.UpdatesTooLong):
1369-
logging.info(updates)
1371+
log.info(updates)
13701372
except Exception as e:
1371-
logging.error(e, exc_info=True)
1373+
log.error(e, exc_info=True)
13721374

13731375
async def send(self, data: TLObject, retries: int = Session.MAX_RETRIES, timeout: float = Session.WAIT_TIMEOUT):
13741376
"""Send raw Telegram queries.
@@ -1543,7 +1545,7 @@ def load_plugins(self):
15431545
if isinstance(handler, Handler) and isinstance(group, int):
15441546
self.add_handler(handler, group)
15451547

1546-
logging.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
1548+
log.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
15471549
self.session_name, type(handler).__name__, name, group, module_path))
15481550

15491551
count += 1
@@ -1557,12 +1559,12 @@ def load_plugins(self):
15571559
try:
15581560
module = import_module(module_path)
15591561
except ImportError:
1560-
logging.warning('[{}] [LOAD] Ignoring non-existent module "{}"'.format(
1562+
log.warning('[{}] [LOAD] Ignoring non-existent module "{}"'.format(
15611563
self.session_name, module_path))
15621564
continue
15631565

15641566
if "__path__" in dir(module):
1565-
logging.warning('[{}] [LOAD] Ignoring namespace "{}"'.format(
1567+
log.warning('[{}] [LOAD] Ignoring namespace "{}"'.format(
15661568
self.session_name, module_path))
15671569
continue
15681570

@@ -1578,13 +1580,13 @@ def load_plugins(self):
15781580
if isinstance(handler, Handler) and isinstance(group, int):
15791581
self.add_handler(handler, group)
15801582

1581-
logging.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
1583+
log.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
15821584
self.session_name, type(handler).__name__, name, group, module_path))
15831585

15841586
count += 1
15851587
except Exception:
15861588
if warn_non_existent_functions:
1587-
logging.warning('[{}] [LOAD] Ignoring non-existent function "{}" from "{}"'.format(
1589+
log.warning('[{}] [LOAD] Ignoring non-existent function "{}" from "{}"'.format(
15881590
self.session_name, name, module_path))
15891591

15901592
if exclude:
@@ -1595,12 +1597,12 @@ def load_plugins(self):
15951597
try:
15961598
module = import_module(module_path)
15971599
except ImportError:
1598-
logging.warning('[{}] [UNLOAD] Ignoring non-existent module "{}"'.format(
1600+
log.warning('[{}] [UNLOAD] Ignoring non-existent module "{}"'.format(
15991601
self.session_name, module_path))
16001602
continue
16011603

16021604
if "__path__" in dir(module):
1603-
logging.warning('[{}] [UNLOAD] Ignoring namespace "{}"'.format(
1605+
log.warning('[{}] [UNLOAD] Ignoring namespace "{}"'.format(
16041606
self.session_name, module_path))
16051607
continue
16061608

@@ -1616,20 +1618,20 @@ def load_plugins(self):
16161618
if isinstance(handler, Handler) and isinstance(group, int):
16171619
self.remove_handler(handler, group)
16181620

1619-
logging.info('[{}] [UNLOAD] {}("{}") from group {} in "{}"'.format(
1621+
log.info('[{}] [UNLOAD] {}("{}") from group {} in "{}"'.format(
16201622
self.session_name, type(handler).__name__, name, group, module_path))
16211623

16221624
count -= 1
16231625
except Exception:
16241626
if warn_non_existent_functions:
1625-
logging.warning('[{}] [UNLOAD] Ignoring non-existent function "{}" from "{}"'.format(
1627+
log.warning('[{}] [UNLOAD] Ignoring non-existent function "{}" from "{}"'.format(
16261628
self.session_name, name, module_path))
16271629

16281630
if count > 0:
1629-
logging.warning('[{}] Successfully loaded {} plugin{} from "{}"'.format(
1631+
log.warning('[{}] Successfully loaded {} plugin{} from "{}"'.format(
16301632
self.session_name, count, "s" if count > 1 else "", root))
16311633
else:
1632-
logging.warning('[{}] No plugin loaded from "{}"'.format(
1634+
log.warning('[{}] No plugin loaded from "{}"'.format(
16331635
self.session_name, root))
16341636

16351637
# def get_initial_dialogs_chunk(self, offset_date: int = 0):
@@ -1646,10 +1648,10 @@ def load_plugins(self):
16461648
# )
16471649
# )
16481650
# except FloodWait as e:
1649-
# logging.warning("get_dialogs flood: waiting {} seconds".format(e.x))
1651+
# log.warning("get_dialogs flood: waiting {} seconds".format(e.x))
16501652
# time.sleep(e.x)
16511653
# else:
1652-
# logging.info("Total peers: {}".format(self.storage.peers_count))
1654+
# log.info("Total peers: {}".format(self.storage.peers_count))
16531655
# return r
16541656
#
16551657
# def get_initial_dialogs(self):
@@ -1885,7 +1887,7 @@ async def worker(session):
18851887
except Client.StopTransmission:
18861888
raise
18871889
except Exception as e:
1888-
logging.error(e, exc_info=True)
1890+
log.error(e, exc_info=True)
18891891
else:
18901892
if is_big:
18911893
return types.InputFileBig(
@@ -2117,7 +2119,7 @@ async def get_file(self, media_type: int,
21172119
raise e
21182120
except Exception as e:
21192121
if not isinstance(e, Client.StopTransmission):
2120-
logging.error(e, exc_info=True)
2122+
log.error(e, exc_info=True)
21212123

21222124
try:
21232125
os.remove(file_name)

pyrogram/client/ext/dispatcher.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler
3535
)
3636

37+
log = logging.getLogger(__name__)
38+
3739

3840
class Dispatcher:
3941
NEW_MESSAGE_UPDATES = (
@@ -183,7 +185,7 @@ async def update_worker(self, lock):
183185
if handler.check(parsed_update):
184186
args = (parsed_update,)
185187
except Exception as e:
186-
logging.error(e, exc_info=True)
188+
log.error(e, exc_info=True)
187189
continue
188190

189191
elif isinstance(handler, RawUpdateHandler):
@@ -199,10 +201,10 @@ async def update_worker(self, lock):
199201
except pyrogram.ContinuePropagation:
200202
continue
201203
except Exception as e:
202-
logging.error(e, exc_info=True)
204+
log.error(e, exc_info=True)
203205

204206
break
205207
except pyrogram.StopPropagation:
206208
pass
207209
except Exception as e:
208-
logging.error(e, exc_info=True)
210+
log.error(e, exc_info=True)

pyrogram/client/ext/syncer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import logging
2121
import time
2222

23+
log = logging.getLogger(__name__)
24+
2325

2426
class Syncer:
2527
INTERVAL = 20
@@ -81,9 +83,9 @@ def sync(cls, client):
8183
start = time.time()
8284
client.storage.save()
8385
except Exception as e:
84-
logging.critical(e, exc_info=True)
86+
log.critical(e, exc_info=True)
8587
else:
86-
logging.info('Synced "{}" in {:.6} ms'.format(
88+
log.info('Synced "{}" in {:.6} ms'.format(
8789
client.storage.name,
8890
(time.time() - start) * 1000
8991
))

pyrogram/client/methods/chats/get_chat_members.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
import pyrogram
2424
from pyrogram.api import functions, types
2525
from pyrogram.errors import FloodWait
26-
2726
from ...ext import BaseClient
2827

28+
log = logging.getLogger(__name__)
29+
2930

3031
class Filters:
3132
ALL = "all"
@@ -152,7 +153,7 @@ async def get_chat_members(
152153

153154
return pyrogram.List(pyrogram.ChatMember._parse(self, member, users) for member in members)
154155
except FloodWait as e:
155-
logging.warning("Sleeping for {}s".format(e.x))
156+
log.warning("Sleeping for {}s".format(e.x))
156157
await asyncio.sleep(e.x)
157158
else:
158159
raise ValueError("The chat_id \"{}\" belongs to a user".format(chat_id))

pyrogram/client/methods/chats/get_dialogs.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
import pyrogram
2424
from pyrogram.api import functions, types
2525
from pyrogram.errors import FloodWait
26-
2726
from ...ext import BaseClient, utils
2827

28+
log = logging.getLogger(__name__)
29+
2930

3031
class GetDialogs(BaseClient):
3132
async def get_dialogs(
@@ -81,7 +82,7 @@ async def get_dialogs(
8182
)
8283
)
8384
except FloodWait as e:
84-
logging.warning("Sleeping for {}s".format(e.x))
85+
log.warning("Sleeping for {}s".format(e.x))
8586
await asyncio.sleep(e.x)
8687
else:
8788
break
@@ -110,6 +111,6 @@ async def get_dialogs(
110111
if not isinstance(dialog, types.Dialog):
111112
continue
112113

113-
parsed_dialogs.append(pyrogram.Dialogging._parse(self, dialog, messages, users, chats))
114+
parsed_dialogs.append(pyrogram.Dialog._parse(self, dialog, messages, users, chats))
114115

115116
return pyrogram.List(parsed_dialogs)

pyrogram/client/methods/contacts/get_contacts.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
from pyrogram.errors import FloodWait
2626
from ...ext import BaseClient
2727

28+
log = logging.getLogger(__name__)
29+
2830

2931
class GetContacts(BaseClient):
3032
async def get_contacts(self) -> List["pyrogram.User"]:
@@ -43,7 +45,7 @@ async def get_contacts(self) -> List["pyrogram.User"]:
4345
try:
4446
contacts = await self.send(functions.contacts.GetContacts(hash=0))
4547
except FloodWait as e:
46-
logging.warning("get_contacts flood: waiting {} seconds".format(e.x))
48+
log.warning("get_contacts flood: waiting {} seconds".format(e.x))
4749
await asyncio.sleep(e.x)
4850
else:
4951
return pyrogram.List(pyrogram.User._parse(self, user) for user in contacts.users)

pyrogram/client/methods/messages/get_history.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
from pyrogram.api import functions
2525
from pyrogram.client.ext import utils
2626
from pyrogram.errors import FloodWait
27-
2827
from ...ext import BaseClient
2928

29+
log = logging.getLogger(__name__)
30+
3031

3132
class GetHistory(BaseClient):
3233
async def get_history(
@@ -102,7 +103,7 @@ async def get_history(
102103
)
103104
)
104105
except FloodWait as e:
105-
logging.warning("Sleeping for {}s".format(e.x))
106+
log.warning("Sleeping for {}s".format(e.x))
106107
await asyncio.sleep(e.x)
107108
else:
108109
break

pyrogram/client/methods/messages/get_history_count.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
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 logging
1920
from typing import Union
2021

2122
from pyrogram.api import types, functions
2223
from pyrogram.client.ext import BaseClient
2324

25+
log = logging.getLogger(__name__)
26+
2427

2528
class GetHistoryCount(BaseClient):
2629
async def get_history_count(

pyrogram/client/methods/messages/get_messages.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
import pyrogram
2424
from pyrogram.api import functions, types
2525
from pyrogram.errors import FloodWait
26-
2726
from ...ext import BaseClient, utils
2827

28+
log = logging.getLogger(__name__)
29+
2930

3031
# TODO: Rewrite using a flag for replied messages and have message_ids non-optional
3132

@@ -115,7 +116,7 @@ async def get_messages(
115116
try:
116117
r = await self.send(rpc)
117118
except FloodWait as e:
118-
logging.warning("Sleeping for {}s".format(e.x))
119+
log.warning("Sleeping for {}s".format(e.x))
119120
await asyncio.sleep(e.x)
120121
else:
121122
break

pyrogram/client/methods/messages/send_media_group.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
from pyrogram.client.ext import BaseClient, utils
2727
from pyrogram.errors import FloodWait
2828

29+
log = logging.getLogger(__name__)
30+
2931

3032
class SendMediaGroup(BaseClient):
3133
# TODO: Add progress parameter
@@ -87,7 +89,7 @@ async def send_media_group(
8789
)
8890
)
8991
except FloodWait as e:
90-
logging.warning("Sleeping for {}s".format(e.x))
92+
log.warning("Sleeping for {}s".format(e.x))
9193
await asyncio.sleep(e.x)
9294
else:
9395
break
@@ -142,7 +144,7 @@ async def send_media_group(
142144
)
143145
)
144146
except FloodWait as e:
145-
logging.warning("Sleeping for {}s".format(e.x))
147+
log.warning("Sleeping for {}s".format(e.x))
146148
await asyncio.sleep(e.x)
147149
else:
148150
break
@@ -193,7 +195,7 @@ async def send_media_group(
193195
)
194196
)
195197
except FloodWait as e:
196-
logging.warning("Sleeping for {}s".format(e.x))
198+
log.warning("Sleeping for {}s".format(e.x))
197199
await asyncio.sleep(e.x)
198200
else:
199201
break

0 commit comments

Comments
 (0)