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

add purge feature

This commit is contained in:
William Ting 2013-12-16 16:54:27 -06:00
parent bde9ac1d9f
commit 95c81b7665
2 changed files with 24 additions and 19 deletions

View File

@ -21,6 +21,7 @@
from __future__ import division, print_function from __future__ import division, print_function
from itertools import ifilter
from math import sqrt from math import sqrt
from operator import itemgetter from operator import itemgetter
import os import os
@ -32,8 +33,8 @@ from argparse import ArgumentParser
from data import save from data import save
from data import load from data import load
from utils import decode from utils import decode
from utils import encode_local
from utils import is_osx from utils import is_osx
from utils import print_dir
VERSION = 'release-v21.8.0' VERSION = 'release-v21.8.0'
@ -99,9 +100,9 @@ def parse_args(config):
# parser.add_argument( # parser.add_argument(
# '--complete', action="store_true", default=False, # '--complete', action="store_true", default=False,
# help='used for tab completion') # help='used for tab completion')
# parser.add_argument( parser.add_argument(
# '--purge', action="store_true", default=False, '--purge', action="store_true", default=False,
# help='delete all database entries that no longer exist on system') help='remove non-existent paths from database')
parser.add_argument( parser.add_argument(
'-s', '--stat', action="store_true", default=False, '-s', '--stat', action="store_true", default=False,
help='show database entries and their key weights') help='show database entries and their key weights')
@ -116,25 +117,16 @@ def parse_args(config):
sys.exit(0) sys.exit(0)
if args.increase: if args.increase:
path, weight = add_path(config, os.getcwd(), args.increase) print_dir(add_path(config, os.getcwd(), args.increase))
print(encode_local("%.1f:\t%s" % (weight, path)))
sys.exit(0) sys.exit(0)
if args.decrease: if args.decrease:
path, weight = decrease_path(config, os.getcwd(), args.decrease) print_dir(decrease_path(config, os.getcwd(), args.decrease))
print(encode_local("%.1f:\t%s" % (weight, path)))
sys.exit(0) sys.exit(0)
# if args.purge: if args.purge:
# removed = db.purge() print("Purged %d entries." % purge_missing_paths(config))
sys.exit(0)
# if len(removed):
# for dir in removed:
# output(dir)
# print("Number of database entries removed: %d" % len(removed))
# sys.exit(0)
if args.stat: if args.stat:
print_stats(config) print_stats(config)
@ -176,11 +168,20 @@ def decrease_path(config, path, increment=15):
return path, data[path] return path, data[path]
def purge_missing_paths(config):
"""Remove non-existent paths."""
exists = lambda x: os.path.exists(x[0])
old_data = load(config)
new_data = dict(ifilter(exists, old_data.iteritems()))
save(config, new_data)
return len(old_data) - len(new_data)
def print_stats(config): def print_stats(config):
data = load(config) data = load(config)
for path, weight in sorted(data.iteritems(), key=itemgetter(1)): for path, weight in sorted(data.iteritems(), key=itemgetter(1)):
print(encode_local("%.1f:\t%s" % (weight, path))) print_dir(path, weight)
print("________________________________________\n") print("________________________________________\n")
print("%d:\t total weight" % sum(data.itervalues())) print("%d:\t total weight" % sum(data.itervalues()))

View File

@ -67,3 +67,7 @@ def move_file(src, dst):
# raises exception if file is in use on Windows # raises exception if file is in use on Windows
os.remove(dst) os.remove(dst)
shutil.move(src, dst) shutil.move(src, dst)
def print_dir(path, weight):
print(encode_local("%.1f:\t%s" % (weight, path)))