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.' 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/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..d38fde6 100644 --- a/saltlint/formatters/__init__.py +++ b/saltlint/formatters/__init__.py @@ -5,77 +5,92 @@ 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", + "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", + } -class Formatter(object): + if not use: + for color in colors: + colors[color] = '' - def process(self, matches, colored=False): + return colors + + +class BaseFormatter(object): + + 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() + + +class Formatter(BaseFormatter): - def format(self, match, colored=False): + def format(self, match): 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) - - -class SeverityFormatter(object): - def process(self, matches, colored=False): - for match in matches: - print(self.format(match, colored)) - def format(self, match, colored=False): + 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(BaseFormatter): + + def format(self, match): 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)) - - -class JsonFormatter(object): + 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(BaseFormatter): def process(self, matches, *args, **kwargs): items = [] 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=[