From 9e6490008b93a54ff25180ca6507540d5a775946 Mon Sep 17 00:00:00 2001 From: Roald Nefs Date: Sun, 21 Jun 2020 10:32:53 +0200 Subject: [PATCH 1/4] fix: remove salt as a dependency Remove Salt as a dependency of salt-lint by: - Including the `six` and `pyyaml` libraries directly. - Specify the colors directly. This also fixes #135. Signed-off-by: Roald Nefs --- saltlint/config.py | 4 +- saltlint/formatters/__init__.py | 105 +++++++++++++++++--------------- saltlint/linter.py | 4 +- setup.py | 2 +- 4 files changed, 60 insertions(+), 55 deletions(-) diff --git a/saltlint/config.py b/saltlint/config.py index 064491e..67f391d 100644 --- a/saltlint/config.py +++ b/saltlint/config.py @@ -5,9 +5,7 @@ import os import sys import pathspec - -# Import Salt libs -from salt.ext import six +import six import saltlint.utils diff --git a/saltlint/formatters/__init__.py b/saltlint/formatters/__init__.py index f8b0a8f..a8f1175 100644 --- a/saltlint/formatters/__init__.py +++ b/saltlint/formatters/__init__.py @@ -5,11 +5,36 @@ import json -# Import salt libs -try: - import salt.utils.color as saltcolor -except ImportError: - import salt.utils as saltcolor + +def get_colors(use=True): + """ + Return the colors as a dict, pass False to return the colors as empty + strings. + """ + colors = { + "BLACK": "\033[0;30m", # black + "DARK_GRAY": "\033[1;30m", # bold, black + "RED": "\033[0;31m",# red + "LIGHT_RED": "\033[1;31m" , # bold, red + "GREEN": "\033[0;32m", # green + "LIGHT_GREEN": "\033[1;32m", # bold, green + "BLUE": "\033[0;34m", # blue + "LIGHT_BLUE": "\033[1;34m", # bold, blue + "MAGENTA": "\033[0;35m", # magenta, + "LIGHT_MAGENTA": "\033[1;35m", # bold, magenta + "CYAN": "\033[0;36m", # cyan + "LIGHT_CYAN": "\033[1;36m", # bold, cyan + "LIGHT_GRAY": "\033[0;37m", # white + "WHITE": "\033[1;37m", # bold, white + "DEFAULT_COLOR": "\033[00m", # default + "ENDC": "\033[0m", # reset + } + + if not use: + for color in colors: + colors[color] = '' + + return colors class Formatter(object): @@ -20,26 +45,19 @@ def process(self, matches, colored=False): def format(self, match, colored=False): formatstr = u"{0} {1}\n{2}:{3}\n{4}\n" - if colored: - color = saltcolor.get_colors() - return formatstr.format( - u'{0}[{1}]{2}'.format(color['RED'], match.rule.id, - color['ENDC']), - u'{0}{1}{2}'.format(color['LIGHT_RED'], match.message, - color['ENDC']), - u'{0}{1}{2}'.format(color['BLUE'], match.filename, - color['ENDC']), - u'{0}{1}{2}'.format(color['CYAN'], str(match.linenumber), - color['ENDC']), - u'{0}{1}{2}'.format(color['MAGENTA'], match.line, color['ENDC']) - ) - else: - return formatstr.format( - u'[{0}]'.format(match.rule.id), - match.message, - match.filename, - match.linenumber, - match.line) + + color = get_colors(self.colored) + return formatstr.format( + u'{0}[{1}]{2}'.format(color['RED'], match.rule.id, + color['ENDC']), + u'{0}{1}{2}'.format(color['LIGHT_RED'], match.message, + color['ENDC']), + u'{0}{1}{2}'.format(color['BLUE'], match.filename, + color['ENDC']), + u'{0}{1}{2}'.format(color['CYAN'], str(match.linenumber), + color['ENDC']), + u'{0}{1}{2}'.format(color['MAGENTA'], match.line, color['ENDC']) + ) class SeverityFormatter(object): @@ -50,29 +68,20 @@ def process(self, matches, colored=False): def format(self, match, colored=False): formatstr = u"{0} {sev} {1}\n{2}:{3}\n{4}\n" - if colored: - color = saltcolor.get_colors() - return formatstr.format( - u'{0}[{1}]{2}'.format(color['RED'], match.rule.id, - color['ENDC']), - u'{0}{1}{2}'.format(color['LIGHT_RED'], match.message, - color['ENDC']), - u'{0}{1}{2}'.format(color['BLUE'], match.filename, - color['ENDC']), - u'{0}{1}{2}'.format(color['CYAN'], str(match.linenumber), - color['ENDC']), - u'{0}{1}{2}'.format(color['MAGENTA'], match.line, color['ENDC']), - sev=u'{0}[{1}]{2}'.format(color['RED'], match.rule.severity, - color['ENDC']) - ) - else: - return formatstr.format( - u'[{0}]'.format(match.rule.id), - match.message, - match.filename, - match.linenumber, - match.line, - sev=u'[{0}]'.format(match.rule.severity)) + color = get_colors(self.colored) + return formatstr.format( + u'{0}[{1}]{2}'.format(color['RED'], match.rule.id, + color['ENDC']), + u'{0}{1}{2}'.format(color['LIGHT_RED'], match.message, + color['ENDC']), + u'{0}{1}{2}'.format(color['BLUE'], match.filename, + color['ENDC']), + u'{0}{1}{2}'.format(color['CYAN'], str(match.linenumber), + color['ENDC']), + u'{0}{1}{2}'.format(color['MAGENTA'], match.line, color['ENDC']), + sev=u'{0}[{1}]{2}'.format(color['RED'], match.rule.severity, + color['ENDC']) + ) class JsonFormatter(object): diff --git a/saltlint/linter.py b/saltlint/linter.py index bb818f2..0168282 100644 --- a/saltlint/linter.py +++ b/saltlint/linter.py @@ -9,9 +9,7 @@ import re import sys import codecs - -# import Salt libs -from salt.ext import six +import six import saltlint.utils from saltlint.config import SaltLintConfig diff --git a/setup.py b/setup.py index f5f2915..c230667 100644 --- a/setup.py +++ b/setup.py @@ -49,7 +49,7 @@ }, include_package_data=True, python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - install_requires=['salt', 'pathspec>=0.6.0'], + install_requires=['six', 'pyyaml', 'pathspec>=0.6.0'], license=__license__, zip_safe=False, classifiers=[ From 5902c4b4a1115b1a90f91fa92e04608a5850c7ab Mon Sep 17 00:00:00 2001 From: Roald Nefs Date: Sat, 23 Nov 2019 20:13:33 +0100 Subject: [PATCH 2/4] Update Formatter initialization Update Formatter initialization: - Use common BaseFormatter. - Set color flag on initialization of the formatter. Signed-off-by: Roald Nefs Signed-off-by: Roald Nefs --- saltlint/cli.py | 6 +++--- saltlint/formatters/__init__.py | 26 ++++++++++++++++---------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/saltlint/cli.py b/saltlint/cli.py index fd7845f..024aaff 100644 --- a/saltlint/cli.py +++ b/saltlint/cli.py @@ -114,9 +114,9 @@ def run(args=None): if config.json: formatter = formatters.JsonFormatter() elif config.severity: - formatter = formatters.SeverityFormatter() + formatter = formatters.SeverityFormatter(config.colored) else: - formatter = formatters.Formatter() + formatter = formatters.Formatter(config.colored) for state in states: runner = Runner(rules, state, config, checked_files) @@ -126,7 +126,7 @@ def run(args=None): matches.sort(key=lambda x: (x.filename, x.linenumber, x.rule.id)) # Show the matches on the screen - formatter.process(matches, config.colored) + formatter.process(matches) # Delete stdin temporary file if stdin_state: diff --git a/saltlint/formatters/__init__.py b/saltlint/formatters/__init__.py index a8f1175..49f97bf 100644 --- a/saltlint/formatters/__init__.py +++ b/saltlint/formatters/__init__.py @@ -37,13 +37,22 @@ def get_colors(use=True): return colors -class Formatter(object): +class BaseFormatter(object): - def process(self, matches, colored=False): + def __init__(self, colored=False): + self.colored = colored + + def process(self, matches): for match in matches: - print(self.format(match, colored)) + print(self.format(match)) + + def format(self, match): + raise NotImplementedError() + - def format(self, match, colored=False): +class Formatter(BaseFormatter): + + def format(self, match): formatstr = u"{0} {1}\n{2}:{3}\n{4}\n" color = get_colors(self.colored) @@ -60,12 +69,9 @@ def format(self, match, colored=False): ) -class SeverityFormatter(object): - def process(self, matches, colored=False): - for match in matches: - print(self.format(match, colored)) +class SeverityFormatter(BaseFormatter): - def format(self, match, colored=False): + def format(self, match): formatstr = u"{0} {sev} {1}\n{2}:{3}\n{4}\n" color = get_colors(self.colored) @@ -84,7 +90,7 @@ def format(self, match, colored=False): ) -class JsonFormatter(object): +class JsonFormatter(BaseFormatter): def process(self, matches, *args, **kwargs): items = [] From 12d83fae35ab03ed669352ebe553ca8d1edfc5e1 Mon Sep 17 00:00:00 2001 From: Roald Nefs Date: Thu, 25 Jun 2020 21:31:15 +0200 Subject: [PATCH 3/4] chore: bump version to v0.3.0 Signed-off-by: Roald Nefs --- saltlint/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/saltlint/__init__.py b/saltlint/__init__.py index 1b58a3e..90141ba 100644 --- a/saltlint/__init__.py +++ b/saltlint/__init__.py @@ -5,7 +5,7 @@ """ NAME = 'salt-lint' -VERSION = '0.2.2' +VERSION = '0.3.0' DESCRIPTION = __doc__ __author__ = 'Warpnet B.V.' From 2b118e0619538724e4079ec8d838e79f8b26cce2 Mon Sep 17 00:00:00 2001 From: Roald Nefs Date: Thu, 25 Jun 2020 21:44:04 +0200 Subject: [PATCH 4/4] fix: flake8 issues in formatter Signed-off-by: Roald Nefs --- saltlint/formatters/__init__.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/saltlint/formatters/__init__.py b/saltlint/formatters/__init__.py index 49f97bf..d38fde6 100644 --- a/saltlint/formatters/__init__.py +++ b/saltlint/formatters/__init__.py @@ -12,22 +12,22 @@ def get_colors(use=True): strings. """ colors = { - "BLACK": "\033[0;30m", # black - "DARK_GRAY": "\033[1;30m", # bold, black - "RED": "\033[0;31m",# red - "LIGHT_RED": "\033[1;31m" , # bold, red - "GREEN": "\033[0;32m", # green - "LIGHT_GREEN": "\033[1;32m", # bold, green - "BLUE": "\033[0;34m", # blue - "LIGHT_BLUE": "\033[1;34m", # bold, blue - "MAGENTA": "\033[0;35m", # magenta, - "LIGHT_MAGENTA": "\033[1;35m", # bold, magenta - "CYAN": "\033[0;36m", # cyan - "LIGHT_CYAN": "\033[1;36m", # bold, cyan - "LIGHT_GRAY": "\033[0;37m", # white - "WHITE": "\033[1;37m", # bold, white - "DEFAULT_COLOR": "\033[00m", # default - "ENDC": "\033[0m", # reset + "BLACK": "\033[0;30m", + "DARK_GRAY": "\033[1;30m", + "RED": "\033[0;31m", + "LIGHT_RED": "\033[1;31m", + "GREEN": "\033[0;32m", + "LIGHT_GREEN": "\033[1;32m", + "BLUE": "\033[0;34m", + "LIGHT_BLUE": "\033[1;34m", + "MAGENTA": "\033[0;35m", + "LIGHT_MAGENTA": "\033[1;35m", + "CYAN": "\033[0;36m", + "LIGHT_CYAN": "\033[1;36m", + "LIGHT_GRAY": "\033[0;37m", + "WHITE": "\033[1;37m", + "DEFAULT_COLOR": "\033[00m", + "ENDC": "\033[0m", } if not use: