From 63c391f67aa9cf93c7627c9d817142419444e9de Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Mon, 26 Aug 2013 20:12:59 -0400 Subject: [PATCH] When creating a new cheatsheet using the -c option, the program now first attempts to write to the ~/.cheat directory, or alternatively create it if it does not already exist. Failing that, it attempts to write to the python package directory. --- cheat | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/cheat b/cheat index 0772de8..b8932d7 100755 --- a/cheat +++ b/cheat @@ -67,8 +67,19 @@ def create_cheatsheet(cheat, cheatsheets): exit() import cheatsheets as cs - subprocess.call([os.environ['EDITOR'], - os.path.join(cs.cheat_dir, cheat)]) + + # Attempt to write the new cheatsheet to the user's ~/.cheat dir if it + # exists. If it does not exist, attempt to create it. + if os.access(DEFAULT_CHEAT_DIR, os.W_OK) or os.makedirs(DEFAULT_CHEAT_DIR): + subprocess.call([os.environ['EDITOR'], + os.path.join(DEFAULT_CHEAT_DIR, cheat)]) + + # If the directory cannot be created, write to the python package + # directory, though that will likely require the use of sudo + else: + subprocess.call([os.environ['EDITOR'], + os.path.join(cs.cheat_dir, cheat)]) + exit() def main():