From 9112dc97d99d5de7d47a6b830e5f379f94a42efc Mon Sep 17 00:00:00 2001 From: William Ting Date: Mon, 16 Dec 2013 20:27:41 -0600 Subject: [PATCH] exact match with single argument finished --- bin/autojump.py | 48 +++++++++++++++++++++++++++++++++++++++++++----- bin/utils.py | 17 ++++++++++++++++- 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/bin/autojump.py b/bin/autojump.py index 137feb2..5d1c69f 100755 --- a/bin/autojump.py +++ b/bin/autojump.py @@ -21,8 +21,12 @@ from __future__ import division, print_function +from collections import namedtuple +from functools import partial from itertools import ifilter +from itertools import imap from math import sqrt +from operator import attrgetter from operator import itemgetter import os import platform @@ -30,13 +34,16 @@ import sys from argparse import ArgumentParser -from data import save from data import load +from data import save from utils import decode +from utils import encode_local +from utils import first from utils import is_osx -from utils import print_dir +from utils import print_entry VERSION = 'release-v21.8.0' +Entry = namedtuple('Entry', ['path', 'weight']) def set_defaults(): @@ -114,7 +121,7 @@ def parse_args(config): if args.increase: try: - print_dir(add_path(config, os.getcwdu(), args.increase)) + print_entry(add_path(config, os.getcwdu(), args.increase)) sys.exit(0) except OSError: print("Current directory no longer exists.", file=sys.stderr) @@ -122,7 +129,7 @@ def parse_args(config): if args.decrease: try: - print_dir(decrease_path(config, os.getcwdu(), args.decrease)) + print_entry(decrease_path(config, os.getcwdu(), args.decrease)) sys.exit(0) except OSError: print("Current directory no longer exists.", file=sys.stderr) @@ -136,6 +143,9 @@ def parse_args(config): print_stats(config) sys.exit(0) + print(encode_local(find_matches(config, args.directory))) + sys.exit(0) + # if args.complete: # config['match_cnt'] = 9 # config['ignore_case'] = True @@ -172,6 +182,34 @@ def decrease_path(config, path, increment=15): return path, data[path] +def find_matches(config, needles, count=1): + """Return [count] paths matching needles.""" + entriefy = lambda tup: Entry(*tup) + exists = lambda entry: os.path.exists(entry.path) + data = sorted( + ifilter(exists, imap(entriefy, load(config).iteritems())), + key=attrgetter('weight'), + reverse=True) + + print(data[:3]) + + # if no arguments, return first path + if not needles: + return first(data).path + + sanitize = lambda x: decode(x).rstrip(os.sep) + needle = first(imap(sanitize, needles)) + + exact_matches = match_exact(needle, data) + + return first(exact_matches).path + + +def match_exact(needle, haystack): + find = lambda haystack: needle in haystack.path + return ifilter(find, haystack) + + def purge_missing_paths(config): """Remove non-existent paths.""" exists = lambda x: os.path.exists(x[0]) @@ -185,7 +223,7 @@ def print_stats(config): data = load(config) for path, weight in sorted(data.iteritems(), key=itemgetter(1)): - print_dir(path, weight) + print_entry(path, weight) print("________________________________________\n") print("%d:\t total weight" % sum(data.itervalues())) diff --git a/bin/utils.py b/bin/utils.py index aab2504..5ed3fe5 100644 --- a/bin/utils.py +++ b/bin/utils.py @@ -3,7 +3,9 @@ from __future__ import print_function +from collections import Iterable import errno +from itertools import islice import os import platform import shutil @@ -40,6 +42,14 @@ def encode_local(string, encoding=None): return string +def first(xs): + it = iter(xs) + try: + return it.next() + except StopIteration: + return None + + def is_python2(): return sys.version_info[0] == 2 @@ -69,5 +79,10 @@ def move_file(src, dst): shutil.move(src, dst) -def print_dir(path, weight): +def print_entry(path, weight): print(encode_local("%.1f:\t%s" % (weight, path))) + + +def take(n, iterable): + """Return first n items of an iterable.""" + return islice(iterable, n)