From ab87bb11c46051aa65f75dacf4d9dc1b97cab029 Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Thu, 31 Jan 2019 17:01:46 -0500 Subject: [PATCH] Refactored (2) Created an `Editor` class out methods in the `Util` class to enhance code clarity. --- bin/cheat | 10 ++++++---- cheat/editor.py | 28 ++++++++++++++++++++++++++++ cheat/sheet.py | 8 +++++--- cheat/utils.py | 20 -------------------- 4 files changed, 39 insertions(+), 27 deletions(-) create mode 100644 cheat/editor.py diff --git a/bin/cheat b/bin/cheat index 5e90ec4..2b518e7 100755 --- a/bin/cheat +++ b/bin/cheat @@ -36,10 +36,11 @@ Examples: # require the dependencies from __future__ import print_function -from cheat.sheets import Sheets -from cheat.sheet import Sheet -from cheat.utils import Utils from cheat.configuration import Configuration +from cheat.editor import Editor +from cheat.sheet import Sheet +from cheat.sheets import Sheets +from cheat.utils import Utils from docopt import docopt import os @@ -53,9 +54,10 @@ if __name__ == '__main__': config.validate() # bootsrap + editor = Editor(config) sheets = Sheets(config) utils = Utils(config) - sheet = Sheet(sheets, utils) + sheet = Sheet(sheets, utils, editor) # list directories if options['--directories']: diff --git a/cheat/editor.py b/cheat/editor.py new file mode 100644 index 0000000..cb35bd9 --- /dev/null +++ b/cheat/editor.py @@ -0,0 +1,28 @@ +from __future__ import print_function +import subprocess + + +class Editor: + + def __init__(self, config): + self._config = config + + def editor(self): + """ Determines the user's preferred editor """ + + # assert that the editor is set + if (not self._config.cheat_editor): + Utils.die( + 'You must set a CHEAT_EDITOR, VISUAL, or EDITOR environment ' + 'variable or setting in order to create/edit a cheatsheet.' + ) + + return self._config.cheat_editor + + def open(self, filepath): + """ Open `filepath` using the EDITOR specified by the env variables """ + editor_cmd = self.editor().split() + try: + subprocess.call(editor_cmd + [filepath]) + except OSError: + Utils.die('Could not launch ' + self.editor()) diff --git a/cheat/sheet.py b/cheat/sheet.py index 81553a1..0fffd82 100644 --- a/cheat/sheet.py +++ b/cheat/sheet.py @@ -2,14 +2,16 @@ import io import os import shutil +from cheat.editor import Editor from cheat.utils import Utils class Sheet: - def __init__(self, sheets, utils): + def __init__(self, sheets, utils, editor): self._sheets = sheets self._utils = utils + self._editor = editor def copy(self, current_sheet_path, new_sheet_path): """ Copies a sheet to a new path """ @@ -44,11 +46,11 @@ class Sheet: def create(self, sheet): """ Creates a cheatsheet """ new_sheet_path = os.path.join(self._sheets.default_path(), sheet) - self._utils.open_with_editor(new_sheet_path) + self._editor.open(new_sheet_path) def edit(self, sheet): """ Opens a cheatsheet for editing """ - self._utils.open_with_editor(self.path(sheet)) + self._editor.open(self.path(sheet)) def exists(self, sheet): """ Predicate that returns true if the sheet exists """ diff --git a/cheat/utils.py b/cheat/utils.py index f1a46a3..fad1453 100644 --- a/cheat/utils.py +++ b/cheat/utils.py @@ -68,26 +68,6 @@ class Utils: Utils.warn(message) exit(1) - def editor(self): - """ Determines the user's preferred editor """ - - # assert that the editor is set - if (not self._config.cheat_editor): - Utils.die( - 'You must set a CHEAT_EDITOR, VISUAL, or EDITOR environment ' - 'variable or setting in order to create/edit a cheatsheet.' - ) - - return self._config.cheat_editor - - def open_with_editor(self, filepath): - """ Open `filepath` using the EDITOR specified by the env variables """ - editor_cmd = self.editor().split() - try: - subprocess.call(editor_cmd + [filepath]) - except OSError: - Utils.die('Could not launch ' + self.editor()) - @staticmethod def warn(message): """ Prints a message to stderr """