Cleanup json session storage specific code as it is used only for migrations

This commit is contained in:
bakatrouble 2019-02-27 22:49:23 +03:00
parent 10fc340eff
commit 033622cfb8
6 changed files with 1 additions and 81 deletions

View File

@ -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()

View File

@ -38,10 +38,6 @@ class SessionStorage(abc.ABC):
@abc.abstractmethod
def save(self, sync=False):
...
@abc.abstractmethod
def sync_cleanup(self):
...
@property
@abc.abstractmethod

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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