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

18
cheat
View File

@ -123,6 +123,7 @@ class CheatSheets(object):
exit(1) exit(1)
else: else:
editor = os.environ['EDITOR'].split() editor = os.environ['EDITOR'].split()
# if the cheatsheet already exists, open it for editing # if the cheatsheet already exists, open it for editing
try: try:
if cheat in self.sheets: if cheat in self.sheets:
@ -134,7 +135,7 @@ class CheatSheets(object):
% (cheat, sheet_path)) % (cheat, sheet_path))
print ('Do you want to ' print ('Do you want to '
'copy it to your user cheatsheets directory [%s] ' 'copy it to your user cheatsheets directory [%s] '
'before editing ?\nKeep in mind that your sheet ' 'before editing?\nKeep in mind that your sheet '
'will always be used before system-wide one.' 'will always be used before system-wide one.'
% DEFAULT_CHEAT_DIR) % DEFAULT_CHEAT_DIR)
awn = raw_input('[y/n] ') awn = raw_input('[y/n] ')
@ -144,9 +145,18 @@ class CheatSheets(object):
'again with sudo.') 'again with sudo.')
exit(1) exit(1)
import shutil import shutil
new_sheet = os.path.join(DEFAULT_CHEAT_DIR, cheat)
shutil.copy(sheet_path, new_sheet) # attempt to copy the cheatsheet to DEFAULT_CHEAT_DIR
subprocess.call(editor + [new_sheet]) 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 # otherwise, create it
else: else: