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.sheet import Sheet
from cheat.sheets import Sheets
from cheat.utils import Utils
from docopt import docopt
import os

View File

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

View File

@ -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():

View File

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

View File

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

View File

@ -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 = {}

View File

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

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