mirror of https://github.com/cheat/cheat.git
Refactored (7)
Refactored for general code-clarity, with particular focus on removing needless abstraction within `Sheet` and `Sheets` classes.
This commit is contained in:
parent
d61e4e7c34
commit
e2b5728283
29
bin/cheat
29
bin/cheat
|
@ -41,6 +41,7 @@ from cheat.configuration import Configuration
|
||||||
from cheat.sheet import Sheet
|
from cheat.sheet import Sheet
|
||||||
from cheat.sheets import Sheets
|
from cheat.sheets import Sheets
|
||||||
from docopt import docopt
|
from docopt import docopt
|
||||||
|
import os
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
@ -51,6 +52,32 @@ if __name__ == '__main__':
|
||||||
config = Configuration()
|
config = Configuration()
|
||||||
config.validate()
|
config.validate()
|
||||||
|
|
||||||
|
# create the CHEAT_DEFAULT_DIR if it does not exist
|
||||||
|
if not os.path.isdir(config.cheat_default_dir):
|
||||||
|
try:
|
||||||
|
os.mkdir(config.cheat_default_dir)
|
||||||
|
|
||||||
|
except OSError:
|
||||||
|
Utils.die("%s %s %s" % (
|
||||||
|
'Could not create CHEAT_DEFAULT_DIR (',
|
||||||
|
config.cheat_default_dir,
|
||||||
|
')')
|
||||||
|
)
|
||||||
|
|
||||||
|
# assert that the CHEAT_DEFAULT_DIR is readable and writable
|
||||||
|
if not os.access(config.cheat_default_dir, os.R_OK):
|
||||||
|
Utils.die("%s %s %s" % (
|
||||||
|
'The CHEAT_DEFAULT_DIR (',
|
||||||
|
config.cheat_default_dir,
|
||||||
|
') is not readable')
|
||||||
|
)
|
||||||
|
if not os.access(config.cheat_default_dir, os.W_OK):
|
||||||
|
Utils.die("%s %s %s" % (
|
||||||
|
'The CHEAT_DEFAULT_DIR (',
|
||||||
|
config.cheat_default_dir,
|
||||||
|
') is not writeable')
|
||||||
|
)
|
||||||
|
|
||||||
# bootsrap
|
# bootsrap
|
||||||
sheets = Sheets(config)
|
sheets = Sheets(config)
|
||||||
sheet = Sheet(config, sheets)
|
sheet = Sheet(config, sheets)
|
||||||
|
@ -58,7 +85,7 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
# list directories
|
# list directories
|
||||||
if options['--directories']:
|
if options['--directories']:
|
||||||
print("\n".join(sheets.paths()))
|
print("\n".join(sheets.directories()))
|
||||||
|
|
||||||
# list cheatsheets
|
# list cheatsheets
|
||||||
elif options['--list']:
|
elif options['--list']:
|
||||||
|
|
|
@ -6,10 +6,14 @@ class Configuration:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# compute the location of the config files
|
# compute the location of the config files
|
||||||
config_file_path_global = os.environ.get('CHEAT_GLOBAL_CONF_PATH') \
|
config_file_path_global = self._select([
|
||||||
or '/etc/cheat'
|
os.environ.get('CHEAT_GLOBAL_CONF_PATH'),
|
||||||
config_file_path_local = (os.environ.get('CHEAT_LOCAL_CONF_PATH') \
|
'/etc/cheat',
|
||||||
or os.path.expanduser('~/.config/cheat/cheat'))
|
])
|
||||||
|
config_file_path_local = self._select([
|
||||||
|
os.environ.get('CHEAT_LOCAL_CONF_PATH'),
|
||||||
|
os.path.expanduser('~/.config/cheat/cheat'),
|
||||||
|
])
|
||||||
|
|
||||||
# attempt to read the global config file
|
# attempt to read the global config file
|
||||||
config = {}
|
config = {}
|
||||||
|
@ -39,7 +43,10 @@ class Configuration:
|
||||||
self.cheat_default_dir = self._select([
|
self.cheat_default_dir = self._select([
|
||||||
os.environ.get('CHEAT_DEFAULT_DIR'),
|
os.environ.get('CHEAT_DEFAULT_DIR'),
|
||||||
os.environ.get('DEFAULT_CHEAT_DIR'),
|
os.environ.get('DEFAULT_CHEAT_DIR'),
|
||||||
'~/.cheat',
|
# TODO: XDG home?
|
||||||
|
os.path.expanduser(
|
||||||
|
os.path.expandvars(os.path.join('~', '.cheat'))
|
||||||
|
),
|
||||||
])
|
])
|
||||||
|
|
||||||
# self.cheat_editor
|
# self.cheat_editor
|
||||||
|
|
|
@ -8,8 +8,9 @@ import shutil
|
||||||
class Sheet:
|
class Sheet:
|
||||||
|
|
||||||
def __init__(self, config, sheets):
|
def __init__(self, config, sheets):
|
||||||
self._sheets = sheets
|
self._config = config
|
||||||
self._editor = Editor(config)
|
self._editor = Editor(config)
|
||||||
|
self._sheets = sheets
|
||||||
|
|
||||||
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 """
|
||||||
|
@ -34,7 +35,7 @@ class Sheet:
|
||||||
# default path before editing
|
# default path before editing
|
||||||
elif self.exists(sheet) and not self.exists_in_default_path(sheet):
|
elif self.exists(sheet) and not self.exists_in_default_path(sheet):
|
||||||
self.copy(self.path(sheet),
|
self.copy(self.path(sheet),
|
||||||
os.path.join(self._sheets.default_path(), sheet))
|
os.path.join(self._config.cheat_default_dir, sheet))
|
||||||
self.edit(sheet)
|
self.edit(sheet)
|
||||||
|
|
||||||
# if it exists and is in the default path, then just open it
|
# if it exists and is in the default path, then just open it
|
||||||
|
@ -43,7 +44,7 @@ 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._config.cheat_default_dir, sheet)
|
||||||
self._editor.open(new_sheet_path)
|
self._editor.open(new_sheet_path)
|
||||||
|
|
||||||
def edit(self, sheet):
|
def edit(self, sheet):
|
||||||
|
@ -57,7 +58,7 @@ class Sheet:
|
||||||
|
|
||||||
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"""
|
""" Predicate that returns true if the sheet exists in default_path"""
|
||||||
default_path_sheet = os.path.join(self._sheets.default_path(), sheet)
|
default_path_sheet = os.path.join(self._config.cheat_default_dir, sheet)
|
||||||
return (sheet in self._sheets.get() and
|
return (sheet in self._sheets.get() and
|
||||||
os.access(default_path_sheet, os.R_OK))
|
os.access(default_path_sheet, os.R_OK))
|
||||||
|
|
||||||
|
|
|
@ -10,47 +10,25 @@ class Sheets:
|
||||||
self._config = config
|
self._config = config
|
||||||
self._colorize = Colorize(config);
|
self._colorize = Colorize(config);
|
||||||
|
|
||||||
def default_path(self):
|
# Assembles a dictionary of cheatsheets as name => file-path
|
||||||
""" Returns the default cheatsheet path """
|
self._sheets = {}
|
||||||
|
sheet_paths = [
|
||||||
|
config.cheat_default_dir
|
||||||
|
]
|
||||||
|
|
||||||
# determine the default cheatsheet dir
|
# merge the CHEAT_PATH paths into the sheet_paths
|
||||||
# TODO: should probably rename `CHEAT_DEFAULT_DIR` to
|
if config.cheat_path:
|
||||||
# `CHEAT_USER_DIR` or something for clarity.
|
for path in config.cheat_path.split(os.pathsep):
|
||||||
default_sheets_dir = (self._config.cheat_default_dir or
|
if os.path.isdir(path):
|
||||||
os.path.join('~', '.cheat'))
|
sheet_paths.append(path)
|
||||||
default_sheets_dir = os.path.expanduser(
|
|
||||||
os.path.expandvars(default_sheets_dir))
|
|
||||||
|
|
||||||
# create the CHEAT_DEFAULT_DIR if it does not exist
|
if not sheet_paths:
|
||||||
if not os.path.isdir(default_sheets_dir):
|
Utils.die('The CHEAT_DEFAULT_DIR dir does not exist '
|
||||||
try:
|
+ 'or the CHEAT_PATH is not set.')
|
||||||
# @kludge: unclear on why this is necessary
|
|
||||||
os.umask(0000)
|
|
||||||
os.mkdir(default_sheets_dir)
|
|
||||||
|
|
||||||
except OSError:
|
|
||||||
Utils.die('Could not create CHEAT_DEFAULT_DIR')
|
|
||||||
|
|
||||||
# assert that the CHEAT_DEFAULT_DIR is readable and writable
|
|
||||||
if not os.access(default_sheets_dir, os.R_OK):
|
|
||||||
Utils.die('The CHEAT_DEFAULT_DIR ('
|
|
||||||
+ default_sheets_dir
|
|
||||||
+ ') is not readable.')
|
|
||||||
if not os.access(default_sheets_dir, os.W_OK):
|
|
||||||
Utils.die('The CHEAT_DEFAULT_DIR ('
|
|
||||||
+ default_sheets_dir
|
|
||||||
+ ') is not writable.')
|
|
||||||
|
|
||||||
# return the default dir
|
|
||||||
return default_sheets_dir
|
|
||||||
|
|
||||||
def get(self):
|
|
||||||
""" Assembles a dictionary of cheatsheets as name => file-path """
|
|
||||||
cheats = {}
|
|
||||||
|
|
||||||
# otherwise, scan the filesystem
|
# otherwise, scan the filesystem
|
||||||
for cheat_dir in reversed(self.paths()):
|
for cheat_dir in reversed(sheet_paths):
|
||||||
cheats.update(
|
self._sheets.update(
|
||||||
dict([
|
dict([
|
||||||
(cheat, os.path.join(cheat_dir, cheat))
|
(cheat, os.path.join(cheat_dir, cheat))
|
||||||
for cheat in os.listdir(cheat_dir)
|
for cheat in os.listdir(cheat_dir)
|
||||||
|
@ -59,26 +37,22 @@ class Sheets:
|
||||||
])
|
])
|
||||||
)
|
)
|
||||||
|
|
||||||
return cheats
|
def directories(self):
|
||||||
|
|
||||||
def paths(self):
|
|
||||||
""" Assembles a list of directories containing cheatsheets """
|
""" Assembles a list of directories containing cheatsheets """
|
||||||
sheet_paths = [
|
sheet_paths = [
|
||||||
self.default_path(),
|
self._config.cheat_default_dir,
|
||||||
]
|
]
|
||||||
|
|
||||||
# merge the CHEAT_PATH paths into the sheet_paths
|
# merge the CHEATPATH paths into the sheet_paths
|
||||||
if self._config.cheat_path:
|
|
||||||
for path in self._config.cheat_path.split(os.pathsep):
|
for path in self._config.cheat_path.split(os.pathsep):
|
||||||
if os.path.isdir(path):
|
|
||||||
sheet_paths.append(path)
|
sheet_paths.append(path)
|
||||||
|
|
||||||
if not sheet_paths:
|
|
||||||
Utils.die('The CHEAT_DEFAULT_DIR dir does not exist '
|
|
||||||
+ 'or the CHEAT_PATH is not set.')
|
|
||||||
|
|
||||||
return sheet_paths
|
return sheet_paths
|
||||||
|
|
||||||
|
def get(self):
|
||||||
|
""" Returns a dictionary of cheatsheets as name => file-path """
|
||||||
|
return self._sheets
|
||||||
|
|
||||||
def list(self):
|
def list(self):
|
||||||
""" Lists the available cheatsheets """
|
""" Lists the available cheatsheets """
|
||||||
sheet_list = ''
|
sheet_list = ''
|
||||||
|
|
Loading…
Reference in New Issue