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
59
autojump
59
autojump
@ -21,19 +21,16 @@ frequently used places."""
|
||||
|
||||
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
|
||||
from sys import argv, stderr, version_info, exit
|
||||
from tempfile import NamedTemporaryFile
|
||||
from operator import itemgetter
|
||||
import os
|
||||
|
||||
MAX_KEYWEIGHT = 1000
|
||||
MAX_STORED_PATHS = 600
|
||||
COMPLETION_SEPARATOR = '__'
|
||||
|
||||
if "AUTOJUMP_DATA_DIR" in os.environ:
|
||||
CONFIG_DIR = os.environ.get("AUTOJUMP_DATA_DIR")
|
||||
else:
|
||||
@ -57,12 +54,14 @@ def save(path_dict, dic_file):
|
||||
# Otherwise, fail quietly
|
||||
if (not os.path.exists(dic_file)) or os.getuid() == os.stat(dic_file)[4]:
|
||||
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()
|
||||
os.fsync(temp)
|
||||
temp.close()
|
||||
#cf. http://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/
|
||||
os.rename(temp.name, dic_file)
|
||||
os.rename(temp.name, dic_file)
|
||||
try: #backup file
|
||||
import time
|
||||
if (not os.path.exists(dic_file+".bak") or
|
||||
@ -73,6 +72,26 @@ def save(path_dict, dic_file):
|
||||
print("Error while creating backup autojump file. (%s)" %
|
||||
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):
|
||||
"""Gradually forget about directories. Only call
|
||||
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)):
|
||||
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():
|
||||
filename = "autojump.txt"
|
||||
if CONFIG_DIR == os.path.expanduser("~"):
|
||||
dic_file = CONFIG_DIR+"/.autojump_py"
|
||||
dic_file = CONFIG_DIR+"/." + filename
|
||||
else:
|
||||
dic_file = CONFIG_DIR+"/autojump_py"
|
||||
dic_file = CONFIG_DIR+"/" + filename
|
||||
return dic_file
|
||||
|
||||
def shell_utility():
|
||||
|
Loading…
Reference in New Issue
Block a user