From cfd756bd2439f504a6ca7136ced035cb6e502116 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 8 Sep 2019 11:58:34 +0200 Subject: [PATCH] Add log_out method --- compiler/docs/compiler.py | 1 + pyrogram/client/client.py | 28 +++++++++++++++++++++-- pyrogram/client/storage/file_storage.py | 4 ++++ pyrogram/client/storage/memory_storage.py | 3 +++ pyrogram/client/storage/storage.py | 3 +++ 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 34db202f..93e71da6 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -264,6 +264,7 @@ def pyrogram_api(): send_recovery_code recover_password accept_terms_of_service + log_out """, advanced=""" Advanced diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index eae20e50..993af2f0 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -222,7 +222,10 @@ class Client(Methods, BaseClient): return self.start() def __exit__(self, *args): - self.stop() + 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) diff --git a/pyrogram/client/storage/file_storage.py b/pyrogram/client/storage/file_storage.py index e6ba8420..e367b447 100644 --- a/pyrogram/client/storage/file_storage.py +++ b/pyrogram/client/storage/file_storage.py @@ -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) diff --git a/pyrogram/client/storage/memory_storage.py b/pyrogram/client/storage/memory_storage.py index e69d247f..b24fce38 100644 --- a/pyrogram/client/storage/memory_storage.py +++ b/pyrogram/client/storage/memory_storage.py @@ -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( diff --git a/pyrogram/client/storage/storage.py b/pyrogram/client/storage/storage.py index e0810645..cd6438d4 100644 --- a/pyrogram/client/storage/storage.py +++ b/pyrogram/client/storage/storage.py @@ -30,6 +30,9 @@ class Storage: def close(self): raise NotImplementedError + def destroy(self): + raise NotImplementedError + def update_peers(self, peers): raise NotImplementedError