|
|
|
@ -104,19 +104,44 @@ class Database:
|
|
|
|
|
Try to open the database file, recovering from backup if needed.
|
|
|
|
|
"""
|
|
|
|
|
if os.path.exists(self.filename):
|
|
|
|
|
try:
|
|
|
|
|
with open(self.filename, 'r') as f:
|
|
|
|
|
for line in f.readlines():
|
|
|
|
|
weight, path = line[:-1].split("\t", 1)
|
|
|
|
|
path = decode(path, 'utf-8')
|
|
|
|
|
self.data[path] = float(weight)
|
|
|
|
|
elif os.path.exists(self.filename + '.bak'):
|
|
|
|
|
except (IOError, EOFError):
|
|
|
|
|
self.load_backup(error_recovery)
|
|
|
|
|
else:
|
|
|
|
|
self.load_backup(error_recovery)
|
|
|
|
|
|
|
|
|
|
def load_backup(self, error_recovery = False):
|
|
|
|
|
"""
|
|
|
|
|
Loads database from backup file.
|
|
|
|
|
"""
|
|
|
|
|
if os.path.exists(self.filename + '.bak'):
|
|
|
|
|
if not error_recovery:
|
|
|
|
|
print('Problem with autojump database,\
|
|
|
|
|
trying to recover from backup...', file=sys.stderr)
|
|
|
|
|
shutil.copy(self.filename + '.bak', self.filename)
|
|
|
|
|
return self.load(True)
|
|
|
|
|
else:
|
|
|
|
|
# migration code, autojump_py last used in v17
|
|
|
|
|
self.migrate()
|
|
|
|
|
|
|
|
|
|
def maintenance(self):
|
|
|
|
|
"""
|
|
|
|
|
Trims and decays database entries when exceeding settings.
|
|
|
|
|
"""
|
|
|
|
|
if sum(self.data.values()) > MAX_KEYWEIGHT:
|
|
|
|
|
self.decay()
|
|
|
|
|
if len(self.data) > MAX_STORED_PATHS:
|
|
|
|
|
self.trim()
|
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
|
|
def migrate(self):
|
|
|
|
|
"""
|
|
|
|
|
Migrates database from old format autojump_py, last used in release-v17.
|
|
|
|
|
"""
|
|
|
|
|
self.filename = get_db_file('autojump_py')
|
|
|
|
|
if os.path.exists(self.filename):
|
|
|
|
|
try: # fix to get optimised pickle in python < 3
|
|
|
|
@ -140,16 +165,6 @@ class Database:
|
|
|
|
|
pass
|
|
|
|
|
return {} # if everything fails, return an empty dictionary
|
|
|
|
|
|
|
|
|
|
def maintenance(self):
|
|
|
|
|
"""
|
|
|
|
|
Trims and decays database entries when exceeding settings.
|
|
|
|
|
"""
|
|
|
|
|
if sum(self.data.values()) > MAX_KEYWEIGHT:
|
|
|
|
|
self.decay()
|
|
|
|
|
if len(self.data) > MAX_STORED_PATHS:
|
|
|
|
|
self.trim()
|
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
|
|
def purge(self):
|
|
|
|
|
"""
|
|
|
|
|
Deletes all entries that no longer exist on system.
|
|
|
|
|