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
recover_password
accept_terms_of_service
log_out
""",
advanced="""
Advanced

View File

@ -222,7 +222,10 @@ class Client(Methods, BaseClient):
return self.start()
def __exit__(self, *args):
try:
self.stop()
except ConnectionError:
pass
@property
def proxy(self):
@ -773,6 +776,27 @@ class Client(Methods, BaseClient):
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):
"""Start the client.
@ -1338,7 +1362,7 @@ class Client(Methods, BaseClient):
elif isinstance(updates, types.UpdateShort):
self.dispatcher.updates_queue.put((updates.update, {}, {}))
elif isinstance(updates, types.UpdatesTooLong):
log.warning(updates)
log.info(updates)
except Exception as e:
log.error(e, exc_info=True)

View File

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

View File

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

View File

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