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

335 lines
11 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
2012-05-07 06:19:19 +00:00
"""
Copyright © 2008-2012 Joel Schaerer
2013-07-07 02:17:54 +00:00
Copyright © 2012-2013 William Ting
2012-05-07 06:19:19 +00:00
* This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
* This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
2013-12-17 18:51:39 +00:00
from __future__ import print_function
2013-12-17 21:57:36 +00:00
from difflib import SequenceMatcher
2013-12-17 02:28:54 +00:00
from functools import partial
2013-12-17 19:52:34 +00:00
from itertools import chain
2013-12-17 18:51:39 +00:00
# FIXME(ting|2013-12-17): fix imports for Python 3 compatability
2013-12-17 02:28:54 +00:00
from itertools import ifilter
from itertools import imap
from math import sqrt
from operator import attrgetter
from operator import itemgetter
import os
2013-12-17 02:28:54 +00:00
import platform
import re
import sys
2013-12-17 02:28:54 +00:00
from argparse import ArgumentParser
2013-12-17 20:48:12 +00:00
from data import dictify
from data import entriefy
from data import Entry
2013-12-17 02:28:54 +00:00
from data import load
from data import save
from utils import decode
from utils import encode_local
from utils import first
2013-12-17 20:48:12 +00:00
from utils import get_pwd
2013-12-17 16:35:43 +00:00
from utils import has_uppercase
2013-12-17 02:28:54 +00:00
from utils import is_osx
2013-12-17 21:57:36 +00:00
from utils import last
2013-12-17 02:28:54 +00:00
from utils import print_entry
from utils import second
2013-12-17 18:51:39 +00:00
from utils import take
2013-12-17 02:28:54 +00:00
VERSION = 'release-v21.8.0'
2013-12-17 21:57:36 +00:00
FUZZY_MATCH_THRESHOLD = 0.6
2013-07-07 01:23:34 +00:00
def set_defaults():
config = {}
2013-12-17 02:28:54 +00:00
config['tab_menu_separator'] = '__'
2013-12-17 02:28:54 +00:00
if is_osx():
data_home = os.path.join(
os.path.expanduser('~'),
'Library',
'autojump')
else:
data_home = os.getenv(
'XDG_DATA_HOME',
os.path.join(
os.path.expanduser('~'),
'.local',
'share',
'autojump'))
config['data_path'] = os.path.join(data_home, 'autojump.txt')
config['backup_path'] = os.path.join(data_home, 'autojump.txt.bak')
config['tmp_path'] = os.path.join(data_home, 'data.tmp')
return config
2013-12-17 18:03:57 +00:00
def parse_environment(config):
2013-12-17 02:28:54 +00:00
# TODO(ting|2013-12-16): add autojump_data_dir support
# TODO(ting|2013-12-15): add ignore case / smartcase support
# TODO(ting|2013-12-15): add symlink support
return config
2013-12-17 02:28:54 +00:00
2013-12-17 19:52:34 +00:00
def parse_arguments():
2013-12-17 18:03:57 +00:00
"""Evaluate arguments and run appropriate logic, returning an error code."""
2013-12-17 02:28:54 +00:00
parser = ArgumentParser(
2013-05-15 01:58:24 +00:00
description='Automatically jump to directory passed as an argument.',
epilog="Please see autojump(1) man pages for full documentation.")
2013-02-25 05:49:45 +00:00
parser.add_argument(
'directory', metavar='DIRECTORY', nargs='*', default='',
help='directory to jump to')
2013-02-25 05:49:45 +00:00
parser.add_argument(
'-a', '--add', metavar='DIRECTORY',
2013-12-17 02:28:54 +00:00
help='add path')
parser.add_argument(
'-i', '--increase', metavar='WEIGHT', nargs='?', type=int,
const=20, default=False,
2013-12-17 02:28:54 +00:00
help='increase current directory weight')
2013-02-25 05:49:45 +00:00
parser.add_argument(
'-d', '--decrease', metavar='WEIGHT', nargs='?', type=int,
const=15, default=False,
2013-12-17 02:28:54 +00:00
help='decrease current directory weight')
# parser.add_argument(
# '-b', '--bash', action="store_true", default=False,
# help='enclose directory quotes to prevent errors')
# parser.add_argument(
# '--complete', action="store_true", default=False,
# help='used for tab completion')
2013-02-25 05:49:45 +00:00
parser.add_argument(
'--purge', action="store_true", default=False,
2013-12-17 02:28:54 +00:00
help='remove non-existent paths from database')
2013-02-25 05:49:45 +00:00
parser.add_argument(
'-s', '--stat', action="store_true", default=False,
help='show database entries and their key weights')
2013-02-25 05:49:45 +00:00
parser.add_argument(
'-v', '--version', action="version", version="%(prog)s " +
2013-12-17 02:28:54 +00:00
VERSION, help='show version information')
2013-12-17 19:52:34 +00:00
return parser.parse_args()
2012-05-07 01:09:37 +00:00
2013-12-17 20:48:12 +00:00
def add_path(data, path, increment=10):
2013-12-17 15:52:41 +00:00
"""
Add a new path or increment an existing one.
os.path.realpath() is not used because users prefer to have short, symlinked
paths with duplicate entries in the database than a single canonical path.
"""
2013-12-17 02:28:54 +00:00
path = decode(path).rstrip(os.sep)
if path == os.path.expanduser('~'):
2013-12-17 20:48:12 +00:00
return data, Entry(path, 0)
2013-12-17 02:28:54 +00:00
if path in data:
data[path] = sqrt((data[path]**2) + (increment**2))
else:
2013-12-17 02:28:54 +00:00
data[path] = increment
2013-12-17 20:48:12 +00:00
return data, Entry(path, data[path])
2012-05-07 00:34:03 +00:00
2012-04-07 14:14:19 +00:00
2013-12-17 20:48:12 +00:00
def decrease_path(data, path, increment=15):
2013-12-17 02:28:54 +00:00
"""Decrease weight of existing path."""
path = decode(path).rstrip(os.sep)
data[path] = max(0, data[path]-increment)
2013-12-17 20:48:12 +00:00
return data, Entry(path, data[path])
2013-12-17 02:28:54 +00:00
2013-12-17 18:04:11 +00:00
def detect_smartcase(needles):
"""
If any needles contain an uppercase letter then use case sensitive
searching. Otherwise use case insensitive searching.
"""
return not any(imap(has_uppercase, needles))
2013-12-17 20:48:12 +00:00
def find_matches(entries, needles):
2013-12-17 19:52:34 +00:00
"""Return an iterator to matching entries."""
2013-12-17 20:48:12 +00:00
try:
not_cwd = lambda entry: entry.path != os.getcwdu()
except OSError:
# tautology if current working directory no longer exists
not_cwd = lambda x: True
2013-12-17 02:28:54 +00:00
data = sorted(
2013-12-17 20:48:12 +00:00
ifilter(not_cwd, entries),
2013-12-17 02:28:54 +00:00
key=attrgetter('weight'),
reverse=True)
if not needles:
return first(data).path
sanitize = lambda x: decode(x).rstrip(os.sep)
needles = map(sanitize, needles)
ignore_case = detect_smartcase(needles)
2013-12-17 02:28:54 +00:00
2013-12-17 19:52:34 +00:00
consecutive_matches = match_consecutive(needles, data, ignore_case)
2013-12-17 21:57:36 +00:00
quicksilver_matches = match_quicksilver(needles, data, ignore_case)
2013-12-17 19:52:34 +00:00
fuzzy_matches = match_fuzzy(needles, data, ignore_case)
exists = lambda entry: os.path.exists(entry.path)
2013-12-17 21:57:36 +00:00
return ifilter(
exists,
chain(consecutive_matches, quicksilver_matches, fuzzy_matches))
2013-12-17 02:28:54 +00:00
2013-12-17 17:54:40 +00:00
def match_consecutive(needles, haystack, ignore_case=False):
"""
Matches consecutive needles at the end of a path.
For example:
needles = ['foo', 'baz']
haystack = [
2013-12-17 21:57:36 +00:00
(path="/foo/bar/baz", weight=10),
(path="/foo/baz/moo", weight=10),
(path="/moo/foo/baz", weight=10),
(path="/foo/baz", weight=10)]
2013-12-17 17:54:40 +00:00
2013-12-17 18:51:39 +00:00
regex_needle = re.compile(r'''
2013-12-17 17:54:40 +00:00
foo # needle #1
[^/]* # all characters except os.sep zero or more times
/ # os.sep
[^/]* # all characters except os.sep zero or more times
baz # needle #2
[^/]* # all characters except os.sep zero or more times
$ # end of string
2013-12-17 18:51:39 +00:00
''')
2013-12-17 17:54:40 +00:00
result = [
2013-12-17 21:57:36 +00:00
(path="/moo/foo/baz", weight=10),
(path="/foo/baz", weight=10)]
2013-12-17 17:54:40 +00:00
"""
regex_no_sep = '[^' + os.sep + ']*'
regex_one_sep = regex_no_sep + os.sep + regex_no_sep
regex_no_sep_end = regex_no_sep + '$'
# can't use compiled regex because of flags
2013-12-17 18:51:39 +00:00
regex_needle = regex_one_sep.join(needles) + regex_no_sep_end
2013-12-17 17:54:40 +00:00
regex_flags = re.IGNORECASE | re.UNICODE if ignore_case else re.UNICODE
2013-12-17 21:57:36 +00:00
found = lambda entry: re.search(
2013-12-17 18:51:39 +00:00
regex_needle,
2013-12-17 21:57:36 +00:00
entry.path,
2013-12-17 17:54:40 +00:00
flags=regex_flags)
return ifilter(found, haystack)
2013-12-17 19:52:34 +00:00
def match_fuzzy(needles, haystack, ignore_case=False):
2013-12-17 21:57:36 +00:00
"""
Performs an approximate match with the last needle against the end of
every path past an acceptable threshold (FUZZY_MATCH_THRESHOLD).
For example:
needles = ['foo', 'bar']
haystack = [
(path="/foo/bar/baz", weight=11),
(path="/foo/baz/moo", weight=10),
(path="/moo/foo/baz", weight=10),
(path="/foo/baz", weight=10),
(path="/foo/bar", weight=10)]
result = [
(path="/foo/bar/baz", weight=11),
(path="/moo/foo/baz", weight=10),
(path="/foo/baz", weight=10),
(path="/foo/bar", weight=10)]
This is a weak heuristic and be used as a last resort to find matches.
"""
needle = last(needles)
end_dir = lambda path: last(os.path.split(path))
match_percent = lambda entry: SequenceMatcher(
a=needle,
b=end_dir(entry.path)).ratio()
meets_threshold = lambda entry: match_percent(entry) \
>= FUZZY_MATCH_THRESHOLD
return ifilter(meets_threshold, haystack)
def match_quicksilver(needles, haystack, ignore_case=False):
2013-12-17 15:52:30 +00:00
"""
"""
2013-12-17 19:52:34 +00:00
return []
2013-12-17 02:28:54 +00:00
2013-12-17 20:48:12 +00:00
def purge_missing_paths(entries):
"""Remove non-existent paths from a list of entries."""
exists = lambda entry: os.path.exists(entry.path)
return ifilter(exists, entries)
2013-12-17 02:28:54 +00:00
2013-12-17 20:48:12 +00:00
def print_stats(data, data_path):
2013-12-17 02:28:54 +00:00
for path, weight in sorted(data.iteritems(), key=itemgetter(1)):
2013-12-17 20:48:12 +00:00
print_entry(Entry(path, weight))
2013-12-17 02:28:54 +00:00
print("________________________________________\n")
print("%d:\t total weight" % sum(data.itervalues()))
print("%d:\t number of entries" % len(data))
try:
print("%.2f:\t current directory weight" % data.get(os.getcwdu(), 0))
except OSError:
pass
2013-12-17 20:48:12 +00:00
print("\ndata:\t %s" % data_path)
2013-12-17 02:28:54 +00:00
def main():
2013-12-17 19:52:34 +00:00
config = parse_environment(set_defaults())
args = parse_arguments()
if args.add:
2013-12-17 20:48:12 +00:00
save(config, first(add_path(load(config), args.add)))
2013-12-17 19:52:34 +00:00
# elif args.complete:
# config['match_cnt'] = 9
# config['ignore_case'] = True
elif args.decrease:
2013-12-17 20:48:12 +00:00
data, entry = decrease_path(load(config), get_pwd(), args.decrease)
save(config, data)
print_entry(entry)
2013-12-17 19:52:34 +00:00
elif args.increase:
2013-12-17 20:48:12 +00:00
data, entry = add_path(load(config), get_pwd(), args.decrease)
save(config, data)
print_entry(entry)
2013-12-17 19:52:34 +00:00
elif args.purge:
2013-12-17 20:48:12 +00:00
old_data = load(config)
new_data = dictify(purge_missing_paths(entriefy(old_data)))
save(config, new_data)
print("Purged %d entries." % (len(old_data)-len(new_data)))
2013-12-17 19:52:34 +00:00
elif args.stat:
2013-12-17 20:48:12 +00:00
print_stats(load(config), config['data_path'])
2013-12-17 19:52:34 +00:00
else:
# default behavior, no optional arguments
2013-12-17 20:48:12 +00:00
result = first(find_matches(entriefy(load(config)), args.directory))
2013-12-17 19:52:34 +00:00
if result:
2013-12-17 22:06:27 +00:00
print(encode_local(surround_quotes(result.path)))
2013-12-17 19:52:34 +00:00
else:
# always return something so the calling shell function has an
# argument to `cd` to
print(encode_local('.'))
return 0
2013-12-17 18:03:57 +00:00
2012-04-07 14:14:19 +00:00
if __name__ == "__main__":
sys.exit(main())