Did a refactoring to incorporate kragniz's work a little more delicately.

This commit is contained in:
Chris Lane 2013-08-18 15:53:40 -04:00
parent 54f379eda5
commit 26e5261713
2 changed files with 24 additions and 47 deletions

71
cheat
View File

@ -1,69 +1,46 @@
#!/usr/bin/env python #!/usr/bin/env python
import os import os
import sys import sys
import cheatsheets
try: def cheat_directories():
# check to see if the cheat package is available default = [ default_dir for default_dir in [os.path.expanduser('~/.cheat'), cheatsheets.cheat_dir] if os.path.isdir(default_dir) ]
import cheatsheets if 'CHEATPATH' in os.environ and os.environ['CHEATPATH']:
cheat_dir = cheatsheets.cheat_dir return [ path for path in os.environ['CHEATPATH'].split(os.pathsep) if os.path.isdir(path) ] + default
cheatsheets = [(f, cheat_dir) for f in os.listdir(cheat_dir) if '.' not in f] else:
except ImportError: return default
cheatsheets = []
# construct the path to the cheat directory def cheat_files(cheat_directories):
user_cheat_dir = os.path.join(os.path.expanduser('~'), '.cheat') cheats = {}
for cheat_dir in reversed(cheat_directories):
# list the files in the cheat directory cheats.update(dict([ (cheat, cheat_dir) for cheat in os.listdir(cheat_dir) if '.' not in cheat ]))
# add the user's cheat files if they have a ~/.cheat directory return cheats
if os.path.isdir(user_cheat_dir):
cheatsheets += [(f, user_cheat_dir) for f in os.listdir(user_cheat_dir)
if '.' not in f]
# add the cheat files from the directory specified in $CHEATPATH
if 'CHEATPATH' in os.environ and os.environ['CHEATPATH']:
path = os.environ['CHEATPATH']
if os.path.isdir(path):
cheatsheets += [(f, path) for f in os.listdir(path) if '.' not in f]
# remove any duplicates
def remove_duplicates(cheats):
sheets = []
for i, sheet in enumerate(cheats):
if sheet[0] not in [c[0] for c in sheets]:
sheets.append(sheet)
return sheets
cheatsheets = remove_duplicates(cheatsheets)
cheatsheets.sort()
# assemble a keyphrase out of all params passed to the script # assemble a keyphrase out of all params passed to the script
keyphrase = ' '.join(sys.argv[1:]) keyphrase = ' '.join(sys.argv[1:])
cheat_dirs = cheat_directories()
# verify that we have at least one cheat directory # verify that we have at least one cheat directory
if not cheatsheets: if not cheat_dirs:
print >> sys.stderr, 'The ~/.cheat directory does not exist or the CHEATPATH variable is not set.' print >> sys.stderr, 'The ~/.cheat directory does not exist or the CHEATPATH variable is not set.'
exit() exit()
# assemble a keyphrase out of all params passed to the script # list the files in the ~/.cheat directory
keyphrase = ' '.join(sys.argv[1:]) cheatsheets = cheat_files(cheat_dirs)
# print help if requested # print help if requested
if keyphrase.lower() in ['', 'cheat', 'help', '-h', '-help', '--help']: if keyphrase.lower() in ['', 'cheat', 'help', '-h', '-help', '--help']:
print "Usage: cheat [keyphrase]\n" print "Usage: cheat [keyphrase]\n"
print "Available keyphrases:" print "Available keyphrases:"
max_command = max([len(sheet[0]) for sheet in cheatsheets]) + 3 max_command = max([ len(x) for x in cheatsheets.keys() ]) + 3
print '\n'.join(sorted([ '%s [%s]' % (sheet[0].ljust(max_command), sheet[1]) for sheet in cheatsheets])) print '\n'.join(sorted([ '%s [%s]' % (key.ljust(max_command), value) for key, value in cheatsheets.items() ]))
exit() exit()
sheet_found = False
# print the cheatsheet if it exists # print the cheatsheet if it exists
for sheet in cheatsheets: if keyphrase in cheatsheets:
if keyphrase == sheet[0]: with open (os.path.join(cheatsheets[keyphrase], keyphrase), 'r') as cheatsheet:
cheatsheet_filename = os.path.join(sheet[1], keyphrase) print cheatsheet.read()
with open(cheatsheet_filename, 'r') as cheatsheet:
print cheatsheet.read()
sheet_found = True
# if it does not, say so # if it does not, say so
else: else:
print 'No cheatsheet found for %s.' % keyphrase print 'No cheatsheet found for %s.' % keyphrase

BIN
cheatsheets/__init__.pyc Normal file

Binary file not shown.