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:
parent
0b07cf030e
commit
0298ef5484
28
autojump
28
autojump
@ -76,9 +76,8 @@ def open_dic(dic_file, error_recovery=False):
|
|||||||
"""Try hard to open the database file, recovering
|
"""Try hard to open the database file, recovering
|
||||||
from backup if needed. """
|
from backup if needed. """
|
||||||
try:
|
try:
|
||||||
aj_file = open(dic_file, 'r')
|
|
||||||
path_dict = {}
|
path_dict = {}
|
||||||
with aj_file:
|
with open(dic_file, 'r') as aj_file:
|
||||||
for l in aj_file.readlines():
|
for l in aj_file.readlines():
|
||||||
weight,path = l[:-1].split("\t",1)
|
weight,path = l[:-1].split("\t",1)
|
||||||
path_dict[path] = float(weight)
|
path_dict[path] = float(weight)
|
||||||
@ -90,7 +89,27 @@ def open_dic(dic_file, error_recovery=False):
|
|||||||
import shutil
|
import shutil
|
||||||
shutil.copy(dic_file+".bak", dic_file)
|
shutil.copy(dic_file+".bak", dic_file)
|
||||||
return open_dic(dic_file, True)
|
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):
|
def forget(path_dict, dic_file):
|
||||||
"""Gradually forget about directories. Only call
|
"""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 :
|
if len(result_list) >= max_matches :
|
||||||
break
|
break
|
||||||
|
|
||||||
def get_dic_file():
|
def get_dic_file(filename="autojump.txt"):
|
||||||
filename = "autojump.txt"
|
|
||||||
if CONFIG_DIR == os.path.expanduser("~"):
|
if CONFIG_DIR == os.path.expanduser("~"):
|
||||||
dic_file = CONFIG_DIR+"/." + filename
|
dic_file = CONFIG_DIR+"/." + filename
|
||||||
else:
|
else:
|
||||||
|
Loading…
Reference in New Issue
Block a user