Refactored (9)

Moved some functionality into the `Util` class.
This commit is contained in:
Chris Lane 2019-02-01 11:43:11 -05:00
parent 5793c1845a
commit a657699a24
2 changed files with 17 additions and 15 deletions

View File

@ -33,9 +33,9 @@ class Configuration:
# self.cheat_colors # self.cheat_colors
self.cheat_colors = self._select([ self.cheat_colors = self._select([
self._boolify(os.environ.get('CHEAT_COLORS')), Utils.boolify(os.environ.get('CHEAT_COLORS')),
self._boolify(os.environ.get('CHEATCOLORS')), Utils.boolify(os.environ.get('CHEATCOLORS')),
self._boolify(config.get('CHEAT_COLORS')), Utils.boolify(config.get('CHEAT_COLORS')),
True, True,
]) ])
@ -64,8 +64,8 @@ class Configuration:
config.get('CHEAT_HIGHLIGHT'), config.get('CHEAT_HIGHLIGHT'),
False, False,
]) ])
if (self.cheat_highlight.strip().lower() == "false"): if isinstance(self.cheat_highlight, str):
self.cheat_highlight = False Utils.boolify(self.cheat_highlight)
# self.cheat_path # self.cheat_path
self.cheat_path = self._select([ self.cheat_path = self._select([
@ -75,18 +75,10 @@ class Configuration:
'/usr/share/cheat', '/usr/share/cheat',
]) ])
def _boolify(self, value):
# if `value` is not a string, return it as-is
if not isinstance(value, str):
return value
# otherwise, convert "true" and "false" to Boolean counterparts
return value.strip().lower() == "true"
def _read_config_file(self, path): def _read_config_file(self, path):
# Reads configuration file and returns list of set variables # Reads configuration file and returns list of set variables
config = {} config = {}
if (os.path.isfile(path)): if os.path.isfile(path):
with open(path) as config_file: with open(path) as config_file:
config.update(json.load(config_file)) config.update(json.load(config_file))
return config return config
@ -105,7 +97,7 @@ class Configuration:
'blue', 'magenta', 'cyan', 'white', 'blue', 'magenta', 'cyan', 'white',
False False
] ]
if (self.cheat_highlight not in highlights): if self.cheat_highlight not in highlights:
Utils.die("%s %s" % ('CHEAT_HIGHLIGHT must be one of:', highlights)) Utils.die("%s %s" % ('CHEAT_HIGHLIGHT must be one of:', highlights))
return True return True

View File

@ -14,3 +14,13 @@ class Utils:
def warn(message): def warn(message):
""" Prints a message to stderr """ """ Prints a message to stderr """
print((message), file=sys.stderr) print((message), file=sys.stderr)
@staticmethod
def boolify(value):
""" Type-converts 'true' and 'false' (strings) to Boolean equivalents """
# if `value` is not a string, return it as-is
if not isinstance(value, str):
return value
# otherwise, convert "true" and "false" to Boolean counterparts
return value.strip().lower() == "true"