mirror of
				https://github.com/wting/autojump
				synced 2025-06-13 12:54:07 +00:00 
			
		
		
		
	finished add directory, stats, remove bad entries
This commit is contained in:
		
							parent
							
								
									8cbd19cc79
								
							
						
					
					
						commit
						694d150882
					
				
							
								
								
									
										188
									
								
								bin/autojump.py
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										188
									
								
								bin/autojump.py
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,188 @@ | ||||
| #!/usr/bin/env python | ||||
| # -*- coding: utf-8 -*- | ||||
| """ | ||||
|   Copyright © 2008-2012 Joel Schaerer | ||||
|   Copyright © 2012-2013 William Ting | ||||
| 
 | ||||
|   *  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. | ||||
| """ | ||||
| 
 | ||||
| from __future__ import division, print_function | ||||
| 
 | ||||
| from math import sqrt | ||||
| from operator import itemgetter | ||||
| import os | ||||
| import platform | ||||
| import sys | ||||
| 
 | ||||
| from argparse import ArgumentParser | ||||
| 
 | ||||
| from data import save | ||||
| from data import load | ||||
| from utils import decode | ||||
| from utils import encode_local | ||||
| from utils import is_osx | ||||
| 
 | ||||
| VERSION = 'release-v21.8.0' | ||||
| 
 | ||||
| 
 | ||||
| def set_defaults(): | ||||
|     config = {} | ||||
|     config['tab_menu_separator'] = '__' | ||||
| 
 | ||||
|     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 | ||||
| 
 | ||||
| 
 | ||||
| def parse_env(config): | ||||
|     # TODO(ting|2013-12-15): what if data_home doesn't exist? | ||||
|     # if 'AUTOJUMP_DATA_DIR' in os.environ: | ||||
|         # config['data_home'] = os.environ.get('AUTOJUMP_DATA_DIR') | ||||
|         # config['data_file'] = os.path.join(config['data_home'], 'autojump.pkl') | ||||
| 
 | ||||
|     # TODO(ting|2013-12-15): add ignore case / smartcase support | ||||
|     # TODO(ting|2013-12-15): add symlink support | ||||
| 
 | ||||
|     return config | ||||
| 
 | ||||
| 
 | ||||
| def parse_args(config): | ||||
|     parser = 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='DIRECTORY', nargs='*', default='', | ||||
|             help='directory to jump to') | ||||
|     parser.add_argument( | ||||
|             '-a', '--add', metavar='DIRECTORY', | ||||
|             help='add path') | ||||
|     # parser.add_argument( | ||||
|             # '-i', '--increase', metavar='WEIGHT', nargs='?', type=int, | ||||
|             # const=20, default=False, | ||||
|             # help='manually increase path weight in database') | ||||
|     # parser.add_argument( | ||||
|             # '-d', '--decrease', metavar='WEIGHT', nargs='?', type=int, | ||||
|             # const=15, default=False, | ||||
|             # help='manually decrease path weight in database') | ||||
|     # 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') | ||||
|     # parser.add_argument( | ||||
|             # '--purge', action="store_true", default=False, | ||||
|             # 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( | ||||
|             '-v', '--version', action="version", version="%(prog)s " + | ||||
|             VERSION, help='show version information') | ||||
| 
 | ||||
|     args = parser.parse_args() | ||||
| 
 | ||||
|     if args.add: | ||||
|         add_path(config, args.add) | ||||
|         sys.exit(0) | ||||
| 
 | ||||
|     # if args.increase: | ||||
|         # print("%.2f:\t old directory weight" % db.get_weight(os.getcwd())) | ||||
|         # db.add(os.getcwd(), args.increase) | ||||
|         # print("%.2f:\t new directory weight" % db.get_weight(os.getcwd())) | ||||
|         # sys.exit(0) | ||||
| 
 | ||||
|     # if args.decrease: | ||||
|         # print("%.2f:\t old directory weight" % db.get_weight(os.getcwd())) | ||||
|         # db.decrease(os.getcwd(), args.decrease) | ||||
|         # print("%.2f:\t new directory weight" % db.get_weight(os.getcwd())) | ||||
|         # sys.exit(0) | ||||
| 
 | ||||
|     # if args.purge: | ||||
|         # removed = db.purge() | ||||
| 
 | ||||
|         # if len(removed): | ||||
|             # for dir in removed: | ||||
|                 # output(dir) | ||||
| 
 | ||||
