Make is_connected a property

This is consistent with the rest of is_ properties
This commit is contained in:
Lonami Exo 2022-02-16 11:24:20 +01:00
parent 483e2aadf1
commit 4258ce2bc8
6 changed files with 29 additions and 26 deletions

View File

@ -976,3 +976,5 @@ Now the URL is returned. You can still use ``webbrowser.open`` to get the old be
todo update send_message and send_file docs (well review all functions)
album overhaul. use a list of Message instead.
is_connected is now a property (consistent with the rest of ``is_`` properties)

View File

@ -71,7 +71,7 @@ def start(
async def _start(
self: 'TelegramClient', phone, password, bot_token,
code_callback, first_name, last_name, max_attempts):
if not self.is_connected():
if not self.is_connected:
await self.connect()
# Rather than using `is_user_authorized`, use `get_me`. While this is

View File

@ -308,12 +308,29 @@ async def connect(self: 'TelegramClient') -> None:
self._updates_handle = asyncio.create_task(self._update_loop())
def is_connected(self: 'TelegramClient') -> bool:
sender = getattr(self, '_sender', None)
return sender and sender.is_connected()
return self._sender.is_connected()
async def disconnect(self: 'TelegramClient'):
return await _disconnect_coro(self)
await _disconnect(self)
# Also clean-up all exported senders because we're done with them
async with self._borrow_sender_lock:
for state, sender in self._borrowed_senders.values():
# Note that we're not checking for `state.should_disconnect()`.
# If the user wants to disconnect the client, ALL connections
# to Telegram (including exported senders) should be closed.
#
# Disconnect should never raise, so there's no try/except.
await sender.disconnect()
# Can't use `mark_disconnected` because it may be borrowed.
state._connected = False
# If any was borrowed
self._borrowed_senders.clear()
def set_proxy(self: 'TelegramClient', proxy: typing.Union[tuple, dict]):
init_proxy = None
@ -333,24 +350,6 @@ def set_proxy(self: 'TelegramClient', proxy: typing.Union[tuple, dict]):
else:
connection._proxy = proxy
async def _disconnect_coro(self: 'TelegramClient'):
await _disconnect(self)
# Also clean-up all exported senders because we're done with them
async with self._borrow_sender_lock:
for state, sender in self._borrowed_senders.values():
# Note that we're not checking for `state.should_disconnect()`.
# If the user wants to disconnect the client, ALL connections
# to Telegram (including exported senders) should be closed.
#
# Disconnect should never raise, so there's no try/except.
await sender.disconnect()
# Can't use `mark_disconnected` because it may be borrowed.
state._connected = False
# If any was borrowed
self._borrowed_senders.clear()
async def _disconnect(self: 'TelegramClient'):
"""

View File

@ -2637,7 +2637,7 @@ class TelegramClient:
print('Failed to connect')
"""
@forward_call(telegrambaseclient.is_connected)
@property
def is_connected(self: 'TelegramClient') -> bool:
"""
Returns `True` if the user has connected.
@ -2647,9 +2647,11 @@ class TelegramClient:
Example
.. code-block:: python
while client.is_connected():
# This is a silly example - run_until_disconnected is often better suited
while client.is_connected:
await asyncio.sleep(1)
"""
return telegrambaseclient.is_connected(self)
@forward_call(telegrambaseclient.disconnect)
def disconnect(self: 'TelegramClient'):

View File

@ -168,7 +168,7 @@ async def catch_up(self: 'TelegramClient'):
async def _update_loop(self: 'TelegramClient'):
try:
updates_to_dispatch = deque()
while self.is_connected():
while self.is_connected:
if updates_to_dispatch:
await _dispatch(self, *updates_to_dispatch.popleft())
continue

View File

@ -228,7 +228,7 @@ class App(tkinter.Tk):
"""
Sends a message. Does nothing if the client is not connected.
"""
if not self.cl.is_connected():
if not self.cl.is_connected:
return
# The user needs to configure a chat where the message should be sent.