Refactored (11)

Renamed `CHEAT_DEFAULT_DIR` to `CHEAT_USER_DIR` because the latter more
accurately describes the purpose of the variable.
This commit is contained in:
Chris Lane 2019-02-01 15:10:03 -05:00
parent ba47dc2cbc
commit 3ad923eff0
5 changed files with 25 additions and 24 deletions

View File

@ -83,13 +83,13 @@ with your [dotfiles][].
Configuring Configuring
----------- -----------
### Setting a CHEAT_DEFAULT_DIR ### ### Setting a CHEAT_USER_DIR ###
Personal cheatsheets are saved in the `~/.cheat` directory by default, but you Personal cheatsheets are saved in the `~/.cheat` directory by default, but you
can specify a different default by exporting a `CHEAT_DEFAULT_DIR` environment can specify a different default by exporting a `CHEAT_USER_DIR` environment
variable: variable:
```sh ```sh
export CHEAT_DEFAULT_DIR='/path/to/my/cheats' export CHEAT_USER_DIR='/path/to/my/cheats'
``` ```
### Setting a CHEAT_PATH ### ### Setting a CHEAT_PATH ###

View File

@ -53,29 +53,29 @@ if __name__ == '__main__':
config = Configuration() config = Configuration()
config.validate() config.validate()
# create the CHEAT_DEFAULT_DIR if it does not exist # create the CHEAT_USER_DIR if it does not exist
if not os.path.isdir(config.cheat_default_dir): if not os.path.isdir(config.cheat_user_dir):
try: try:
os.mkdir(config.cheat_default_dir) os.mkdir(config.cheat_user_dir)
except OSError: except OSError:
Utils.die("%s %s %s" % ( Utils.die("%s %s %s" % (
'Could not create CHEAT_DEFAULT_DIR (', 'Could not create CHEAT_USER_DIR (',
config.cheat_default_dir, config.cheat_user_dir,
')') ')')
) )
# assert that the CHEAT_DEFAULT_DIR is readable and writable # assert that the CHEAT_USER_DIR is readable and writable
if not os.access(config.cheat_default_dir, os.R_OK): if not os.access(config.cheat_user_dir, os.R_OK):
Utils.die("%s %s %s" % ( Utils.die("%s %s %s" % (
'The CHEAT_DEFAULT_DIR (', 'The CHEAT_USER_DIR (',
config.cheat_default_dir, config.cheat_user_dir,
') is not readable') ') is not readable')
) )
if not os.access(config.cheat_default_dir, os.W_OK): if not os.access(config.cheat_user_dir, os.W_OK):
Utils.die("%s %s %s" % ( Utils.die("%s %s %s" % (
'The CHEAT_DEFAULT_DIR (', 'The CHEAT_USER_DIR (',
config.cheat_default_dir, config.cheat_user_dir,
') is not writeable') ') is not writeable')
) )

View File

@ -41,8 +41,9 @@ class Configuration:
True, True,
]) ])
# self.cheat_default_dir # self.cheat_user_dir
self.cheat_default_dir = self._select([ self.cheat_user_dir = self._select([
os.environ.get('CHEAT_USER_DIR'),
os.environ.get('CHEAT_DEFAULT_DIR'), os.environ.get('CHEAT_DEFAULT_DIR'),
os.environ.get('DEFAULT_CHEAT_DIR'), os.environ.get('DEFAULT_CHEAT_DIR'),
# TODO: XDG home? # TODO: XDG home?

View File

@ -19,7 +19,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 = os.path.join(self._config.cheat_default_dir, sheet) default_path = os.path.join(self._config.cheat_user_dir, sheet)
return (sheet in self._sheets.get() and return (sheet in self._sheets.get() and
os.access(default_path, os.R_OK)) os.access(default_path, os.R_OK))
@ -32,7 +32,7 @@ class Sheet:
# if the cheatsheet does not exist # if the cheatsheet does not exist
if not self._exists(sheet): if not self._exists(sheet):
new_path = os.path.join(self._config.cheat_default_dir, sheet) new_path = os.path.join(self._config.cheat_user_dir, sheet)
self._editor.open(new_path) self._editor.open(new_path)
# if the cheatsheet exists but not in the default_path, copy it to the # if the cheatsheet exists but not in the default_path, copy it to the
@ -41,11 +41,11 @@ class Sheet:
try: try:
shutil.copy( shutil.copy(
self._path(sheet), self._path(sheet),
os.path.join(self._config.cheat_default_dir, sheet) os.path.join(self._config.cheat_user_dir, sheet)
) )
# fail gracefully if the cheatsheet cannot be copied. This can # fail gracefully if the cheatsheet cannot be copied. This can
# happen if CHEAT_DEFAULT_DIR does not exist # happen if CHEAT_USER_DIR does not exist
except IOError: except IOError:
Utils.die('Could not copy cheatsheet for editing.') Utils.die('Could not copy cheatsheet for editing.')

View File

@ -13,7 +13,7 @@ class Sheets:
# Assembles a dictionary of cheatsheets as name => file-path # Assembles a dictionary of cheatsheets as name => file-path
self._sheets = {} self._sheets = {}
sheet_paths = [ sheet_paths = [
config.cheat_default_dir config.cheat_user_dir
] ]
# merge the CHEAT_PATH paths into the sheet_paths # merge the CHEAT_PATH paths into the sheet_paths
@ -23,7 +23,7 @@ class Sheets:
sheet_paths.append(path) sheet_paths.append(path)
if not sheet_paths: if not sheet_paths:
Utils.die('The CHEAT_DEFAULT_DIR dir does not exist ' Utils.die('The CHEAT_USER_DIR dir does not exist '
+ 'or the CHEAT_PATH is not set.') + 'or the CHEAT_PATH is not set.')
# otherwise, scan the filesystem # otherwise, scan the filesystem
@ -40,7 +40,7 @@ class Sheets:
def directories(self): def directories(self):
""" Assembles a list of directories containing cheatsheets """ """ Assembles a list of directories containing cheatsheets """
sheet_paths = [ sheet_paths = [
self._config.cheat_default_dir, self._config.cheat_user_dir,
] ]
# merge the CHEATPATH paths into the sheet_paths # merge the CHEATPATH paths into the sheet_paths