Don't require the package to be available.

This commit is contained in:
Louis Taylor 2013-08-16 03:02:33 +01:00
parent 389d6d5671
commit 63b0ffba14

18
cheat
View file

@ -1,20 +1,28 @@
#!/usr/bin/env python #!/usr/bin/env python
import os import os
import sys import sys
from cheatsheets import cheat_dir
# assemble a keyphrase out of all params passed to the script try:
keyphrase = ' '.join(sys.argv[1:]) # check to see if the cheat package is available
import cheatsheets
cheat_dir = cheatsheets.cheat_dir
cheatsheets = [(f, cheat_dir) for f in os.listdir(cheat_dir) if '.' not in f]
except ImportError:
cheatsheets = []
# construct the path to the cheat directory
user_cheat_dir = os.path.join(os.path.expanduser('~'), '.cheat') user_cheat_dir = os.path.join(os.path.expanduser('~'), '.cheat')
# list the files in the cheat directory # list the files in the cheat directory
cheatsheets = [(f, cheat_dir) for f in os.listdir(cheat_dir) if '.' not in f]
# add the user's cheat files if they have a ~/.cheat directory # add the user's cheat files if they have a ~/.cheat directory
if os.path.isdir(user_cheat_dir): if os.path.isdir(user_cheat_dir):
cheatsheets += [(f, user_cheat_dir) for f in os.listdir(user_cheat_dir)] cheatsheets += [(f, user_cheat_dir) for f in os.listdir(user_cheat_dir)
if '.' not in f]
cheatsheets.sort() cheatsheets.sort()
# assemble a keyphrase out of all params passed to the script
keyphrase = ' '.join(sys.argv[1:])
# print help if requested # print help if requested
if keyphrase in ['', 'help', '--help', '-h']: if keyphrase in ['', 'help', '--help', '-h']:
print "Usage: cheat [keyphrase]\n" print "Usage: cheat [keyphrase]\n"