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

add python3 fixes/workarounds

This commit is contained in:
William Ting 2013-12-18 16:51:26 -06:00
parent d20ec4afb4
commit 1c8a4c13cd
3 changed files with 57 additions and 24 deletions

View File

@ -24,9 +24,6 @@ from __future__ import print_function
from difflib import SequenceMatcher
from functools import partial
from itertools import chain
# FIXME(ting|2013-12-17): fix imports for Python 3 compatibility
from itertools import ifilter
from itertools import imap
from math import sqrt
from operator import attrgetter
from operator import itemgetter
@ -35,6 +32,14 @@ import platform
import re
import sys
if sys.version_info[0] == 3:
ifilter = filter
imap = map
os.getcwdu = os.getcwd
else:
from itertools import ifilter
from itertools import imap
from argparse import ArgumentParser
from data import dictify
@ -167,14 +172,12 @@ def find_matches(entries, needles):
# tautology if current working directory no longer exists
not_cwd = lambda x: True
# from pprint import pprint as pp; import pdb; pdb.set_trace()
data = sorted(
ifilter(not_cwd, entries),
key=attrgetter('weight'),
reverse=True)
if not needles:
return first(data).path
ignore_case = detect_smartcase(needles)
exists = lambda entry: os.path.exists(entry.path)
@ -184,7 +187,10 @@ def find_matches(entries, needles):
match_consecutive(needles, data, ignore_case),
match_quicksilver(needles, data, ignore_case),
match_fuzzy(needles, data, ignore_case),
match_anywhere(needles, data, ignore_case)))
match_anywhere(needles, data, ignore_case),
# default return value so calling shell functions have an
# argument to `cd` to
[Entry('.', 0)]))
def match_anywhere(needles, haystack, ignore_case=False):
@ -299,16 +305,17 @@ def purge_missing_paths(entries):
def print_stats(data, data_path):
for path, weight in sorted(data.iteritems(), key=itemgetter(1)):
for path, weight in sorted(data.items(), key=itemgetter(1)):
print_entry(Entry(path, weight))
print("________________________________________\n")
print("%d:\t total weight" % sum(data.itervalues()))
print("%d:\t total weight" % sum(data.values()))
print("%d:\t number of entries" % len(data))
try:
print("%.2f:\t current directory weight" % data.get(os.getcwdu(), 0))
except OSError:
# current directory no longer exists
pass
print("\ndata:\t %s" % data_path)
@ -330,7 +337,7 @@ def main(args):
save(config, data)
print_entry(entry)
elif args.increase:
data, entry = add_path(load(config), get_pwd(), args.decrease)
data, entry = add_path(load(config), get_pwd(), args.increase)
save(config, data)
print_entry(entry)
elif args.purge:
@ -368,13 +375,8 @@ def main(args):
print_tab_menu(needle, tab_entries, TAB_SEPARATOR)
else:
# default behavior
try:
print(encode_local(surround_quotes(
first(find_matches(entries, needles)).path)))
except AttributeError:
# no results, always return something so the calling shell
# function has an argument to `cd` to
print(encode_local('.'))
print(encode_local(surround_quotes(
first(find_matches(entries, needles)).path)))
return 0

View File

@ -4,16 +4,22 @@ from __future__ import print_function
from codecs import open
from collections import namedtuple
from itertools import ifilter
from itertools import imap
from operator import itemgetter
import os
import shutil
import sys
from time import time
if sys.version_info[0] == 3:
ifilter = filter
imap = map
else:
from itertools import ifilter
from itertools import imap
from utils import create_dir
from utils import is_osx
from utils import is_python3
from utils import move_file
@ -36,6 +42,8 @@ def dictify(entries):
def entriefy(data):
"""Converts a dictionary into an iterator of entries."""
convert = lambda tup: Entry(*tup)
if is_python3():
return map(convert, data.items())
return imap(convert, data.iteritems())
@ -106,8 +114,11 @@ def save(config, data):
try:
# write to temp file
with open(config['tmp_path'], 'w', encoding='utf-8', errors='replace') as f:
for path, weight in data.iteritems():
f.write((unicode("%s\t%s\n" % (weight, path)).encode('utf-8')))
for path, weight in data.items():
if is_python3():
f.write(("%s\t%s\n" % (weight, path)))
else:
f.write((unicode("%s\t%s\n" % (weight, path)).encode('utf-8')))
f.flush()
os.fsync(f)

View File

@ -13,6 +13,14 @@ import shutil
import sys
import unicodedata
if sys.version_info[0] == 3:
ifilter = filter
imap = map
os.getcwdu = os.getcwd
else:
from itertools import ifilter
from itertools import imap
def create_dir(path):
"""Creates a directory atomically."""
@ -47,6 +55,8 @@ def encode_local(string, encoding=None):
def first(xs):
it = iter(xs)
try:
if is_python3():
return it.__next__()
return it.next()
except StopIteration:
return None
@ -84,6 +94,8 @@ def get_pwd():
def has_uppercase(string):
if is_python3():
return any(ch.isupper() for ch in string)
return any(unicodedata.category(c) == 'Lu' for c in unicode(string))
@ -95,6 +107,10 @@ def is_python2():
return sys.version_info[0] == 2
def is_python3():
return sys.version_info[0] == 3
def is_linux():
return platform.system() == 'Linux'
@ -129,8 +145,12 @@ def last(xs):
it = iter(xs)
tmp = None
try:
while True:
tmp = it.next()
if is_python3():
while True:
tmp = it.__next__()
else:
while True:
tmp = it.next()
except StopIteration:
return tmp
@ -173,7 +193,7 @@ def print_tab_menu(needle, tab_entries, separator):
def sanitize(directories):
clean = lambda x: decode(x).rstrip(os.sep)
return map(clean, directories)
return list(imap(clean, directories))
def second(xs):