mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Backup/restore for preferences stored in metadata.db
This commit is contained in:
commit
73d8d1bcb0
@ -738,6 +738,7 @@ class Main(MainWindow, MainWindowMixin, DeviceMixin, EmailMixin, # {{{
|
|||||||
# Goes here, because if cf is valid, db is valid.
|
# Goes here, because if cf is valid, db is valid.
|
||||||
db.prefs['field_metadata'] = db.field_metadata.all_metadata()
|
db.prefs['field_metadata'] = db.field_metadata.all_metadata()
|
||||||
db.commit_dirty_cache()
|
db.commit_dirty_cache()
|
||||||
|
db.prefs.write_serialized(prefs['library_path'])
|
||||||
for action in self.iactions.values():
|
for action in self.iactions.values():
|
||||||
if not action.shutting_down():
|
if not action.shutting_down():
|
||||||
return
|
return
|
||||||
|
@ -5,7 +5,7 @@ __license__ = 'GPL v3'
|
|||||||
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
import json
|
import json, os
|
||||||
|
|
||||||
from calibre.constants import preferred_encoding
|
from calibre.constants import preferred_encoding
|
||||||
from calibre.utils.config import to_json, from_json
|
from calibre.utils.config import to_json, from_json
|
||||||
@ -71,3 +71,30 @@ class DBPrefs(dict):
|
|||||||
key = u'namespaced:%s:%s'%(namespace, key)
|
key = u'namespaced:%s:%s'%(namespace, key)
|
||||||
self[key] = val
|
self[key] = val
|
||||||
|
|
||||||
|
def write_serialized(self, library_path):
|
||||||
|
try:
|
||||||
|
to_filename = os.path.join(library_path, 'metadata_db_prefs_backup.json')
|
||||||
|
with open(to_filename, "wb") as f:
|
||||||
|
f.write(json.dumps(self, indent=2, default=to_json))
|
||||||
|
except:
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
|
def read_serialized(self, library_path):
|
||||||
|
try:
|
||||||
|
from_filename = os.path.join(library_path,
|
||||||
|
'metadata_db_prefs_backup.json')
|
||||||
|
with open(from_filename, "rb") as f:
|
||||||
|
self.clear()
|
||||||
|
d = json.load(f, object_hook=from_json)
|
||||||
|
self.db.conn.execute('DELETE FROM preferences')
|
||||||
|
for k,v in d.iteritems():
|
||||||
|
raw = self.to_raw(v)
|
||||||
|
self.db.conn.execute(
|
||||||
|
'INSERT INTO preferences (key,val) VALUES (?,?)', (k, raw))
|
||||||
|
self.db.conn.commit()
|
||||||
|
self.clear()
|
||||||
|
self.update(d)
|
||||||
|
except:
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
@ -101,6 +101,7 @@ class Restore(Thread):
|
|||||||
with TemporaryDirectory('_library_restore') as tdir:
|
with TemporaryDirectory('_library_restore') as tdir:
|
||||||
self.library_path = tdir
|
self.library_path = tdir
|
||||||
self.scan_library()
|
self.scan_library()
|
||||||
|
self.load_preferences()
|
||||||
self.create_cc_metadata()
|
self.create_cc_metadata()
|
||||||
self.restore_books()
|
self.restore_books()
|
||||||
if self.successes == 0 and len(self.dirs) > 0:
|
if self.successes == 0 and len(self.dirs) > 0:
|
||||||
@ -109,6 +110,24 @@ class Restore(Thread):
|
|||||||
except:
|
except:
|
||||||
self.tb = traceback.format_exc()
|
self.tb = traceback.format_exc()
|
||||||
|
|
||||||
|
def load_preferences(self):
|
||||||
|
self.progress_callback(None, 1)
|
||||||
|
self.progress_callback('Starting restore preferences', 0)
|
||||||
|
dbpath = os.path.join(self.src_library_path, 'metadata_db_prefs_backup.json')
|
||||||
|
ndbpath = os.path.join(self.library_path, 'metadata_db_prefs_backup.json')
|
||||||
|
if not os.path.exists(dbpath):
|
||||||
|
self.progress_callback('Cannot restore preferences. Backup file not found.', 1)
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
shutil.copyfile(dbpath, ndbpath)
|
||||||
|
db = RestoreDatabase(self.library_path)
|
||||||
|
db.prefs.read_serialized(self.library_path)
|
||||||
|
db.commit()
|
||||||
|
db.conn.close()
|
||||||
|
self.progress_callback('Finished restore preferences', 1)
|
||||||
|
except:
|
||||||
|
self.progress_callback('Restoring preferences failed', 1)
|
||||||
|
|
||||||
def scan_library(self):
|
def scan_library(self):
|
||||||
for dirpath, dirnames, filenames in os.walk(self.src_library_path):
|
for dirpath, dirnames, filenames in os.walk(self.src_library_path):
|
||||||
leaf = os.path.basename(dirpath)
|
leaf = os.path.basename(dirpath)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user