diff --git a/bin/cheat b/bin/cheat index 45524ac..cceb50b 100755 --- a/bin/cheat +++ b/bin/cheat @@ -93,7 +93,7 @@ if __name__ == '__main__': # create/edit cheatsheet elif options['--edit']: - sheet.create_or_edit(options['']) + sheet.edit(options['']) # search among the cheatsheets elif options['--search']: diff --git a/cheat/sheet.py b/cheat/sheet.py index e33df5a..75b5d9a 100644 --- a/cheat/sheet.py +++ b/cheat/sheet.py @@ -12,69 +12,51 @@ class Sheet: self._editor = Editor(config) self._sheets = sheets - def copy(self, current_sheet_path, new_sheet_path): - """ Copies a sheet to a new path """ - - # attempt to copy the sheet to CHEAT_DEFAULT_DIR - try: - shutil.copy(current_sheet_path, new_sheet_path) - - # fail gracefully if the cheatsheet cannot be copied. This can happen - # if CHEAT_DEFAULT_DIR does not exist - except IOError: - Utils.die('Could not copy cheatsheet for editing.') - - def create_or_edit(self, sheet): - """ Creates or edits a cheatsheet """ - - # if the cheatsheet does not exist - if not self.exists(sheet): - self.create(sheet) - - # if the cheatsheet exists but not in the default_path, copy it to the - # default path before editing - elif self.exists(sheet) and not self.exists_in_default_path(sheet): - self.copy(self.path(sheet), - os.path.join(self._config.cheat_default_dir, sheet)) - self.edit(sheet) - - # if it exists and is in the default path, then just open it - else: - self.edit(sheet) - - def create(self, sheet): - """ Creates a cheatsheet """ - new_sheet_path = os.path.join(self._config.cheat_default_dir, sheet) - self._editor.open(new_sheet_path) - - def edit(self, sheet): - """ Opens a cheatsheet for editing """ - self._editor.open(self.path(sheet)) - - def exists(self, sheet): + def _exists(self, sheet): """ Predicate that returns true if the sheet exists """ return (sheet in self._sheets.get() and - os.access(self.path(sheet), os.R_OK)) + os.access(self._path(sheet), os.R_OK)) - def exists_in_default_path(self, sheet): + def _exists_in_default_path(self, sheet): """ Predicate that returns true if the sheet exists in default_path""" default_path_sheet = os.path.join(self._config.cheat_default_dir, sheet) return (sheet in self._sheets.get() and os.access(default_path_sheet, os.R_OK)) - def is_writable(self, sheet): - """ Predicate that returns true if the sheet is writeable """ - return (sheet in self._sheets.get() and - os.access(self.path(sheet), os.W_OK)) - - def path(self, sheet): + def _path(self, sheet): """ Returns a sheet's filesystem path """ return self._sheets.get()[sheet] + def edit(self, sheet): + """ Creates or edits a cheatsheet """ + + # if the cheatsheet does not exist + if not self._exists(sheet): + new_sheet_path = os.path.join(self._config.cheat_default_dir, sheet) + self._editor.open(new_sheet_path) + + # if the cheatsheet exists but not in the default_path, copy it to the + # default path before editing + elif self._exists(sheet) and not self._exists_in_default_path(sheet): + try: + shutil.copy(self._path(sheet), + os.path.join(self._config.cheat_default_dir, sheet)) + + # fail gracefully if the cheatsheet cannot be copied. This can happen + # if CHEAT_DEFAULT_DIR does not exist + except IOError: + Utils.die('Could not copy cheatsheet for editing.') + + self._editor.open(self._path(sheet)) + + # if it exists and is in the default path, then just open it + else: + self._editor.open(self._path(sheet)) + def read(self, sheet): """ Returns the contents of the cheatsheet as a String """ - if not self.exists(sheet): + if not self._exists(sheet): Utils.die('No cheatsheet found for ' + sheet) - with io.open(self.path(sheet), encoding='utf-8') as cheatfile: + with io.open(self._path(sheet), encoding='utf-8') as cheatfile: return cheatfile.read()