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)
This commit is contained in:
Chris Lane 2019-02-01 14:42:10 -05:00
parent df21731c02
commit ba47dc2cbc
8 changed files with 30 additions and 18 deletions

View File

@ -40,6 +40,7 @@ from cheat.colorize import Colorize
from cheat.configuration import Configuration from cheat.configuration import Configuration
from cheat.sheet import Sheet from cheat.sheet import Sheet
from cheat.sheets import Sheets from cheat.sheets import Sheets
from cheat.utils import Utils
from docopt import docopt from docopt import docopt
import os import os

View File

@ -1,4 +0,0 @@
from . import sheet
from . import sheets
from . import utils
from . import configuration

View File

@ -23,7 +23,8 @@ class Colorize:
return haystack return haystack
# if the import succeeds, colorize the needle in 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): def syntax(self, sheet_content):
""" Applies syntax highlighting """ """ Applies syntax highlighting """

View File

@ -2,6 +2,7 @@ from cheat.utils import Utils
import json import json
import os import os
class Configuration: class Configuration:
def __init__(self): def __init__(self):
@ -20,7 +21,8 @@ class Configuration:
try: try:
config.update(self._read_config_file(config_file_path_global)) config.update(self._read_config_file(config_file_path_global))
except Exception as e: 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 # attempt to read the local config file
try: try:
@ -98,6 +100,7 @@ class Configuration:
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

@ -19,9 +19,9 @@ class Sheet:
def _exists_in_default_path(self, sheet): def _exists_in_default_path(self, sheet):
""" Predicate that returns true if the sheet exists in default_path""" """ 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 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): def _path(self, sheet):
""" Returns a sheet's filesystem path """ """ Returns a sheet's filesystem path """
@ -32,18 +32,20 @@ class Sheet:
# if the cheatsheet does not exist # if the cheatsheet does not exist
if not self._exists(sheet): if not self._exists(sheet):
new_sheet_path = os.path.join(self._config.cheat_default_dir, sheet) new_path = os.path.join(self._config.cheat_default_dir, sheet)
self._editor.open(new_sheet_path) self._editor.open(new_path)
# if the cheatsheet exists but not in the default_path, copy it to the # if the cheatsheet exists but not in the default_path, copy it to the
# default path before editing # default path before editing
elif self._exists(sheet) and not self._exists_in_default_path(sheet): elif self._exists(sheet) and not self._exists_in_default_path(sheet):
try: try:
shutil.copy(self._path(sheet), shutil.copy(
os.path.join(self._config.cheat_default_dir, sheet)) self._path(sheet),
os.path.join(self._config.cheat_default_dir, sheet)
)
# fail gracefully if the cheatsheet cannot be copied. This can happen # fail gracefully if the cheatsheet cannot be copied. This can
# if CHEAT_DEFAULT_DIR does not exist # happen if CHEAT_DEFAULT_DIR does not exist
except IOError: except IOError:
Utils.die('Could not copy cheatsheet for editing.') Utils.die('Could not copy cheatsheet for editing.')

View File

@ -8,7 +8,7 @@ class Sheets:
def __init__(self, config): def __init__(self, config):
self._config = config self._config = config
self._colorize = Colorize(config); self._colorize = Colorize(config)
# Assembles a dictionary of cheatsheets as name => file-path # Assembles a dictionary of cheatsheets as name => file-path
self._sheets = {} self._sheets = {}

View File

@ -17,7 +17,7 @@ class Utils:
@staticmethod @staticmethod
def boolify(value): 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 `value` is not a string, return it as-is
if not isinstance(value, str): if not isinstance(value, str):
return value return value

9
ci/lint.sh Executable file
View File

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