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
-----------
### 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 ###

View File

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

View File

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

View File

@ -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.')

View File

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