1
0
mirror of https://github.com/wting/autojump synced 2024-10-27 20:34:07 +00:00
wting_autojump/autojump
2009-02-14 01:57:49 +01:00

117 lines
3.8 KiB
Python
Executable File

#!/usr/bin/python
#This code is licensed under the GPL, version 3.
#A full copy of the license can be obtained by contacting the author at the following email adress: joel.schaerer@laposte.net
from __future__ import division
import cPickle
import getopt
from sys import argv
import os
import signal
def signal_handler(arg1,arg2):
print "Received SIGINT, trying to continue"
signal.signal(signal.SIGINT,signal_handler) #Don't break on sigint
#add the following to your .bashrc:
"""
PROMPT_COMMAND='autojump.py -a $(pwd)'
function j { cd "$(autojump.py $1)"; }
"""
max_keyweight=1000
def uniqadd(list,key):
if key not in list:
list.append(key)
def dicadd(dic,key,increment=1):
dic[key]=dic.get(key,0.)+increment
def match(path,pattern,path_dict,re_flags=0):
import re
if os.path.realpath(os.curdir)==path : return False
if re.search(pattern,"/".join(path.split('/')[-1-pattern.count('/'):]),re_flags) is None:
return False
else:
if os.path.exists(path) : return True
else: #clean up dead directories
del path_dict[path]
return False
optlist, args = getopt.getopt(argv[1:], 'a',['stat','import','completion'])
dic_file=os.path.expanduser("~/.autojump_py")
try:
aj_file=open(dic_file)
path_dict=cPickle.load(aj_file)
aj_file.close()
except IOError:
path_dict={}
if ('-a','') in optlist:
dicadd(path_dict,args[-1])
cPickle.dump(path_dict,open(dic_file,'w'),-1)
elif ('--stat','') in optlist:
a=path_dict.items()
a.sort(key=lambda e:e[1])
for path,count in a[-100:]:
print "%.1f:\t%s" % (count,path)
print "Total key weight: %d" % sum(path_dict.values())
elif ('--import','') in optlist:
for i in open(args[-1]).readlines():
dicadd(path_dict,i[:-1])
cPickle.dump(path_dict,open(dic_file,'w'),-1)
else:
import re
completion=False
userchoice=-1 #3 if the pattern is of the form __pattern__3, otherwise -1
if ('--completion','') in optlist:
completion=True
results=[]
keyweight=sum(path_dict.values()) #Gradually forget about old directories
if keyweight>max_keyweight:
for k in path_dict.keys():
path_dict[k]*=0.9*max_keyweight/keyweight
if not args: args=['']
if completion:
argument=args[-1]
endmatch=re.search("__([0-9]+)$",argument)
if endmatch:
userchoice=int(endmatch.group(1))
argument=argument[2:]
argument=re.sub("__[0-9]+$","",argument)
else:
endmatch=re.match("__(.*)__",argument)
if endmatch: argument=endmatch.group(1)
else:
argument=re.sub("^__.*__[0-9]+__","",args[-1])
dirs=path_dict.items()
dirs.sort(key=lambda e:e[1],reverse=True)
found=False
for path,count in dirs:
if match(path,argument,path_dict): #First look for case-matching path
if not completion:
print path
found=True
break
else:
uniqadd(results,path)
dirs=path_dict.items() #we need to recreate the list since the first iteration potentially deletes paths
dirs.sort(key=lambda e:e[1],reverse=True)
if not found:
for path,count in dirs:
if match(path,argument,path_dict,re.IGNORECASE): #Then try to ignore case
if not completion:
print path
break
else:
uniqadd(results,path)
if completion:
if userchoice!=-1:
print results[userchoice]
else:
print " ".join(("__%s__%d__%s" % (argument,n,r) for n,r in enumerate(results)))
cPickle.dump(path_dict,open(dic_file+".tmp",'w'),-1)
import shutil
shutil.copy(dic_file+".tmp",dic_file) #cPickle.dump doesn't seem to be atomic, so this is more secure