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

View File

@ -36,10 +36,11 @@ Examples:
# require the dependencies # require the dependencies
from __future__ import print_function 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.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 from docopt import docopt
import os import os
@ -53,9 +54,10 @@ if __name__ == '__main__':
config.validate() config.validate()
# bootsrap # bootsrap
editor = Editor(config)
sheets = Sheets(config) sheets = Sheets(config)
utils = Utils(config) utils = Utils(config)
sheet = Sheet(sheets, utils) sheet = Sheet(sheets, utils, editor)
# list directories # list directories
if options['--directories']: if options['--directories']:

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 os
import shutil import shutil
from cheat.editor import Editor
from cheat.utils import Utils from cheat.utils import Utils
class Sheet: class Sheet:
def __init__(self, sheets, utils): def __init__(self, sheets, utils, editor):
self._sheets = sheets self._sheets = sheets
self._utils = utils self._utils = utils
self._editor = editor
def copy(self, current_sheet_path, new_sheet_path): def copy(self, current_sheet_path, new_sheet_path):
""" Copies a sheet to a new path """ """ Copies a sheet to a new path """
@ -44,11 +46,11 @@ class Sheet:
def create(self, sheet): def create(self, sheet):
""" Creates a cheatsheet """ """ Creates a cheatsheet """
new_sheet_path = os.path.join(self._sheets.default_path(), sheet) 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): def edit(self, sheet):
""" Opens a cheatsheet for editing """ """ Opens a cheatsheet for editing """
self._utils.open_with_editor(self.path(sheet)) self._editor.open(self.path(sheet))
def exists(self, sheet): def exists(self, sheet):
""" Predicate that returns true if the sheet exists """ """ Predicate that returns true if the sheet exists """

View File

@ -68,26 +68,6 @@ class Utils:
Utils.warn(message) Utils.warn(message)
exit(1) 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 @staticmethod
def warn(message): def warn(message):
""" Prints a message to stderr """ """ Prints a message to stderr """