2008-04-21 09:43:07 +00:00
|
|
|
#!/usr/bin/python
|
2009-02-12 19:23:49 +00:00
|
|
|
#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
|
2008-04-21 09:43:07 +00:00
|
|
|
from __future__ import division
|
|
|
|
import cPickle
|
|
|
|
import getopt
|
2009-02-14 09:28:27 +00:00
|
|
|
from sys import argv,exit
|
2008-04-21 09:43:07 +00:00
|
|
|
import os
|
2008-06-02 15:43:38 +00:00
|
|
|
import signal
|
2009-02-18 13:12:13 +00:00
|
|
|
max_keyweight=1000
|
2008-06-02 15:43:38 +00:00
|
|
|
|
|
|
|
def signal_handler(arg1,arg2):
|
|
|
|
print "Received SIGINT, trying to continue"
|
|
|
|
signal.signal(signal.SIGINT,signal_handler) #Don't break on sigint
|
2008-04-21 09:43:07 +00:00
|
|
|
|
2009-02-13 22:22:32 +00:00
|
|
|
def uniqadd(list,key):
|
|
|
|
if key not in list:
|
|
|
|
list.append(key)
|
|
|
|
|
2008-04-21 09:43:07 +00:00
|
|
|
def dicadd(dic,key,increment=1):
|
|
|
|
dic[key]=dic.get(key,0.)+increment
|
|
|
|
|
2008-09-02 08:25:32 +00:00
|
|
|
def match(path,pattern,path_dict,re_flags=0):
|
2008-04-21 09:43:07 +00:00
|
|
|
import re
|
|
|
|
if os.path.realpath(os.curdir)==path : return False
|
2008-09-02 08:25:32 +00:00
|
|
|
if re.search(pattern,"/".join(path.split('/')[-1-pattern.count('/'):]),re_flags) is None:
|
2008-04-21 09:43:07 +00:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if os.path.exists(path) : return True
|
|
|
|
else: #clean up dead directories
|
|
|
|
del path_dict[path]
|
|
|
|
return False
|
|
|
|
|
2009-02-18 14:13:29 +00:00
|
|
|
def save(path_dict,dic_file):
|
|
|
|
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
|
|
|
|
|
2009-02-18 13:12:13 +00:00
|
|
|
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
|
|
|
|
if keyweight>max_keyweight:
|
|
|
|
for k in path_dict.keys():
|
|
|
|
path_dict[k]*=0.9*max_keyweight/keyweight
|
2009-02-18 14:13:29 +00:00
|
|
|
save(path_dict,dic_file)
|
2009-02-18 13:12:13 +00:00
|
|
|
|
2009-02-19 09:16:30 +00:00
|
|
|
def find_matches(dirs,pattern,path_dict,result_list,re_flags,max_matches):
|
|
|
|
"""Find max_matches paths that match the pattern, and add them to the result_list"""
|
2009-02-18 13:12:13 +00:00
|
|
|
for path,count in dirs:
|
2009-02-19 09:16:30 +00:00
|
|
|
if len(result_list) >= max_matches : break
|
2009-02-18 13:12:13 +00:00
|
|
|
if match(path,pattern,path_dict,re_flags):
|
|
|
|
uniqadd(result_list,path)
|
|
|
|
|
|
|
|
#Main code
|
2009-02-14 09:28:27 +00:00
|
|
|
try:
|
|
|
|
optlist, args = getopt.getopt(argv[1:], 'a',['stat','import','completion'])
|
|
|
|
except getopt.GetoptError, e:
|
|
|
|
print "Unknown command line argument: %s" % e
|
|
|
|
exit(1)
|
|
|
|
|
2008-04-21 09:43:07 +00:00
|
|
|
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={}
|
|
|
|
|
2009-03-06 19:30:11 +00:00
|
|
|
if ('-a','') in optlist: #this can be done in the background
|
|
|
|
os.nice(10)
|
|
|
|
pid=os.fork()
|
|
|
|
if not pid:
|
|
|
|
os.setsid()
|
|
|
|
dicadd(path_dict,args[-1])
|
|
|
|
save(path_dict,dic_file)
|
2008-04-21 09:43:07 +00:00
|
|
|
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:
|
2009-02-13 23:41:03 +00:00
|
|
|
for i in open(args[-1]).readlines():
|
2008-04-21 09:43:07 +00:00
|
|
|
dicadd(path_dict,i[:-1])
|
|
|
|
cPickle.dump(path_dict,open(dic_file,'w'),-1)
|
|
|
|
else:
|
2009-02-14 00:18:11 +00:00
|
|
|
import re
|
2009-02-13 22:22:32 +00:00
|
|
|
completion=False
|
2009-02-14 00:57:49 +00:00
|
|
|
userchoice=-1 #3 if the pattern is of the form __pattern__3, otherwise -1
|
2009-02-18 13:12:13 +00:00
|
|
|
results=[]
|
2009-02-13 22:22:32 +00:00
|
|
|
if ('--completion','') in optlist:
|
|
|
|
completion=True
|
2009-02-18 13:12:13 +00:00
|
|
|
else:
|
|
|
|
forget(path_dict,dic_file) #gradually forget about old directories
|
|
|
|
if not args: pattern=""
|
|
|
|
else: pattern=args[-1]
|
|
|
|
|
2009-02-19 09:20:46 +00:00
|
|
|
if len(pattern)>0 and pattern[0]=="/" and os.path.exists(pattern): #if pattern is a full path, jump there
|
2009-02-18 13:12:13 +00:00
|
|
|
if not completion : print pattern
|
|
|
|
else:
|
|
|
|
endmatch=re.search("__([0-9]+)",pattern)
|
2009-02-14 00:57:49 +00:00
|
|
|
if endmatch:
|
|
|
|
userchoice=int(endmatch.group(1))
|
2009-02-18 13:12:13 +00:00
|
|
|
pattern=re.sub("__[0-9]+.*","",pattern)
|
2009-02-14 00:57:49 +00:00
|
|
|
else:
|
2009-02-18 13:12:13 +00:00
|
|
|
endmatch=re.match("(.*)__",pattern)
|
|
|
|
if endmatch: pattern=endmatch.group(1)
|
|
|
|
|
|
|
|
dirs=path_dict.items()
|
|
|
|
dirs.sort(key=lambda e:e[1],reverse=True)
|
2009-02-19 09:16:30 +00:00
|
|
|
find_matches(dirs,pattern,path_dict,results,re_flags=0,max_matches=9)
|
2009-02-18 13:12:13 +00:00
|
|
|
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 completion or not results: #if not found, try ignoring case. On completion always show all results
|
2009-02-19 09:16:30 +00:00
|
|
|
find_matches(dirs,pattern,path_dict,results,re_flags=re.IGNORECASE,max_matches=9)
|
2009-02-18 13:12:13 +00:00
|
|
|
|
2009-02-14 00:57:49 +00:00
|
|
|
if userchoice!=-1:
|
2009-02-18 13:12:13 +00:00
|
|
|
if len(results) > userchoice-1 : print results[userchoice-1]
|
|
|
|
elif len(results) > 1 and completion:
|
2009-02-18 13:55:50 +00:00
|
|
|
print "\n".join(("%s__%d__%s" % (pattern,n+1,r) for n,r in enumerate(results[:8])))
|
2009-02-18 13:12:13 +00:00
|
|
|
else:
|
|
|
|
if results : print results[0]
|