How to use the flake8.formatting.base.BaseFormatter function in flake8

To help you get started, we’ve selected a few flake8 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github PyCQA / flake8 / tests / unit / test_base_formatter.py View on Github external
def test_show_source_updates_physical_line_appropriately(line1, line2, column):
    """Ensure the error column is appropriately indicated."""
    formatter = base.BaseFormatter(options(show_source=True))
    error = style_guide.Violation('A000', 'file.py', 1, column, 'error', line1)
    output = formatter.show_source(error)
    assert output == line1 + line2
github PyCQA / flake8 / tests / unit / test_base_formatter.py View on Github external
def test_show_source_returns_nothing_when_there_is_source():
    """Ensure we return nothing when there is no line."""
    formatter = base.BaseFormatter(options(show_source=True))
    assert formatter.show_source(
        style_guide.Violation('A000', 'file.py', 1, 1, 'error text', None)
    ) == ''
github PyCQA / flake8 / tests / unit / test_legacy_api.py View on Github external
def test_styleguide_init_report():
    """Verify we do the right incantation for the Application."""
    app = mock.Mock(guide='fake')
    style_guide = api.StyleGuide(app)

    class FakeFormatter(formatter.BaseFormatter):
        def format(self, *args):
            pass

    style_guide.init_report(FakeFormatter)
    app.make_formatter.assert_called_once_with(FakeFormatter)
    assert app.guide is None
    app.make_guide.assert_called_once_with()
github PyCQA / flake8 / tests / unit / test_base_formatter.py View on Github external
def test_format_needs_to_be_implemented():
    """Ensure BaseFormatter#format raises a NotImplementedError."""
    formatter = base.BaseFormatter(options())
    with pytest.raises(NotImplementedError):
        formatter.format(
            style_guide.Violation('A000', 'file.py', 1, 1, 'error text', None)
        )
github PyCQA / flake8 / tests / unit / test_base_formatter.py View on Github external
def test_write_uses_an_output_file(tee):
    """Verify that we use the output file when it's present."""
    line = 'Something to write'
    source = 'source'
    filemock = mock.Mock()

    formatter = base.BaseFormatter(options(tee=tee))
    formatter.output_fd = filemock

    with mock.patch('flake8.formatting.base.print') as print_func:
        formatter.write(line, source)
        if tee:
            assert print_func.called
            assert print_func.mock_calls == [
                mock.call(line, end='\n'),
                mock.call(source, end='\n'),
            ]
        else:
            assert not print_func.called

    assert filemock.write.called is True
    assert filemock.write.call_count == 2
    assert filemock.write.mock_calls == [
github PyCQA / flake8 / tests / unit / test_base_formatter.py View on Github external
def test_start(filename):
    """Verify we open a new file in the start method."""
    mock_open = mock.mock_open()
    formatter = base.BaseFormatter(options(output_file=filename))
    with mock.patch('flake8.formatting.base.open', mock_open):
        formatter.start()

    if filename is None:
        assert mock_open.called is False
    else:
        mock_open.assert_called_once_with(filename, 'a')
github lordmauve / flake8-html / flake8_html / plugin.py View on Github external
def find_severity(code):
    """Given a flake8-style error code, return an ordinal severity."""
    for prefix, sev in SEVERITY_ORDER:
        if code.startswith(prefix):
            return sev
    return DEFAULT_SEVERITY


IndexEntry = namedtuple(
    'IndexEntry',
    'filename report_name error_count highest_sev'
)


class HTMLPlugin(base.BaseFormatter):
    """A plugin for flake8 to render errors as HTML reports."""

    name = 'flake8-html'
    version = importlib_metadata.version('flake8-html')

    def after_init(self):
        """Configure the plugin run."""
        self.report_template = jinja2_env.get_template('file-report.html')
        self.source_template = jinja2_env.get_template('annotated-source.html')
        self.outdir = self.options.htmldir
        if not self.outdir:
            sys.exit('--htmldir must be given if HTML output is enabled')

        self.pep8report = self.options.htmlpep8

        if not os.path.isdir(self.outdir):
github PyCQA / flake8 / src / flake8 / api / legacy.py View on Github external
def init_report(self, reporter=None):
        """Set up a formatter for this run of Flake8."""
        if reporter is None:
            return
        if not issubclass(reporter, formatter.BaseFormatter):
            raise ValueError(
                "Report should be subclass of "
                "flake8.formatter.BaseFormatter."
            )
        self._application.formatter = None
        self._application.make_formatter(reporter)
        self._application.guide = None
        # NOTE(sigmavirus24): This isn't the intended use of
        # Application#make_guide but it works pretty well.
        # Stop cringing... I know it's gross.
        self._application.make_guide()
        self._application.file_checker_manager = None
        self._application.make_file_checker_manager()
github JetBrains / teamcity-messages / teamcity / flake8_v3_plugin.py View on Github external
import re
from io import BytesIO

from flake8.formatting import base

from teamcity.messages import TeamcityServiceMessages
from teamcity import __version__, is_running_under_teamcity


class TeamcityReport(base.BaseFormatter):
    name = 'teamcity-messages'
    version = __version__

    @staticmethod
    def _add_option(parser, name, *args, **kwargs):
        if all(option.long_option_name != name for option in parser.options):
            parser.add_option(name, *args, **kwargs)

    @classmethod
    def add_options(cls, parser):
        cls._add_option(parser,
                        '--teamcity',
                        default=is_running_under_teamcity(),
                        help="Force output of JetBrains TeamCity service messages")
        cls._add_option(parser,
                        '--no-teamcity',
github life4 / flakehell / flakehell / formatters / _gitlab.py View on Github external
import json
from flake8.formatting.base import BaseFormatter
from .._logic import make_baseline


class GitlabFormatter(BaseFormatter):
    error_format = '{code} {text}'

    def start(self):
        self._write('[')
        self.newline = ''
        self._first_line = True

    def stop(self):
        self._write('\n]\n')

    def handle(self, error):
        # redefined to never output source
        line = self.format(error)
        self._write(line)

    def format(self, error):