1
0
mirror of https://github.com/wting/autojump synced 2024-10-27 20:34:07 +00:00

Migration code for the new database format

This commit is contained in:
Joël Schaerer 2011-09-10 20:20:08 +02:00
parent 0b07cf030e
commit 0298ef5484

View File

@ -76,9 +76,8 @@ def open_dic(dic_file, error_recovery=False):
"""Try hard to open the database file, recovering
from backup if needed. """
try:
aj_file = open(dic_file, 'r')
path_dict = {}
with aj_file:
with open(dic_file, 'r') as aj_file:
for l in aj_file.readlines():
weight,path = l[:-1].split("\t",1)
path_dict[path] = float(weight)
@ -90,7 +89,27 @@ def open_dic(dic_file, error_recovery=False):
import shutil
shutil.copy(dic_file+".bak", dic_file)
return open_dic(dic_file, True)
else: return {} #if everything fails, return an empty file
else:
# Temporary migration code
old_dic_file = get_dic_file("autojump_py")
if os.path.exists(old_dic_file):
try: # fix to get optimised pickle in python < 3
import cPickle as pickle
except ImportError:
import pickle
try:
with open(old_dic_file, 'rb') as aj_file:
if version_info[0] > 2:
#encoding is only specified for python2.x compatibility
path_dict = pickle.load(aj_file, encoding="utf-8")
else:
path_dict = pickle.load(aj_file)
aj_file.close()
return path_dict
except (IOError, EOFError, pickle.UnpicklingError):
pass
return {} #if everything fails, return an empty file
def forget(path_dict, dic_file):
"""Gradually forget about directories. Only call
@ -156,8 +175,7 @@ def find_matches(dirs, patterns, result_list, ignore_case, max_matches):
if len(result_list) >= max_matches :
break
def get_dic_file():
filename = "autojump.txt"
def get_dic_file(filename="autojump.txt"):
if CONFIG_DIR == os.path.expanduser("~"):
dic_file = CONFIG_DIR+"/." + filename
else: