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:
parent
d20ec4afb4
commit
1c8a4c13cd
36
bin/autojump
36
bin/autojump
@ -24,9 +24,6 @@ from __future__ import print_function
|
|||||||
from difflib import SequenceMatcher
|
from difflib import SequenceMatcher
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from itertools import chain
|
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 math import sqrt
|
||||||
from operator import attrgetter
|
from operator import attrgetter
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
@ -35,6 +32,14 @@ import platform
|
|||||||
import re
|
import re
|
||||||
import sys
|
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 argparse import ArgumentParser
|
||||||
|
|
||||||
from data import dictify
|
from data import dictify
|
||||||
@ -167,14 +172,12 @@ def find_matches(entries, needles):
|
|||||||
# tautology if current working directory no longer exists
|
# tautology if current working directory no longer exists
|
||||||
not_cwd = lambda x: True
|
not_cwd = lambda x: True
|
||||||
|
|
||||||
|
# from pprint import pprint as pp; import pdb; pdb.set_trace()
|
||||||
data = sorted(
|
data = sorted(
|
||||||
ifilter(not_cwd, entries),
|
ifilter(not_cwd, entries),
|
||||||
key=attrgetter('weight'),
|
key=attrgetter('weight'),
|
||||||
reverse=True)
|
reverse=True)
|
||||||
|
|
||||||
if not needles:
|
|
||||||
return first(data).path
|
|
||||||
|
|
||||||
ignore_case = detect_smartcase(needles)
|
ignore_case = detect_smartcase(needles)
|
||||||
|
|
||||||
exists = lambda entry: os.path.exists(entry.path)
|
exists = lambda entry: os.path.exists(entry.path)
|
||||||
@ -184,7 +187,10 @@ def find_matches(entries, needles):
|
|||||||
match_consecutive(needles, data, ignore_case),
|
match_consecutive(needles, data, ignore_case),
|
||||||
match_quicksilver(needles, data, ignore_case),
|
match_quicksilver(needles, data, ignore_case),
|
||||||
match_fuzzy(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):
|
def match_anywhere(needles, haystack, ignore_case=False):
|
||||||
@ -299,16 +305,17 @@ def purge_missing_paths(entries):
|
|||||||
|
|
||||||
|
|
||||||
def print_stats(data, data_path):
|
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_entry(Entry(path, weight))
|
||||||
|
|
||||||
print("________________________________________\n")
|
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))
|
print("%d:\t number of entries" % len(data))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print("%.2f:\t current directory weight" % data.get(os.getcwdu(), 0))
|
print("%.2f:\t current directory weight" % data.get(os.getcwdu(), 0))
|
||||||
except OSError:
|
except OSError:
|
||||||
|
# current directory no longer exists
|
||||||
pass
|
pass
|
||||||
|
|
||||||
print("\ndata:\t %s" % data_path)
|
print("\ndata:\t %s" % data_path)
|
||||||
@ -330,7 +337,7 @@ def main(args):
|
|||||||
save(config, data)
|
save(config, data)
|
||||||
print_entry(entry)
|
print_entry(entry)
|
||||||
elif args.increase:
|
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)
|
save(config, data)
|
||||||
print_entry(entry)
|
print_entry(entry)
|
||||||
elif args.purge:
|
elif args.purge:
|
||||||
@ -368,13 +375,8 @@ def main(args):
|
|||||||
print_tab_menu(needle, tab_entries, TAB_SEPARATOR)
|
print_tab_menu(needle, tab_entries, TAB_SEPARATOR)
|
||||||
else:
|
else:
|
||||||
# default behavior
|
# default behavior
|
||||||
try:
|
print(encode_local(surround_quotes(
|
||||||
print(encode_local(surround_quotes(
|
first(find_matches(entries, needles)).path)))
|
||||||
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('.'))
|
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
19
bin/data.py
19
bin/data.py
@ -4,16 +4,22 @@ from __future__ import print_function
|
|||||||
|
|
||||||
from codecs import open
|
from codecs import open
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from itertools import ifilter
|
|
||||||
from itertools import imap
|
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
from time import time
|
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 create_dir
|
||||||
from utils import is_osx
|
from utils import is_osx
|
||||||
|
from utils import is_python3
|
||||||
from utils import move_file
|
from utils import move_file
|
||||||
|
|
||||||
|
|
||||||
@ -36,6 +42,8 @@ def dictify(entries):
|
|||||||
def entriefy(data):
|
def entriefy(data):
|
||||||
"""Converts a dictionary into an iterator of entries."""
|
"""Converts a dictionary into an iterator of entries."""
|
||||||
convert = lambda tup: Entry(*tup)
|
convert = lambda tup: Entry(*tup)
|
||||||
|
if is_python3():
|
||||||
|
return map(convert, data.items())
|
||||||
return imap(convert, data.iteritems())
|
return imap(convert, data.iteritems())
|
||||||
|
|
||||||
|
|
||||||
@ -106,8 +114,11 @@ def save(config, data):
|
|||||||
try:
|
try:
|
||||||
# write to temp file
|
# write to temp file
|
||||||
with open(config['tmp_path'], 'w', encoding='utf-8', errors='replace') as f:
|
with open(config['tmp_path'], 'w', encoding='utf-8', errors='replace') as f:
|
||||||
for path, weight in data.iteritems():
|
for path, weight in data.items():
|
||||||
f.write((unicode("%s\t%s\n" % (weight, path)).encode('utf-8')))
|
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()
|
f.flush()
|
||||||
os.fsync(f)
|
os.fsync(f)
|
||||||
|
26
bin/utils.py
26
bin/utils.py
@ -13,6 +13,14 @@ import shutil
|
|||||||
import sys
|
import sys
|
||||||
import unicodedata
|
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):
|
def create_dir(path):
|
||||||
"""Creates a directory atomically."""
|
"""Creates a directory atomically."""
|
||||||
@ -47,6 +55,8 @@ def encode_local(string, encoding=None):
|
|||||||
def first(xs):
|
def first(xs):
|
||||||
it = iter(xs)
|
it = iter(xs)
|
||||||
try:
|
try:
|
||||||
|
if is_python3():
|
||||||
|
return it.__next__()
|
||||||
return it.next()
|
return it.next()
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
return None
|
return None
|
||||||
@ -84,6 +94,8 @@ def get_pwd():
|
|||||||
|
|
||||||
|
|
||||||
def has_uppercase(string):
|
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))
|
return any(unicodedata.category(c) == 'Lu' for c in unicode(string))
|
||||||
|
|
||||||
|
|
||||||
@ -95,6 +107,10 @@ def is_python2():
|
|||||||
return sys.version_info[0] == 2
|
return sys.version_info[0] == 2
|
||||||
|
|
||||||
|
|
||||||
|
def is_python3():
|
||||||
|
return sys.version_info[0] == 3
|
||||||
|
|
||||||
|
|
||||||
def is_linux():
|
def is_linux():
|
||||||
return platform.system() == 'Linux'
|
return platform.system() == 'Linux'
|
||||||
|
|
||||||
@ -129,8 +145,12 @@ def last(xs):
|
|||||||
it = iter(xs)
|
it = iter(xs)
|
||||||
tmp = None
|
tmp = None
|
||||||
try:
|
try:
|
||||||
while True:
|
if is_python3():
|
||||||
tmp = it.next()
|
while True:
|
||||||
|
tmp = it.__next__()
|
||||||
|
else:
|
||||||
|
while True:
|
||||||
|
tmp = it.next()
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
return tmp
|
return tmp
|
||||||
|
|
||||||
@ -173,7 +193,7 @@ def print_tab_menu(needle, tab_entries, separator):
|
|||||||
|
|
||||||
def sanitize(directories):
|
def sanitize(directories):
|
||||||
clean = lambda x: decode(x).rstrip(os.sep)
|
clean = lambda x: decode(x).rstrip(os.sep)
|
||||||
return map(clean, directories)
|
return list(imap(clean, directories))
|
||||||
|
|
||||||
|
|
||||||
def second(xs):
|
def second(xs):
|
||||||
|
Loading…
Reference in New Issue
Block a user