From d8c723681a990046fe8510f9bf8eaabdc3368b44 Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Mon, 11 Nov 2013 18:10:00 -0500 Subject: [PATCH] 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. --- cheat | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/cheat b/cheat index 58be41b..b4d00da 100755 --- a/cheat +++ b/cheat @@ -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: @@ -134,7 +135,7 @@ class CheatSheets(object): % (cheat, sheet_path)) print ('Do you want to ' '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.' % DEFAULT_CHEAT_DIR) awn = raw_input('[y/n] ') @@ -144,9 +145,18 @@ class CheatSheets(object): 'again with sudo.') exit(1) import shutil - new_sheet = os.path.join(DEFAULT_CHEAT_DIR, cheat) - shutil.copy(sheet_path, new_sheet) - subprocess.call(editor + [new_sheet]) + + # 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: