Format to conform to PEP 8

This is done via autopep8.
pull/141/head
myint 12 years ago
parent 1ab78ae91c
commit 2a499232ed

@ -43,7 +43,8 @@ TESTING = False
if 'AUTOJUMP_DATA_DIR' in os.environ:
CONFIG_DIR = os.environ.get('AUTOJUMP_DATA_DIR')
else:
xdg_data_dir = os.environ.get('XDG_DATA_HOME') or os.path.join(os.environ['HOME'], '.local', 'share')
xdg_data_dir = os.environ.get('XDG_DATA_HOME') or os.path.join(
os.environ['HOME'], '.local', 'share')
CONFIG_DIR = os.path.join(xdg_data_dir, 'autojump')
KEEP_ALL_ENTRIES = False
@ -63,6 +64,7 @@ if CONFIG_DIR == os.path.expanduser('~'):
else:
DB_FILE = CONFIG_DIR + '/autojump.txt'
class Database:
"""
Object for interfacing with autojump database.
@ -84,7 +86,8 @@ class Database:
self.data[path] = increment
else:
import math
self.data[path] = math.sqrt((self.data[path]**2)+(increment**2))
self.data[path] = math.sqrt((self.data[path] ** 2)
+ (increment ** 2))
self.save()
def decay(self):
@ -165,7 +168,8 @@ class Database:
for path, weight in sorted(self.data.items(),
key=itemgetter(1),
reverse=True):
temp.write((unico("%s\t%s\n")%(weight, path)).encode("utf-8"))
temp.write(
(unico("%s\t%s\n") % (weight, path)).encode("utf-8"))
# catching disk errors and skipping save when file handle can't be closed.
try:
@ -205,7 +209,8 @@ def options():
"""
global ARGS
parser = argparse.ArgumentParser(description='Automatically jump to directory passed as an argument.',
parser = argparse.ArgumentParser(
description='Automatically jump to directory passed as an argument.',
epilog="Please see autojump(1) man pages for full documentation.")
parser.add_argument('directory', metavar='DIR', nargs='*', default='',
help='directory to jump to')
@ -219,7 +224,8 @@ def options():
help='delete all database entries that no longer exist on system')
parser.add_argument('-s', '--stat', action="store_true", default=False,
help='show database entries and their key weights')
parser.add_argument('--version', action="version", version="%(prog)s " + VERSION,
parser.add_argument(
'--version', action="version", version="%(prog)s " + VERSION,
help='show version information and exit')
ARGS = parser.parse_args()
@ -251,6 +257,7 @@ def options():
return True
return False
def decode(text, encoding=None, errors="strict"):
"""
Decoding step for Python 2 which does not default to unicode.
@ -262,6 +269,7 @@ def decode(text, encoding=None, errors="strict"):
encoding = sys.getfilesystemencoding()
return text.decode(encoding, errors)
def output(unicode_text, encoding=None):
"""
Wrapper for the print function, using the filesystem encoding by default
@ -274,6 +282,7 @@ def output(unicode_text, encoding=None):
encoding = sys.getfilesystemencoding()
print(unicode_text.encode(encoding))
def unico(text):
"""
If Python 2, convert to a unicode object.
@ -283,6 +292,7 @@ def unico(text):
else:
return unicode(text)
def match_last(pattern):
"""
If the last pattern contains a full path, jump there.
@ -298,6 +308,7 @@ def match_last(pattern):
return True
return False
def match(path, pattern, only_end=False, ignore_case=False):
"""
Check whether a path matches a particular pattern, and return
@ -319,6 +330,7 @@ def match(path, pattern, only_end=False, ignore_case=False):
else:
return (False, path[find_idx + len(pattern):])
def find_matches(db, patterns, max_matches=1, ignore_case=False, fuzzy=False):
"""
Find max_matches paths that match the pattern, and add them to the result_list.
@ -370,7 +382,8 @@ def find_matches(db, patterns, max_matches=1, ignore_case=False, fuzzy=False):
found, tmp = match(tmp, p, True, ignore_case)
else:
found, tmp = match(tmp, p, False, ignore_case)
if not found: break
if not found:
break
if found and (os.path.exists(path) or TESTING):
if path not in results:
@ -379,11 +392,13 @@ def find_matches(db, patterns, max_matches=1, ignore_case=False, fuzzy=False):
break
return results
def shell_utility():
"""
Run this when autojump is called as a shell utility.
"""
if options(): return True
if options():
return True
db = Database(DB_FILE)
# if no directories, add empty string
@ -401,7 +416,8 @@ def shell_utility():
tab_match = re.search(COMPLETION_SEPARATOR + "([0-9]+)", patterns[-1])
if tab_match: # user has selected a tab completion entry
tab_choice = int(tab_match.group(1))
patterns[-1] = re.sub(COMPLETION_SEPARATOR+"[0-9]+.*", "", patterns[-1])
patterns[-1] = re.sub(COMPLETION_SEPARATOR + "[0-9]+.*",
"", patterns[-1])
else: # user hasn't selected a tab completion, display choices again
tab_match = re.match("(.*)" + COMPLETION_SEPARATOR, patterns[-1])
if tab_match:
@ -426,7 +442,8 @@ def shell_utility():
results = find_matches(db, patterns, max_matches, True, True)
quotes = ""
if ARGS.complete and ARGS.bash: quotes = "'"
if ARGS.complete and ARGS.bash:
quotes = "'"
if tab_choice != -1:
if len(results) > tab_choice - 1:
@ -446,4 +463,5 @@ def shell_utility():
return True
if __name__ == "__main__":
if not shell_utility(): sys.exit(1)
if not shell_utility():
sys.exit(1)

@ -14,16 +14,21 @@ import sys
import tempfile
import unittest
@contextlib.contextmanager
def no_stderr():
savestderr = sys.stderr
class DevNull(object):
def write(self, _): pass
def write(self, _):
pass
sys.stderr = DevNull()
yield
sys.stderr = savestderr
# test suite
class TestAutojump(unittest.TestCase):
def setUp(self):
@ -79,7 +84,8 @@ class TestAutojump(unittest.TestCase):
def test_db_load_backup(self):
# setup
fname = '/tmp/autojump_test_db_load_backup_' + str(random.randint(0,32678))
fname = '/tmp/autojump_test_db_load_backup_' + str(
random.randint(0, 32678))
db = autojump.Database(fname)
db.add('/1')
os.rename(fname, fname + '.bak')
@ -102,7 +108,8 @@ class TestAutojump(unittest.TestCase):
def test_db_save(self):
# setup
fname = '/tmp/autojump_test_db_save_' + str(random.randint(0,32678)) + '.txt'
fname = '/tmp/autojump_test_db_save_' + str(random.randint(
0, 32678)) + '.txt'
db = autojump.Database(fname)
try:
@ -182,8 +189,10 @@ class TestAutojump(unittest.TestCase):
self.db.add('/9')
patterns = [u'']
results = autojump.find_matches(self.db, patterns, max_matches, ignore_case)
self.assertEquals(results, ['/5', '/6', '/9', '/8', '/7', '/4', '/3', '/2', '/1'])
results = autojump.find_matches(
self.db, patterns, max_matches, ignore_case)
self.assertEquals(
results, ['/5', '/6', '/9', '/8', '/7', '/4', '/3', '/2', '/1'])
def test_match_case_insensitive(self):
max_matches = 1
@ -192,7 +201,8 @@ class TestAutojump(unittest.TestCase):
self.db.add('/foo', 10)
patterns = [u'fo']
results = autojump.find_matches(self.db, patterns, max_matches, ignore_case)
results = autojump.find_matches(
self.db, patterns, max_matches, ignore_case)
self.assertEquals(results[0], '/FOO')
def test_match_fuzzy(self):
@ -204,19 +214,23 @@ class TestAutojump(unittest.TestCase):
self.db.add('/abcdefg', 10)
patterns = [u'random']
results = autojump.find_matches(self.db, patterns, max_matches, ignore_case, fuzzy_search)
results = autojump.find_matches(
self.db, patterns, max_matches, ignore_case, fuzzy_search)
self.assertTrue(len(results) == 0)
patterns = [u'abcdefg']
results = autojump.find_matches(self.db, patterns, max_matches, ignore_case, fuzzy_search)
results = autojump.find_matches(
self.db, patterns, max_matches, ignore_case, fuzzy_search)
self.assertEquals(results[0], '/abcdefg')
patterns = [u'abcefg']
results = autojump.find_matches(self.db, patterns, max_matches, ignore_case, fuzzy_search)
results = autojump.find_matches(
self.db, patterns, max_matches, ignore_case, fuzzy_search)
self.assertEquals(results[0], '/abcdefg')
patterns = [u'bacef']
results = autojump.find_matches(self.db, patterns, max_matches, ignore_case, fuzzy_search)
results = autojump.find_matches(
self.db, patterns, max_matches, ignore_case, fuzzy_search)
self.assertEquals(results[0], '/abcdefg')
if __name__ == '__main__':

@ -13,14 +13,17 @@ from IPython.iplib import InteractiveShell
ip = get()
def magic_j(self, parameter_s=''):
cmd = ['autojump'] + parameter_s.split()
# print 'executing autojump with args %s' % str(cmd)
newpath=sub.Popen(cmd,stdout=sub.PIPE,shell=False).communicate()[0][:-1] # delete last '\n'
newpath = sub.Popen(cmd, stdout=sub.PIPE, shell=False).communicate(
)[0][:-1] # delete last '\n'
# print 'Autojump answer: \'%s\'' % newpath
if newpath:
ip.magic('cd \'%s\'' % newpath)
def cd_decorator(f):
def autojump_cd_monitor(self, parameter_s=''):
f(self, parameter_s)

Loading…
Cancel
Save