Add log_out method

This commit is contained in:
Dan 2019-09-08 11:58:34 +02:00
parent c7782b146f
commit cfd756bd24
5 changed files with 37 additions and 2 deletions

View File

@ -264,6 +264,7 @@ def pyrogram_api():
send_recovery_code send_recovery_code
recover_password recover_password
accept_terms_of_service accept_terms_of_service
log_out
""", """,
advanced=""" advanced="""
Advanced Advanced

View File

@ -222,7 +222,10 @@ class Client(Methods, BaseClient):
return self.start() return self.start()
def __exit__(self, *args): def __exit__(self, *args):
self.stop() try:
self.stop()
except ConnectionError:
pass
@property @property
def proxy(self): def proxy(self):
@ -773,6 +776,27 @@ class Client(Methods, BaseClient):
return signed_up return signed_up
def log_out(self):
"""Log out from Telegram and delete the *\\*.session* file.
When you log out, the current client is stopped and the storage session destroyed.
No more API calls can be made until you start the client and re-authorize again.
Returns:
``bool``: On success, True is returned.
Example:
.. code-block:: python
# Log out.
app.log_out()
"""
self.send(functions.auth.LogOut())
self.stop()
self.storage.destroy()
return True
def start(self): def start(self):
"""Start the client. """Start the client.
@ -1338,7 +1362,7 @@ class Client(Methods, BaseClient):
elif isinstance(updates, types.UpdateShort): elif isinstance(updates, types.UpdateShort):
self.dispatcher.updates_queue.put((updates.update, {}, {})) self.dispatcher.updates_queue.put((updates.update, {}, {}))
elif isinstance(updates, types.UpdatesTooLong): elif isinstance(updates, types.UpdatesTooLong):
log.warning(updates) log.info(updates)
except Exception as e: except Exception as e:
log.error(e, exc_info=True) log.error(e, exc_info=True)

View File

@ -19,6 +19,7 @@
import base64 import base64
import json import json
import logging import logging
import os
import sqlite3 import sqlite3
from pathlib import Path from pathlib import Path
from threading import Lock from threading import Lock
@ -108,3 +109,6 @@ class FileStorage(MemoryStorage):
with self.conn: with self.conn:
self.conn.execute("VACUUM") self.conn.execute("VACUUM")
def destroy(self):
os.remove(self.database)

View File

@ -97,6 +97,9 @@ class MemoryStorage(Storage):
with self.lock: with self.lock:
self.conn.close() self.conn.close()
def destroy(self):
pass
def update_peers(self, peers: List[Tuple[int, int, str, str, str]]): def update_peers(self, peers: List[Tuple[int, int, str, str, str]]):
with self.lock: with self.lock:
self.conn.executemany( self.conn.executemany(

View File

@ -30,6 +30,9 @@ class Storage:
def close(self): def close(self):
raise NotImplementedError raise NotImplementedError
def destroy(self):
raise NotImplementedError
def update_peers(self, peers): def update_peers(self, peers):
raise NotImplementedError raise NotImplementedError