From 3ad923eff050bb44d6682f0820ff21bf89c1557e Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Fri, 1 Feb 2019 15:10:03 -0500 Subject: [PATCH] Refactored (11) Renamed `CHEAT_DEFAULT_DIR` to `CHEAT_USER_DIR` because the latter more accurately describes the purpose of the variable. --- README.md | 6 +++--- bin/cheat | 24 ++++++++++++------------ cheat/configuration.py | 5 +++-- cheat/sheet.py | 8 ++++---- cheat/sheets.py | 6 +++--- 5 files changed, 25 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index a9c86e7..ec2a354 100644 --- a/README.md +++ b/README.md @@ -83,13 +83,13 @@ with your [dotfiles][]. Configuring ----------- -### Setting a CHEAT_DEFAULT_DIR ### +### Setting a CHEAT_USER_DIR ### 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: ```sh -export CHEAT_DEFAULT_DIR='/path/to/my/cheats' +export CHEAT_USER_DIR='/path/to/my/cheats' ``` ### Setting a CHEAT_PATH ### diff --git a/bin/cheat b/bin/cheat index 3791427..7ced880 100755 --- a/bin/cheat +++ b/bin/cheat @@ -53,29 +53,29 @@ if __name__ == '__main__': config = Configuration() config.validate() - # create the CHEAT_DEFAULT_DIR if it does not exist - if not os.path.isdir(config.cheat_default_dir): + # create the CHEAT_USER_DIR if it does not exist + if not os.path.isdir(config.cheat_user_dir): try: - os.mkdir(config.cheat_default_dir) + os.mkdir(config.cheat_user_dir) except OSError: Utils.die("%s %s %s" % ( - 'Could not create CHEAT_DEFAULT_DIR (', - config.cheat_default_dir, + 'Could not create CHEAT_USER_DIR (', + config.cheat_user_dir, ')') ) - # assert that the CHEAT_DEFAULT_DIR is readable and writable - if not os.access(config.cheat_default_dir, os.R_OK): + # assert that the CHEAT_USER_DIR is readable and writable + if not os.access(config.cheat_user_dir, os.R_OK): Utils.die("%s %s %s" % ( - 'The CHEAT_DEFAULT_DIR (', - config.cheat_default_dir, + 'The CHEAT_USER_DIR (', + config.cheat_user_dir, ') 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" % ( - 'The CHEAT_DEFAULT_DIR (', - config.cheat_default_dir, + 'The CHEAT_USER_DIR (', + config.cheat_user_dir, ') is not writeable') ) diff --git a/cheat/configuration.py b/cheat/configuration.py index 73a5021..7b2d3bf 100644 --- a/cheat/configuration.py +++ b/cheat/configuration.py @@ -41,8 +41,9 @@ class Configuration: True, ]) - # self.cheat_default_dir - self.cheat_default_dir = self._select([ + # self.cheat_user_dir + self.cheat_user_dir = self._select([ + os.environ.get('CHEAT_USER_DIR'), os.environ.get('CHEAT_DEFAULT_DIR'), os.environ.get('DEFAULT_CHEAT_DIR'), # TODO: XDG home? diff --git a/cheat/sheet.py b/cheat/sheet.py index dfd42ff..4cd3f96 100644 --- a/cheat/sheet.py +++ b/cheat/sheet.py @@ -19,7 +19,7 @@ class Sheet: def _exists_in_default_path(self, sheet): """ 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 os.access(default_path, os.R_OK)) @@ -32,7 +32,7 @@ class Sheet: # if the cheatsheet does not exist 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) # if the cheatsheet exists but not in the default_path, copy it to the @@ -41,11 +41,11 @@ class Sheet: try: shutil.copy( 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 - # happen if CHEAT_DEFAULT_DIR does not exist + # happen if CHEAT_USER_DIR does not exist except IOError: Utils.die('Could not copy cheatsheet for editing.') diff --git a/cheat/sheets.py b/cheat/sheets.py index 341fcfd..8458ea6 100644 --- a/cheat/sheets.py +++ b/cheat/sheets.py @@ -13,7 +13,7 @@ class Sheets: # Assembles a dictionary of cheatsheets as name => file-path self._sheets = {} sheet_paths = [ - config.cheat_default_dir + config.cheat_user_dir ] # merge the CHEAT_PATH paths into the sheet_paths @@ -23,7 +23,7 @@ class Sheets: sheet_paths.append(path) 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.') # otherwise, scan the filesystem @@ -40,7 +40,7 @@ class Sheets: def directories(self): """ Assembles a list of directories containing cheatsheets """ sheet_paths = [ - self._config.cheat_default_dir, + self._config.cheat_user_dir, ] # merge the CHEATPATH paths into the sheet_paths