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

more pylint

We are good now except for the shell_utility function which needs to be
refactored.
This commit is contained in:
Joel Schaerer 2011-01-04 21:00:59 +01:00
parent b2e010e69a
commit 4e579d5f63

View File

@ -1,19 +1,23 @@
#!/usr/bin/env python #!/usr/bin/env python
#Copyright Joel Schaerer 2008-2010 """Copyright Joel Schaerer 2008-2010
#This file is part of autojump This file is part of autojump
#autojump is free software: you can redistribute it and/or modify autojump is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version. (at your option) any later version.
#
#autojump is distributed in the hope that it will be useful, autojump is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details. GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
#along with autojump. If not, see <http://www.gnu.org/licenses/>. along with autojump. If not, see <http://www.gnu.org/licenses/>.
Autojump is a small tool that maintains a database of your most
used directories, and finds the best match to help you jump to
frequently used places."""
from __future__ import division, print_function from __future__ import division, print_function
@ -27,7 +31,6 @@ from sys import argv, stderr, version_info
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
from operator import itemgetter from operator import itemgetter
import os import os
import signal
MAX_KEYWEIGHT = 1000 MAX_KEYWEIGHT = 1000
MAX_STORED_PATHS = 600 MAX_STORED_PATHS = 600
COMPLETION_SEPARATOR = '__' COMPLETION_SEPARATOR = '__'
@ -44,22 +47,24 @@ def dicadd(dic, key, increment=1):
dic[key] = dic.get(key, 0.)+increment dic[key] = dic.get(key, 0.)+increment
def save(path_dict, dic_file): def save(path_dict, dic_file):
f = NamedTemporaryFile(dir=CONFIG_DIR, delete=False) """Save the database in an atomic way, and preserve
pickle.dump(path_dict, f, -1) a backup file."""
f.flush() temp = NamedTemporaryFile(dir=CONFIG_DIR, delete=False)
os.fsync(f) pickle.dump(path_dict, temp, -1)
f.close() temp.flush()
os.fsync(temp)
temp.close()
#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) os.rename(temp.name, dic_file)
try: #backup file try: #backup file
import time import time
if (not os.path.exists(dic_file+".bak") or if (not os.path.exists(dic_file+".bak") or
time.time()-os.path.getmtime(dic_file+".bak")>86400): time.time()-os.path.getmtime(dic_file+".bak")>86400):
import shutil import shutil
shutil.copy(dic_file, dic_file+".bak") shutil.copy(dic_file, dic_file+".bak")
except OSError as e: except OSError as ex:
print("Error while creating backup autojump file. (%s)" % e, print("Error while creating backup autojump file. (%s)" %
file=stderr) ex, file=stderr)
def forget(path_dict, dic_file): def forget(path_dict, dic_file):
"""Gradually forget about directories. Only call """Gradually forget about directories. Only call
@ -75,14 +80,16 @@ def clean_dict(sorted_dirs, path_dict):
Returns True if keys were deleted""" Returns True if keys were deleted"""
if len(sorted_dirs) > MAX_STORED_PATHS: if len(sorted_dirs) > MAX_STORED_PATHS:
#remove 25 more than needed, to avoid doing it every time #remove 25 more than needed, to avoid doing it every time
for dir, dummy in sorted_dirs[MAX_STORED_PATHS-25:]: for path, dummy in sorted_dirs[MAX_STORED_PATHS-25:]:
del path_dict[dir] del path_dict[path]
return True return True
else: return False else: return False
def match(path, pattern, ignore_case=False, only_end=False): def match(path, pattern, ignore_case=False, only_end=False):
"""Check whether a path matches a particular pattern"""
try: try:
if os.path.realpath(os.curdir) == path : return False if os.path.realpath(os.curdir) == path :
return False
#Sometimes the current path doesn't exist anymore. #Sometimes the current path doesn't exist anymore.
#In that case, jump if possible. #In that case, jump if possible.
except OSError: except OSError:
@ -92,24 +99,27 @@ def match(path, pattern, ignore_case=False, only_end=False):
else: else:
match_string = path match_string = path
if ignore_case: if ignore_case:
match=(match_string.lower().find(pattern.lower()) != -1) does_match = (match_string.lower().find(pattern.lower()) != -1)
else: else:
match = (match_string.find(pattern) != -1) does_match = (match_string.find(pattern) != -1)
#return True if there is a match and the path exists #return True if there is a match and the path exists
#(useful in the case of external drives, for example) #(useful in the case of external drives, for example)
return match and os.path.exists(path) return does_match and os.path.exists(path)
def find_matches(dirs, patterns, result_list, ignore_case, max_matches): def find_matches(dirs, patterns, result_list, ignore_case, max_matches):
"""Find max_matches paths that match the pattern, """Find max_matches paths that match the pattern,
and add them to the result_list""" and add them to the result_list"""
for path, count in dirs: for path, count in dirs:
if len(result_list) >= max_matches : break if len(result_list) >= max_matches :
break
#For the last pattern, only match the end of the pattern #For the last pattern, only match the end of the pattern
if all(match(path, p, ignore_case, if all(match(path, p, ignore_case,
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): def open_dic(dic_file, error_recovery=False):
"""Try hard to open the database file, recovering
from backup if needed. """
try: try:
aj_file = open(dic_file, 'rb') aj_file = open(dic_file, 'rb')
if version_info[0] > 2: if version_info[0] > 2:
@ -128,12 +138,13 @@ def open_dic(dic_file, error_recovery=False):
return open_dic(dic_file, True) return open_dic(dic_file, True)
else: return {} #if everything fails, return an empty file else: return {} #if everything fails, return an empty file
#Main code def shell_utility():
"""Run this when autojump is called as a shell utility"""
try: try:
optlist, args = getopt.getopt(argv[1:], 'a', optlist, args = getopt.getopt(argv[1:], 'a',
['stat', 'import', 'completion', 'bash']) ['stat', 'import', 'completion', 'bash'])
except getopt.GetoptError as e: except getopt.GetoptError as ex:
print("Unknown command line argument: %s" % e) print("Unknown command line argument: %s" % ex)
exit(1) exit(1)
if CONFIG_DIR == os.path.expanduser("~"): if CONFIG_DIR == os.path.expanduser("~"):
@ -148,12 +159,12 @@ if ('-a', '') in optlist:
dicadd(path_dict, args[-1]) dicadd(path_dict, args[-1])
save(path_dict, dic_file) save(path_dict, dic_file)
elif ('--stat', '') in optlist: elif ('--stat', '') in optlist:
a = list(path_dict.items()) paths = list(path_dict.items())
a.sort(key=itemgetter(1)) paths.sort(key=itemgetter(1))
for path, count in a[-100:]: for path, count in paths[-100:]:
print("%.1f:\t%s" % (count, path)) print("%.1f:\t%s" % (count, path))
print("Total key weight: %d. Number of stored paths: %d" % print("Total key weight: %d. Number of stored paths: %d" %
(sum(path_dict.values()), len(a))) (sum(path_dict.values()), len(paths)))
elif ('--import', '') in optlist: elif ('--import', '') in optlist:
for i in open(args[-1]).readlines(): for i in open(args[-1]).readlines():
dicadd(path_dict, i[:-1]) dicadd(path_dict, i[:-1])
@ -161,7 +172,8 @@ elif ('--import', '') in optlist:
else: else:
import re import re
completion = False completion = False
userchoice = -1 #i if the pattern is of the form __pattern__i, otherwise -1 #userchoice is i if the pattern is __pattern__i, otherwise -1
userchoice = -1
results = [] results = []
if ('--completion', '') in optlist: if ('--completion', '') in optlist:
completion = True completion = True
@ -216,3 +228,7 @@ else:
COMPLETION_SEPARATOR, n+1, COMPLETION_SEPARATOR, r) COMPLETION_SEPARATOR, n+1, COMPLETION_SEPARATOR, r)
for n, r in enumerate(results[:8])))) for n, r in enumerate(results[:8]))))
elif results: print(quotes+results[0]+quotes) elif results: print(quotes+results[0]+quotes)
if __name__ == "__main__":
shell_utility()