Refactored (2)

Created an `Editor` class out methods in the `Util` class to enhance
code clarity.
This commit is contained in:
Chris Lane
2019-01-31 17:01:46 -05:00
parent 8f757d7735
commit ab87bb11c4
4 changed files with 39 additions and 27 deletions

28
cheat/editor.py Normal file
View File

@ -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())

View File

@ -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 """

View File

@ -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 """