1919import base64
2020import json
2121import logging
22- import os
2322import sqlite3
2423from pathlib import Path
25- from sqlite3 import DatabaseError
2624from threading import Lock
27- from typing import Union
2825
2926from .memory_storage import MemoryStorage
3027
@@ -43,25 +40,18 @@ def __init__(self, name: str, workdir: Path):
4340 self .lock = Lock ()
4441
4542 # noinspection PyAttributeOutsideInit
46- def migrate_from_json (self , path : Union [str , Path ]):
47- log .warning ("JSON session storage detected! Pyrogram will now convert it into an SQLite session storage..." )
48-
49- with open (path , encoding = "utf-8" ) as f :
50- json_session = json .load (f )
51-
52- os .remove (path )
53-
43+ def migrate_from_json (self , session_json : dict ):
5444 self .open ()
5545
56- self .dc_id = json_session ["dc_id" ]
57- self .test_mode = json_session ["test_mode" ]
58- self .auth_key = base64 .b64decode ("" .join (json_session ["auth_key" ]))
59- self .user_id = json_session ["user_id" ]
60- self .date = json_session .get ("date" , 0 )
61- self .is_bot = json_session .get ("is_bot" , False )
46+ self .dc_id = session_json ["dc_id" ]
47+ self .test_mode = session_json ["test_mode" ]
48+ self .auth_key = base64 .b64decode ("" .join (session_json ["auth_key" ]))
49+ self .user_id = session_json ["user_id" ]
50+ self .date = session_json .get ("date" , 0 )
51+ self .is_bot = session_json .get ("is_bot" , False )
6252
63- peers_by_id = json_session .get ("peers_by_id" , {})
64- peers_by_phone = json_session .get ("peers_by_phone" , {})
53+ peers_by_id = session_json .get ("peers_by_id" , {})
54+ peers_by_phone = session_json .get ("peers_by_phone" , {})
6555
6656 peers = {}
6757
@@ -81,22 +71,40 @@ def migrate_from_json(self, path: Union[str, Path]):
8171 # noinspection PyTypeChecker
8272 self .update_peers (peers .values ())
8373
84- log .warning ("Done! The session has been successfully converted from JSON to SQLite storage" )
85-
8674 def open (self ):
87- database_exists = os .path .isfile (self .database )
75+ path = self .database
76+ file_exists = path .is_file ()
77+
78+ if file_exists :
79+ try :
80+ with open (path , encoding = "utf-8" ) as f :
81+ session_json = json .load (f )
82+ except ValueError :
83+ pass
84+ else :
85+ log .warning ("JSON session storage detected! Converting it into an SQLite session storage..." )
86+
87+ path .rename (path .name + ".OLD" )
88+
89+ log .warning ('The old session file has been renamed to "{}.OLD"' .format (path .name ))
90+
91+ self .migrate_from_json (session_json )
92+
93+ log .warning ("Done! The session has been successfully converted from JSON to SQLite storage" )
94+
95+ return
96+
97+ if Path (path .name + ".OLD" ).is_file ():
98+ log .warning ('Old session file detected: "{}.OLD". You can remove this file now' .format (path .name ))
8899
89100 self .conn = sqlite3 .connect (
90- str ( self . database ) ,
101+ path ,
91102 timeout = 1 ,
92103 check_same_thread = False
93104 )
94105
95- try :
96- if not database_exists :
97- self .create ()
106+ if not file_exists :
107+ self .create ()
98108
99- with self .conn :
100- self .conn .execute ("VACUUM" )
101- except DatabaseError :
102- self .migrate_from_json (self .database )
109+ with self .conn :
110+ self .conn .execute ("VACUUM" )
0 commit comments