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

(ugly) fixes for python 3

This commit is contained in:
Joël Schaerer 2011-09-12 17:04:37 +02:00
parent 81670c5fbc
commit 0ee020c72c

View File

@ -50,6 +50,9 @@ def dicadd(dic, key, increment=1):
def output(unicode_text,encoding=None): def output(unicode_text,encoding=None):
"""Wrapper for the print function, using the filesystem encoding by default """Wrapper for the print function, using the filesystem encoding by default
to minimize encoding mismatch problems in directory names""" to minimize encoding mismatch problems in directory names"""
if version_info[0] > 2:
print(unicode_text)
else:
if encoding is None: if encoding is None:
encoding = getfilesystemencoding() encoding = getfilesystemencoding()
print(unicode_text.encode(encoding)) print(unicode_text.encode(encoding))
@ -63,6 +66,13 @@ def decode(text,encoding=None,errors="strict"):
encoding = getfilesystemencoding() encoding = getfilesystemencoding()
return text.decode(encoding,errors) return text.decode(encoding,errors)
def unico(text):
"""if python2, convert to a unicode object"""
if version_info[0] > 2:
return text
else:
return unicode(text)
def save(path_dict, dic_file): def save(path_dict, dic_file):
"""Save the database in an atomic way, and preserve """Save the database in an atomic way, and preserve
a backup file.""" a backup file."""
@ -72,7 +82,7 @@ def save(path_dict, dic_file):
temp = NamedTemporaryFile(dir=CONFIG_DIR, delete=False) temp = NamedTemporaryFile(dir=CONFIG_DIR, delete=False)
for path in path_dict: for path in path_dict:
# the db is stored in utf-8 # the db is stored in utf-8
temp.write((u"%s\t%s\n" %(path_dict[path],path)).encode("utf-8")) temp.write((unico("%s\t%s\n")%(path_dict[path],path)).encode("utf-8"))
temp.flush() temp.flush()
os.fsync(temp) os.fsync(temp)
temp.close() temp.close()
@ -224,7 +234,7 @@ def shell_utility():
paths = list(path_dict.items()) paths = list(path_dict.items())
paths.sort(key=itemgetter(1)) paths.sort(key=itemgetter(1))
for path, count in paths[-100:]: for path, count in paths[-100:]:
output(u"%.1f:\t%s" % (count, path)) output(unico("%.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(paths))) (sum(path_dict.values()), len(paths)))
else: else:
@ -237,7 +247,7 @@ def shell_utility():
completion = True completion = True
else: else:
forget(path_dict, dic_file) #gradually forget about old directories forget(path_dict, dic_file) #gradually forget about old directories
if not args: patterns = [u""] if not args: patterns = [unico("")]
else: patterns = [decode(a) for a in args] else: patterns = [decode(a) for a in args]
# If the last pattern contains a full path, jump there # If the last pattern contains a full path, jump there
@ -280,13 +290,12 @@ def shell_utility():
if userchoice != -1: if userchoice != -1:
if len(results) > userchoice-1 : if len(results) > userchoice-1 :
output(u"%s%s%s" % (quotes,results[userchoice-1],quotes)) output(unico("%s%s%s") % (quotes,results[userchoice-1],quotes))
print(quotes+results[userchoice-1]+quotes)
elif len(results) > 1 and completion: elif len(results) > 1 and completion:
output("\n".join(("%s%s%d%s%s" % (patterns[-1], output("\n".join(("%s%s%d%s%s" % (patterns[-1],
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: output(u"%s%s%s"%(quotes,results[0],quotes)) elif results: output(unico("%s%s%s")%(quotes,results[0],quotes))
else: else:
return False return False
return True return True