Graceful failure on failed copy to DEFAULT_CHEAT_DIR

Pull request #108 added the option to automatically copy an
otherwise-uneditable cheatsheet to your DEFAULT_CHEAT_DIR upon an edit
request. This is a minor tweak that implements some graceful failing if
the DEFAULT_CHEAT_DIR does not exist.
This commit is contained in:
Chris Lane 2013-11-11 18:10:00 -05:00
parent 3234d21654
commit d8c723681a
1 changed files with 14 additions and 4 deletions

10
cheat
View File

@ -123,6 +123,7 @@ class CheatSheets(object):
exit(1)
else:
editor = os.environ['EDITOR'].split()
# if the cheatsheet already exists, open it for editing
try:
if cheat in self.sheets:
@ -144,10 +145,19 @@ class CheatSheets(object):
'again with sudo.')
exit(1)
import shutil
# attempt to copy the cheatsheet to DEFAULT_CHEAT_DIR
try:
new_sheet = os.path.join(DEFAULT_CHEAT_DIR, cheat)
shutil.copy(sheet_path, new_sheet)
subprocess.call(editor + [new_sheet])
# fail gracefully if the cheatsheet cannot be copied. This
# can happen if DEFAULT_CHEAT_DIR does not exist
except IOError:
print ('Could not copy cheatsheet for editing.')
exit(1)
# otherwise, create it
else:
import cheatsheets as cs