|         # print("Number of database entries removed: %d" % len(removed)) | ||||
| 
 | ||||
|         # sys.exit(0) | ||||
| 
 | ||||
|     if args.stat: | ||||
|         print_stats(config) | ||||
|         sys.exit(0) | ||||
| 
 | ||||
|     # if args.complete: | ||||
|         # config['match_cnt'] = 9 | ||||
|         # config['ignore_case'] = True | ||||
| 
 | ||||
|     # config['args'] = args | ||||
|     return config | ||||
| 
 | ||||
| 
 | ||||
| def add_path(config, path, increment=10): | ||||
|     """Add a new path or increment an existing one.""" | ||||
|     path = decode(path).rstrip(os.sep) | ||||
|     if path == os.path.expanduser('~'): | ||||
|         return | ||||
| 
 | ||||
|     data = load(config) | ||||
| 
 | ||||
|     if path in data: | ||||
|         data[path] = sqrt((data[path]**2) + (increment**2)) | ||||
|     else: | ||||
|         data[path] = increment | ||||
| 
 | ||||
|     save(config, data) | ||||
| 
 | ||||
| 
 | ||||
| def print_stats(config): | ||||
|     data = load(config) | ||||
| 
 | ||||
|     for path, weight in sorted(data.iteritems(), key=itemgetter(1)): | ||||
|         print(encode_local("%.1f:\t%s" % (weight, path))) | ||||
| 
 | ||||
|     print("________________________________________\n") | ||||
|     print("%d:\t total weight" % sum(data.itervalues())) | ||||
|     print("%d:\t number of entries" % len(data)) | ||||
|     print("%.2f:\t current directory weight" % data.get(os.getcwd(), 0)) | ||||
| 
 | ||||
|     print("\ndata:\t %s" % config['data_path']) | ||||
| 
 | ||||
| 
 | ||||
| def main(): | ||||
|     config = parse_args(parse_env(set_defaults())) | ||||
|     return 0 | ||||
| 
 | ||||
| if __name__ == "__main__": | ||||
|     sys.exit(main()) | ||||
							
								
								
									
										12
									
								
								bin/data.py
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								bin/data.py
									
									
									
									
									
								
							| @ -3,6 +3,7 @@ | ||||
| from __future__ import print_function | ||||
| 
 | ||||
| from codecs import open | ||||
| from itertools import ifilter | ||||
| from itertools import imap | ||||
| from operator import itemgetter | ||||
| import os | ||||
| @ -37,11 +38,11 @@ def load(config): | ||||
| 
 | ||||
|         # example: u'10.0\t/home/user\n' -> ['10.0', u'/home/user'] | ||||
|         parse = lambda x: x.strip().split('\t') | ||||
| 
 | ||||
|         # example: ['10.0', u'/home/user'] -> (u'/home/user', 10.0) | ||||
|         convert = lambda x: (x[1], float(x[0])) | ||||
|         correct_length = lambda x: len(x) == 2 | ||||
| 
 | ||||
|         return dict(imap(convert, imap(parse, lines))) | ||||
|         return dict(imap(convert, ifilter(correct_length, imap(parse, lines)))) | ||||
|     return {} | ||||
| 
 | ||||
| 
 | ||||
| @ -83,10 +84,7 @@ def save(config, data): | ||||
|     try: | ||||
|         # write to temp file | ||||
|         with open(config['tmp_path'], 'w', encoding='utf-8', errors='replace') as f: | ||||
|             for path, weight in sorted( | ||||
|                     data.iteritems(), | ||||
|                     key=itemgetter(1), | ||||
|                     reverse=True): | ||||
|             for path, weight in data.iteritems(): | ||||
|                 f.write((unicode("%s\t%s\n" % (weight, path)).encode('utf-8'))) | ||||
| 
 | ||||
|             f.flush() | ||||
| @ -101,4 +99,4 @@ def save(config, data): | ||||
|         move_file(config['data_path'], config['backup_path']) | ||||
| 
 | ||||
|     # move temp_file -> autojump.txt | ||||
|     move_file(temp_file.name, config['data_path']) | ||||
|     move_file(config['tmp_path'], config['data_path']) | ||||
|  | ||||
| @ -3,8 +3,10 @@ | ||||
| 
 | ||||
| from __future__ import print_function | ||||
| 
 | ||||
| import errno | ||||
| import os | ||||
| import platform | ||||
| import shutil | ||||
| import sys | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user