From ba47dc2cbcefad6c02c2983597d6635b57f0ff05 Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Fri, 1 Feb 2019 14:42:10 -0500 Subject: [PATCH] Refactored (10) - Added `ci/lint.sh`, which uses `flake8` to lint the relevant files - Made changes to appease the linter. - Bugfix in `cheat/configuration` (missing dependency) --- bin/cheat | 1 + cheat/__init__.py | 4 ---- cheat/colorize.py | 5 +++-- cheat/configuration.py | 7 +++++-- cheat/sheet.py | 18 ++++++++++-------- cheat/sheets.py | 2 +- cheat/utils.py | 2 +- ci/lint.sh | 9 +++++++++ 8 files changed, 30 insertions(+), 18 deletions(-) create mode 100755 ci/lint.sh diff --git a/bin/cheat b/bin/cheat index cceb50b..3791427 100755 --- a/bin/cheat +++ b/bin/cheat @@ -40,6 +40,7 @@ from cheat.colorize import Colorize from cheat.configuration import Configuration from cheat.sheet import Sheet from cheat.sheets import Sheets +from cheat.utils import Utils from docopt import docopt import os diff --git a/cheat/__init__.py b/cheat/__init__.py index 2560cf4..e69de29 100644 --- a/cheat/__init__.py +++ b/cheat/__init__.py @@ -1,4 +0,0 @@ -from . import sheet -from . import sheets -from . import utils -from . import configuration \ No newline at end of file diff --git a/cheat/colorize.py b/cheat/colorize.py index 89d79f8..7b18beb 100644 --- a/cheat/colorize.py +++ b/cheat/colorize.py @@ -23,14 +23,15 @@ class Colorize: return haystack # if the import succeeds, colorize the needle in haystack - return haystack.replace(needle, colored(needle, self._config.cheat_highlight)) + return haystack.replace(needle, + colored(needle, self._config.cheat_highlight)) def syntax(self, sheet_content): """ Applies syntax highlighting """ # only colorize if cheat_colors is true, and stdout is a tty if self._config.cheat_colors is False or not sys.stdout.isatty(): - return sheet_content + return sheet_content # don't attempt to colorize an empty cheatsheet if not sheet_content.strip(): diff --git a/cheat/configuration.py b/cheat/configuration.py index 75251af..73a5021 100644 --- a/cheat/configuration.py +++ b/cheat/configuration.py @@ -2,6 +2,7 @@ from cheat.utils import Utils import json import os + class Configuration: def __init__(self): @@ -20,7 +21,8 @@ class Configuration: try: config.update(self._read_config_file(config_file_path_global)) except Exception as e: - Utils.warn('Error while parsing global configuration: ' + e.message) + Utils.warn('Error while parsing global configuration: ' + + e.message) # attempt to read the local config file try: @@ -98,6 +100,7 @@ class Configuration: False ] 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 diff --git a/cheat/sheet.py b/cheat/sheet.py index 75b5d9a..dfd42ff 100644 --- a/cheat/sheet.py +++ b/cheat/sheet.py @@ -19,9 +19,9 @@ class Sheet: def _exists_in_default_path(self, sheet): """ Predicate that returns true if the sheet exists in default_path""" - default_path_sheet = os.path.join(self._config.cheat_default_dir, sheet) + default_path = os.path.join(self._config.cheat_default_dir, sheet) return (sheet in self._sheets.get() and - os.access(default_path_sheet, os.R_OK)) + os.access(default_path, os.R_OK)) def _path(self, sheet): """ Returns a sheet's filesystem path """ @@ -32,18 +32,20 @@ class Sheet: # if the cheatsheet does not exist if not self._exists(sheet): - new_sheet_path = os.path.join(self._config.cheat_default_dir, sheet) - self._editor.open(new_sheet_path) + new_path = os.path.join(self._config.cheat_default_dir, sheet) + self._editor.open(new_path) # if the cheatsheet exists but not in the default_path, copy it to the # default path before editing elif self._exists(sheet) and not self._exists_in_default_path(sheet): try: - shutil.copy(self._path(sheet), - os.path.join(self._config.cheat_default_dir, sheet)) + shutil.copy( + self._path(sheet), + os.path.join(self._config.cheat_default_dir, sheet) + ) - # fail gracefully if the cheatsheet cannot be copied. This can happen - # if CHEAT_DEFAULT_DIR does not exist + # fail gracefully if the cheatsheet cannot be copied. This can + # happen if CHEAT_DEFAULT_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 d83d832..341fcfd 100644 --- a/cheat/sheets.py +++ b/cheat/sheets.py @@ -8,7 +8,7 @@ class Sheets: def __init__(self, config): self._config = config - self._colorize = Colorize(config); + self._colorize = Colorize(config) # Assembles a dictionary of cheatsheets as name => file-path self._sheets = {} diff --git a/cheat/utils.py b/cheat/utils.py index 75f3802..c481a7c 100644 --- a/cheat/utils.py +++ b/cheat/utils.py @@ -17,7 +17,7 @@ class Utils: @staticmethod def boolify(value): - """ Type-converts 'true' and 'false' (strings) to Boolean equivalents """ + """ Type-converts 'true' and 'false' to Booleans """ # if `value` is not a string, return it as-is if not isinstance(value, str): return value diff --git a/ci/lint.sh b/ci/lint.sh new file mode 100755 index 0000000..e6a3d23 --- /dev/null +++ b/ci/lint.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +# Resolve the app root +SCRIPT=`realpath $0` +SCRIPTPATH=`dirname $SCRIPT` +APPROOT=`realpath "$SCRIPTPATH/.."` + +flake8 $APPROOT/bin/cheat +flake8 $APPROOT/cheat/*.py