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

attempt to make autojump more pep8-compliant

(pour les grincheux)
This commit is contained in:
Joel Schaerer 2010-12-23 11:05:20 +01:00
parent 0217d6e066
commit 43d2d94a48

View File

@ -23,7 +23,7 @@ except ImportError:
import pickle
import getopt
from sys import argv,exit,stderr,version_info
from sys import argv, stderr, version_info
from tempfile import NamedTemporaryFile
from operator import itemgetter
import os
@ -50,25 +50,30 @@ def save(path_dict,dic_file):
f.flush()
os.fsync(f)
f.close()
os.rename(f.name,dic_file) #cf. http://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/
#cf. http://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/
os.rename(f.name, dic_file)
try: #backup file
import time
if not os.path.exists(dic_file+".bak") or time.time()-os.path.getmtime(dic_file+".bak")>86400:
if (not os.path.exists(dic_file+".bak") or
time.time()-os.path.getmtime(dic_file+".bak")>86400):
import shutil
shutil.copy(dic_file, dic_file+".bak")
except OSError as e:
print("Error while creating backup autojump file. (%s)" % e, file=stderr)
print("Error while creating backup autojump file. (%s)" % e,
file=stderr)
def forget(path_dict, dic_file):
"""Gradually forget about directories. Only call from the actual jump since it can take time"""
keyweight=sum(path_dict.values()) #Gradually forget about old directories
"""Gradually forget about directories. Only call
from the actual jump since it can take time"""
keyweight = sum(path_dict.values())
if keyweight>max_keyweight:
for k in path_dict.keys():
path_dict[k]*=0.9*max_keyweight/keyweight
save(path_dict, dic_file)
def clean_dict(sorted_dirs, path_dict):
"""Limits the sized of the path_dict to max_stored_paths. Returns True if keys were deleted"""
"""Limits the sized of the path_dict to max_stored_paths.
Returns True if keys were deleted"""
if len(sorted_dirs) > max_stored_paths:
#remove 25 more than needed, to avoid doing it every time
for dir, dummy in sorted_dirs[max_stored_paths-25:]:
@ -79,7 +84,9 @@ def clean_dict(sorted_dirs,path_dict):
def match(path, pattern, ignore_case=False, only_end=False):
try:
if os.path.realpath(os.curdir) == path : return False
except OSError: #sometimes the current path doesn't exist anymore. In that case, jump if possible.
#Sometimes the current path doesn't exist anymore.
#In that case, jump if possible.
except OSError:
pass
if only_end:
match_string = "/".join(path.split('/')[-1-pattern.count('/'):])
@ -89,15 +96,18 @@ def match(path,pattern,ignore_case=False,only_end=False):
match=(match_string.lower().find(pattern.lower()) != -1)
else:
match = (match_string.find(pattern) != -1)
#return true if there is a match and the path exists (useful in the case of external drives, for example)
#return True if there is a match and the path exists
#(useful in the case of external drives, for example)
return match and os.path.exists(path)
def find_matches(dirs, patterns, result_list, ignore_case, max_matches):
"""Find max_matches paths that match the pattern, and add them to the result_list"""
"""Find max_matches paths that match the pattern,
and add them to the result_list"""
for path, count in dirs:
if len(result_list) >= max_matches : break
#For the last pattern, only match the end of the pattern
if all(match(path,p,ignore_case, only_end = (n==len(patterns)-1)) for n,p in enumerate(patterns)):
if all(match(path, p, ignore_case,
only_end=(n == len(patterns)-1)) for n, p in enumerate(patterns)):
uniqadd(result_list, path)
def open_dic(dic_file, error_recovery=False):
@ -112,7 +122,8 @@ def open_dic(dic_file,error_recovery=False):
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)
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)
@ -120,7 +131,8 @@ def open_dic(dic_file,error_recovery=False):
#Main code
try:
optlist, args = getopt.getopt(argv[1:], 'a',['stat','import','completion', 'bash'])
optlist, args = getopt.getopt(argv[1:], 'a',
['stat', 'import', 'completion', 'bash'])
except getopt.GetoptError as e:
print("Unknown command line argument: %s" % e)
exit(1)
@ -131,7 +143,9 @@ else:
dic_file = config_dir+"/autojump_py"
path_dict = open_dic(dic_file)
if ('-a', '') in optlist:
if(args[-1] != os.path.expanduser("~")): # home dir can be reached quickly by "cd" and may interfere with other directories
# The home dir can be reached quickly by "cd"
# and may interfere with other directories
if(args[-1] != os.path.expanduser("~")):
dicadd(path_dict, args[-1])
save(path_dict, dic_file)
elif ('--stat', '') in optlist:
@ -139,7 +153,8 @@ elif ('--stat','') in optlist:
a.sort(key=itemgetter(1))
for path, count in a[-100:]:
print("%.1f:\t%s" % (count, path))
print("Total key weight: %d. Number of stored paths: %d" % (sum(path_dict.values()),len(a)))
print("Total key weight: %d. Number of stored paths: %d" %
(sum(path_dict.values()), len(a)))
elif ('--import', '') in optlist:
for i in open(args[-1]).readlines():
dicadd(path_dict, i[:-1])
@ -156,18 +171,21 @@ else:
if not args: patterns = [""]
else: patterns = args
#if the last pattern contains a full path, jump there
#the regexp is because we need to support stuff like "j wo jo__3__/home/joel/workspace/joel" for zsh
# If the last pattern contains a full path, jump there
# The regexp is because we need to support stuff like
# "j wo jo__3__/home/joel/workspace/joel" for zsh
last_pattern_path = re.sub("(.*)"+completion_separator, "", patterns[-1])
#print >> stderr, last_pattern_path
if len(last_pattern_path)>0 and last_pattern_path[0]=="/" and os.path.exists(last_pattern_path):
if (len(last_pattern_path)>0 and
last_pattern_path[0] == "/" and
os.path.exists(last_pattern_path)):
if not completion: print(last_pattern_path)
else:
#check for ongoing completion, and act accordingly
endmatch=re.search(completion_separator+"([0-9]+)",patterns[-1]) #user has selected a completion
if endmatch:
endmatch = re.search(completion_separator+"([0-9]+)", patterns[-1])
if endmatch: #user has selected a completion
userchoice = int(endmatch.group(1))
patterns[-1]=re.sub(completion_separator+"[0-9]+.*","",patterns[-1])
patterns[-1] = re.sub(completion_separator+"[0-9]+.*",
"", patterns[-1])
else: #user hasn't selected a completion, display the same choices again
endmatch = re.match("(.*)"+completion_separator, patterns[-1])
if endmatch: patterns[-1] = endmatch.group(1)
@ -179,17 +197,23 @@ else:
else:
max_matches = 1
find_matches(dirs, patterns, results, False, max_matches)
if completion or not results: #if not found, try ignoring case. On completion always show all results
find_matches(dirs,patterns,results,ignore_case=True,max_matches=max_matches)
if not completion and clean_dict(dirs,path_dict): #keep the database to a reasonable size
# If not found, try ignoring case.
# On completion always show all results
if completion or not results:
find_matches(dirs, patterns, results,
ignore_case=True, max_matches=max_matches)
# Keep the database to a reasonable size
if not completion and clean_dict(dirs, path_dict):
save(path_dict, dic_file)
if completion and ('--bash', '') in optlist: quotes = '"'
else: quotes = ""
if userchoice != -1:
if len(results) > userchoice-1 : print(quotes+results[userchoice-1]+quotes)
if len(results) > userchoice-1 :
print(quotes+results[userchoice-1]+quotes)
elif len(results) > 1 and completion:
print("\n".join(("%s%s%d%s%s" % (patterns[-1],completion_separator,n+1,completion_separator,r)\
print("\n".join(("%s%s%d%s%s" % (patterns[-1],
completion_separator, n+1, completion_separator, r)
for n, r in enumerate(results[:8]))))
elif results: print(quotes+results[0]+quotes)