From a657699a24c209e3a1581f1ee544aa2f35ac1f3c Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Fri, 1 Feb 2019 11:43:11 -0500 Subject: [PATCH] Refactored (9) Moved some functionality into the `Util` class. --- cheat/configuration.py | 22 +++++++--------------- cheat/utils.py | 10 ++++++++++ 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/cheat/configuration.py b/cheat/configuration.py index 3d8a8e5..75251af 100644 --- a/cheat/configuration.py +++ b/cheat/configuration.py @@ -33,9 +33,9 @@ class Configuration: # self.cheat_colors self.cheat_colors = self._select([ - self._boolify(os.environ.get('CHEAT_COLORS')), - self._boolify(os.environ.get('CHEATCOLORS')), - self._boolify(config.get('CHEAT_COLORS')), + Utils.boolify(os.environ.get('CHEAT_COLORS')), + Utils.boolify(os.environ.get('CHEATCOLORS')), + Utils.boolify(config.get('CHEAT_COLORS')), True, ]) @@ -64,8 +64,8 @@ class Configuration: config.get('CHEAT_HIGHLIGHT'), False, ]) - if (self.cheat_highlight.strip().lower() == "false"): - self.cheat_highlight = False + if isinstance(self.cheat_highlight, str): + Utils.boolify(self.cheat_highlight) # self.cheat_path self.cheat_path = self._select([ @@ -75,18 +75,10 @@ class Configuration: '/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): # Reads configuration file and returns list of set variables config = {} - if (os.path.isfile(path)): + if os.path.isfile(path): with open(path) as config_file: config.update(json.load(config_file)) return config @@ -105,7 +97,7 @@ class Configuration: 'blue', 'magenta', 'cyan', 'white', 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)) return True diff --git a/cheat/utils.py b/cheat/utils.py index 01bd8a3..75f3802 100644 --- a/cheat/utils.py +++ b/cheat/utils.py @@ -14,3 +14,13 @@ class Utils: def warn(message): """ Prints a message to 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"