From 033622cfb85efbd8a09abf8b0949f9ccc0495b90 Mon Sep 17 00:00:00 2001 From: bakatrouble Date: Wed, 27 Feb 2019 22:49:23 +0300 Subject: [PATCH] Cleanup json session storage specific code as it is used only for migrations --- pyrogram/client/ext/syncer.py | 2 - pyrogram/client/session_storage/abstract.py | 4 -- pyrogram/client/session_storage/json.py | 67 +------------------ pyrogram/client/session_storage/memory.py | 3 - .../client/session_storage/sqlite/__init__.py | 3 - pyrogram/client/session_storage/string.py | 3 - 6 files changed, 1 insertion(+), 81 deletions(-) diff --git a/pyrogram/client/ext/syncer.py b/pyrogram/client/ext/syncer.py index e13212be..9e7d2303 100644 --- a/pyrogram/client/ext/syncer.py +++ b/pyrogram/client/ext/syncer.py @@ -88,5 +88,3 @@ class Syncer: log.critical(e, exc_info=True) else: log.info("Synced {}".format(client.session_name)) - finally: - client.session_storage.sync_cleanup() diff --git a/pyrogram/client/session_storage/abstract.py b/pyrogram/client/session_storage/abstract.py index 39517a01..134d5c8c 100644 --- a/pyrogram/client/session_storage/abstract.py +++ b/pyrogram/client/session_storage/abstract.py @@ -38,10 +38,6 @@ class SessionStorage(abc.ABC): @abc.abstractmethod def save(self, sync=False): ... - - @abc.abstractmethod - def sync_cleanup(self): - ... @property @abc.abstractmethod diff --git a/pyrogram/client/session_storage/json.py b/pyrogram/client/session_storage/json.py index 570e1525..4a48d3c1 100644 --- a/pyrogram/client/session_storage/json.py +++ b/pyrogram/client/session_storage/json.py @@ -59,70 +59,5 @@ class JsonSessionStorage(MemorySessionStorage): self._date = s.get("date", 0) self._is_bot = s.get('is_bot', self._is_bot) - for k, v in s.get("peers_by_id", {}).items(): - self._peers_cache['i' + k] = utils.get_input_peer(int(k), v) - - for k, v in s.get("peers_by_username", {}).items(): - try: - self._peers_cache['u' + k] = self.get_peer_by_id(v) - except KeyError: - pass - - for k, v in s.get("peers_by_phone", {}).items(): - try: - self._peers_cache['p' + k] = self.get_peer_by_id(v) - except KeyError: - pass - def save(self, sync=False): - file_path = self._get_file_name(self._session_name) - - if sync: - file_path += '.tmp' - - log.info('Saving JSON session to {}, sync={}'.format(file_path, sync)) - - auth_key = base64.b64encode(self._auth_key).decode() - auth_key = [auth_key[i: i + 43] for i in range(0, len(auth_key), 43)] # split key in lines of 43 chars - - os.makedirs(self._client.workdir, exist_ok=True) - - data = { - 'dc_id': self._dc_id, - 'test_mode': self._test_mode, - 'auth_key': auth_key, - 'user_id': self._user_id, - 'date': self._date, - 'is_bot': self._is_bot, - 'peers_by_id': { - k[1:]: getattr(v, "access_hash", None) - for k, v in self._peers_cache.copy().items() - if k[0] == 'i' - }, - 'peers_by_username': { - k[1:]: utils.get_peer_id(v) - for k, v in self._peers_cache.copy().items() - if k[0] == 'u' - }, - 'peers_by_phone': { - k[1:]: utils.get_peer_id(v) - for k, v in self._peers_cache.copy().items() - if k[0] == 'p' - } - } - - with open(file_path, "w", encoding="utf-8") as f: - json.dump(data, f, indent=4) - - f.flush() - os.fsync(f.fileno()) - - # execution won't be here if an error has occurred earlier - if sync: - shutil.move(file_path, self._get_file_name(self._session_name)) - - def sync_cleanup(self): - try: - os.remove(self._get_file_name(self._session_name) + '.tmp') - except OSError: - pass + pass diff --git a/pyrogram/client/session_storage/memory.py b/pyrogram/client/session_storage/memory.py index d5f92f0d..c0610e70 100644 --- a/pyrogram/client/session_storage/memory.py +++ b/pyrogram/client/session_storage/memory.py @@ -20,9 +20,6 @@ class MemorySessionStorage(SessionStorage): def save(self, sync=False): pass - def sync_cleanup(self): - pass - @property def dc_id(self): return self._dc_id diff --git a/pyrogram/client/session_storage/sqlite/__init__.py b/pyrogram/client/session_storage/sqlite/__init__.py index 4fc7ff64..0308a4dc 100644 --- a/pyrogram/client/session_storage/sqlite/__init__.py +++ b/pyrogram/client/session_storage/sqlite/__init__.py @@ -146,6 +146,3 @@ class SQLiteSessionStorage(MemorySessionStorage): self._conn.execute('insert into sessions values (?, ?, ?, ?, ?, ?)', (self._dc_id, self._test_mode, self._auth_key, self._user_id, self._date, self._is_bot)) self._conn.commit() - - def sync_cleanup(self): - pass diff --git a/pyrogram/client/session_storage/string.py b/pyrogram/client/session_storage/string.py index f8ec740a..11051323 100644 --- a/pyrogram/client/session_storage/string.py +++ b/pyrogram/client/session_storage/string.py @@ -44,6 +44,3 @@ class StringSessionStorage(MemorySessionStorage): encoded = ':' + base64.b64encode(packed, b'-_').decode('latin-1').rstrip('=') split = '\n'.join(['"{}"'.format(encoded[i: i + 50]) for i in range(0, len(encoded), 50)]) print('Created session string:\n{}'.format(split)) - - def sync_cleanup(self): - pass