Skip to content

Instantly share code, notes, and snippets.

@sanand0
Created April 4, 2010 11:41
Show Gist options
  • Save sanand0/355342 to your computer and use it in GitHub Desktop.
Save sanand0/355342 to your computer and use it in GitHub Desktop.
Opens files wherever they are in your machine. Change to any directory from anywhere.
@echo off
REM = """
REM python -x skipped that first line...
REM make sure that this file is saved with Windows newlines
REM make sure to change the next lines to whatever this file is called:
set FOLDER=D:/Apps/Misc
python -x %FOLDER%/o.cmd %*
if errorlevel == 1 goto changedir
goto end
"""
import os, os.path, re, sys
folders = (
('', 'D:\\blog', 0),
('', 'D:\\Anand', 9),
('', 'D:\\Entertainment\\Books', 9),
('', 'D:\\Entertainment\\Non-Fiction', 9),
('', 'D:\\My Music', 9),
('', 'D:\\Infy', 9),
('', 'D:\\site', 3),
('', 'D:\\My Dropbox', 9),
('', 'D:\\code', 2),
('', 'D:\\ext', 2),
(r'(exe|bat)$', 'D:\\xampp', 0),
(r'(exe|bat)$', 'D:\\Apps', 2),
(r'exe$', 'C:\\Program Files', 2),
)
current_dir = ('', '.', 0)
fallback = (
current_dir,
('', 'D:\\Downloads', 0),
('', 'D:\\temp', 0),
('', 'D:\\Entertainment\\Non-Fiction', 9),
)
exclude_dirs = re.compile(r'^\.|^Archive', re.IGNORECASE)
exclude_files = re.compile(r'^\.|unins', re.IGNORECASE)
path, file = os.path.split(__file__)
config_file = os.path.join(path, 'o.index')
if not os.path.exists(config_file): open(config_file, 'w').close()
file_index = open(config_file).readlines()
used_file = os.path.join(path, 'o.shortcuts')
if not os.path.exists(used_file): open(used_file, 'w').close()
used_index = dict(line.split('\t')[:2] for line in open(used_file))
dir_index = sorted(dict(((os.path.split(file)[0], 1) for file in file_index)).keys())
def get_files(folders):
for condition, folder, depth in folders:
print folder
match = condition and re.compile(condition) or None
for root, dirs, files in os.walk(folder):
level = root.count(os.path.sep) - folder.count(os.path.sep)
for i in xrange(len(dirs), 0, -1):
if level >= depth or exclude_dirs.search(dirs[i-1]): del dirs[i-1]
for file in files:
if exclude_files.search(file): continue
if match:
if match.search(file): yield os.path.join(root, file) + '\n'
else:
yield os.path.join(root, file) + '\n'
def run_file(file):
file = file.strip()
print "Opening", file
path, name = os.path.split(file)
os.chdir(path)
os.startfile(file)
def change_dir(folder):
'''Todo: Figure out how to change directory of the command prompt'''
open(os.path.join(path, '__cd.bat'), 'w').write('cd "%s"' % folder)
print
sys.exit(1)
def exactmatch(file, words):
return file.lower().endswith('\\' + ' '.join(words).lower())
def filter_index(index, words, nofallback = False, command=run_file):
'''Filter the index using the words, and run the only program left'''
# Check if already used
phrase = ' '.join(words).lower()
result = used_index.get(phrase, None)
if result:
command(result)
return
# Filter out all files not matching the words
for word in words:
p = re.compile(word, re.IGNORECASE)
for i in xrange(len(index), 0, -1):
if not p.search(index[i-1]): del index[i-1]
index = sorted(index, key=lambda file:not(exactmatch(file, words)))
exactmatches = [file for file in index if exactmatch(file, words)]
if len(exactmatches) >= 1: index = exactmatches # If there are exact matches, just stick to them
if len(index) > 1: # If there's more than one choice,
for i, file in enumerate(index[:10]): # Show the first 10
print " ", i, file.strip()
choice = raw_input('> (0-9, q, any word): ').strip() # Get the user's choice
if re.match(r'^\d$', choice): # If a number,
result = index[int(choice)]
command(result) # run that programe
open(used_file, 'a').write(phrase+'\t'+result) # and save it as a used result
elif choice == 'q':
return # If 'q', quit
else:
filter_index(index, words + choice.split()) # If words, filter further
elif len(index) == 1: # If there's only one choice,
command(index[0]) # Run it
else:
if not nofallback: filter_index(list(get_files(fallback)), words,
nofallback = True)
if len(sys.argv) > 1 and sys.argv[1] == '-index': open(config_file, 'w').writelines(get_files(folders))
elif len(sys.argv) > 1 and sys.argv[1] == 'cd': filter_index(dir_index, sys.argv[2:], command=change_dir)
elif len(sys.argv) > 1 and sys.argv[1] == '.': filter_index(list(get_files([current_dir])), sys.argv[2:], nofallback=True)
else: filter_index(file_index, sys.argv[1:])
"""
:changedir
call %FOLDER%/__cd.bat
:end """
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment