From c70dc002fa995d7c4a0e134b3b47a187b07589ad Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Wed, 11 Feb 2015 21:02:45 -0500 Subject: [PATCH] Issue #180 Now, whenever a cheatsheet is to be edited, if that cheatsheet does not exist on the `DEFAULT_SHEET_PATH`, it is first copied there before being opened for editing. This prevents system-wide cheatsheets from being edited when using `cheat` as `root`. --- bin/cheat | 2 +- cheat/sheet.py | 16 +++++++++------- setup.py | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/bin/cheat b/bin/cheat index 20b634b..1dfedf4 100755 --- a/bin/cheat +++ b/bin/cheat @@ -38,7 +38,7 @@ from docopt import docopt if __name__ == '__main__': # parse the command-line options - options = docopt(__doc__, version='cheat 2.1.5') + options = docopt(__doc__, version='cheat 2.1.6') # list directories if options['--directories']: diff --git a/cheat/sheet.py b/cheat/sheet.py index 8218278..f4e380f 100644 --- a/cheat/sheet.py +++ b/cheat/sheet.py @@ -25,14 +25,10 @@ def create_or_edit(sheet): # if the cheatsheet does not exist if not exists(sheet): create(sheet) - - # if the cheatsheet exists and is writeable... - elif exists(sheet) and is_writable(sheet): - edit(sheet) - # if the cheatsheet exists but is not writable... - elif exists(sheet) and not is_writable(sheet): - # copy the cheatsheet to the home directory before editing + # if the cheatsheet exists but not in the default_path, copy it to the + # default path before editing + elif exists(sheet) and not exists_in_default_path(sheet): copy(path(sheet), os.path.join(sheets.default_path(), sheet)) edit(sheet) @@ -63,6 +59,12 @@ def exists(sheet): return sheet in sheets.get() and os.access(path(sheet), os.R_OK) +def exists_in_default_path(sheet): + """ Predicate that returns true if the sheet exists in default_path""" + default_path_sheet = os.path.join(sheets.default_path(), sheet) + return sheet in sheets.get() and os.access(default_path_sheet, os.R_OK) + + def is_writable(sheet): """ Predicate that returns true if the sheet is writeable """ return sheet in sheets.get() and os.access(path(sheet), os.W_OK) diff --git a/setup.py b/setup.py index 1e39c54..6a81c99 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ import os setup( name = 'cheat', - version = '2.1.5', + version = '2.1.6', author = 'Chris Lane', author_email = 'chris@chris-allen-lane.com', license = 'GPL3',