Skip to content

Commit cfd756b

Browse files
committed
Add log_out method
1 parent c7782b1 commit cfd756b

5 files changed

Lines changed: 37 additions & 2 deletions

File tree

compiler/docs/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ def get_title_list(s: str) -> list:
264264
send_recovery_code
265265
recover_password
266266
accept_terms_of_service
267+
log_out
267268
""",
268269
advanced="""
269270
Advanced

pyrogram/client/client.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,10 @@ def __enter__(self):
222222
return self.start()
223223

224224
def __exit__(self, *args):
225-
self.stop()
225+
try:
226+
self.stop()
227+
except ConnectionError:
228+
pass
226229

227230
@property
228231
def proxy(self):
@@ -773,6 +776,27 @@ def authorize(self) -> User:
773776

774777
return signed_up
775778

779+
def log_out(self):
780+
"""Log out from Telegram and delete the *\\*.session* file.
781+
782+
When you log out, the current client is stopped and the storage session destroyed.
783+
No more API calls can be made until you start the client and re-authorize again.
784+
785+
Returns:
786+
``bool``: On success, True is returned.
787+
788+
Example:
789+
.. code-block:: python
790+
791+
# Log out.
792+
app.log_out()
793+
"""
794+
self.send(functions.auth.LogOut())
795+
self.stop()
796+
self.storage.destroy()
797+
798+
return True
799+
776800
def start(self):
777801
"""Start the client.
778802
@@ -1338,7 +1362,7 @@ def updates_worker(self):
13381362
elif isinstance(updates, types.UpdateShort):
13391363
self.dispatcher.updates_queue.put((updates.update, {}, {}))
13401364
elif isinstance(updates, types.UpdatesTooLong):
1341-
log.warning(updates)
1365+
log.info(updates)
13421366
except Exception as e:
13431367
log.error(e, exc_info=True)
13441368

pyrogram/client/storage/file_storage.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import base64
2020
import json
2121
import logging
22+
import os
2223
import sqlite3
2324
from pathlib import Path
2425
from threading import Lock
@@ -108,3 +109,6 @@ def open(self):
108109

109110
with self.conn:
110111
self.conn.execute("VACUUM")
112+
113+
def destroy(self):
114+
os.remove(self.database)

pyrogram/client/storage/memory_storage.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ def close(self):
9797
with self.lock:
9898
self.conn.close()
9999

100+
def destroy(self):
101+
pass
102+
100103
def update_peers(self, peers: List[Tuple[int, int, str, str, str]]):
101104
with self.lock:
102105
self.conn.executemany(

pyrogram/client/storage/storage.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ def save(self):
3030
def close(self):
3131
raise NotImplementedError
3232

33+
def destroy(self):
34+
raise NotImplementedError
35+
3336
def update_peers(self, peers):
3437
raise NotImplementedError
3538

0 commit comments

Comments
 (0)