mirror of
https://github.com/wting/autojump
synced 2024-10-27 20:34:07 +00:00
use a text format for the path file, for easy edition
Still needs a migration script
This commit is contained in:
parent
00ce1696a7
commit
01179b86f4
57
autojump
57
autojump
@ -21,19 +21,16 @@ frequently used places."""
|
|||||||
|
|
||||||
from __future__ import division, print_function
|
from __future__ import division, print_function
|
||||||
|
|
||||||
try: # fix to get optimised pickle in python < 3
|
|
||||||
import cPickle as pickle
|
|
||||||
except ImportError:
|
|
||||||
import pickle
|
|
||||||
|
|
||||||
import getopt
|
import getopt
|
||||||
from sys import argv, stderr, version_info, exit
|
from sys import argv, stderr, version_info, exit
|
||||||
from tempfile import NamedTemporaryFile
|
from tempfile import NamedTemporaryFile
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
import os
|
import os
|
||||||
|
|
||||||
MAX_KEYWEIGHT = 1000
|
MAX_KEYWEIGHT = 1000
|
||||||
MAX_STORED_PATHS = 600
|
MAX_STORED_PATHS = 600
|
||||||
COMPLETION_SEPARATOR = '__'
|
COMPLETION_SEPARATOR = '__'
|
||||||
|
|
||||||
if "AUTOJUMP_DATA_DIR" in os.environ:
|
if "AUTOJUMP_DATA_DIR" in os.environ:
|
||||||
CONFIG_DIR = os.environ.get("AUTOJUMP_DATA_DIR")
|
CONFIG_DIR = os.environ.get("AUTOJUMP_DATA_DIR")
|
||||||
else:
|
else:
|
||||||
@ -57,7 +54,9 @@ def save(path_dict, dic_file):
|
|||||||
# Otherwise, fail quietly
|
# Otherwise, fail quietly
|
||||||
if (not os.path.exists(dic_file)) or os.getuid() == os.stat(dic_file)[4]:
|
if (not os.path.exists(dic_file)) or os.getuid() == os.stat(dic_file)[4]:
|
||||||
temp = NamedTemporaryFile(dir=CONFIG_DIR, delete=False)
|
temp = NamedTemporaryFile(dir=CONFIG_DIR, delete=False)
|
||||||
pickle.dump(path_dict, temp, 2)
|
for path in path_dict:
|
||||||
|
print(path_dict[path])
|
||||||
|
temp.write((repr(path_dict[path]) + "\t" + path + "\n").encode("utf-8"))
|
||||||
temp.flush()
|
temp.flush()
|
||||||
os.fsync(temp)
|
os.fsync(temp)
|
||||||
temp.close()
|
temp.close()
|
||||||
@ -73,6 +72,26 @@ def save(path_dict, dic_file):
|
|||||||
print("Error while creating backup autojump file. (%s)" %
|
print("Error while creating backup autojump file. (%s)" %
|
||||||
ex, file=stderr)
|
ex, file=stderr)
|
||||||
|
|
||||||
|
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:
|
||||||
|
for l in aj_file.readlines():
|
||||||
|
weight,path = l[:-1].split("\t",1)
|
||||||
|
path_dict[path] = float(weight)
|
||||||
|
return path_dict
|
||||||
|
except (IOError, EOFError):
|
||||||
|
if not error_recovery and os.path.exists(dic_file+".bak"):
|
||||||
|
print('Problem with autojump database,\
|
||||||
|
trying to recover from backup...', file=stderr)
|
||||||
|
import shutil
|
||||||
|
shutil.copy(dic_file+".bak", dic_file)
|
||||||
|
return open_dic(dic_file, True)
|
||||||
|
else: 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
|
||||||
from the actual jump since it can take time"""
|
from the actual jump since it can take time"""
|
||||||
@ -124,32 +143,12 @@ def find_matches(dirs, patterns, result_list, ignore_case, max_matches):
|
|||||||
only_end=(n == len(patterns)-1)) for n, p in enumerate(patterns)):
|
only_end=(n == len(patterns)-1)) for n, p in enumerate(patterns)):
|
||||||
uniqadd(result_list, path)
|
uniqadd(result_list, path)
|
||||||
|
|
||||||
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, 'rb')
|
|
||||||
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):
|
|
||||||
if not error_recovery and os.path.exists(dic_file+".bak"):
|
|
||||||
print('Problem with autojump database,\
|
|
||||||
trying to recover from backup...', file=stderr)
|
|
||||||
import shutil
|
|
||||||
shutil.copy(dic_file+".bak", dic_file)
|
|
||||||
return open_dic(dic_file, True)
|
|
||||||
else: return {} #if everything fails, return an empty file
|
|
||||||
|
|
||||||
def get_dic_file():
|
def get_dic_file():
|
||||||
|
filename = "autojump.txt"
|
||||||
if CONFIG_DIR == os.path.expanduser("~"):
|
if CONFIG_DIR == os.path.expanduser("~"):
|
||||||
dic_file = CONFIG_DIR+"/.autojump_py"
|
dic_file = CONFIG_DIR+"/." + filename
|
||||||
else:
|
else:
|
||||||
dic_file = CONFIG_DIR+"/autojump_py"
|
dic_file = CONFIG_DIR+"/" + filename
|
||||||
return dic_file
|
return dic_file
|
||||||
|
|
||||||
def shell_utility():
|
def shell_utility():
|
||||||
|
Loading…
Reference in New Issue
Block a user