<commit_before># -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import logging

from badwolf.lint import Problem
from badwolf.lint.linters import Linter
from badwolf.lint.utils import in_path, run_command


logger = logging.getLogger(__name__)


class Flake8Linter(Linter):
    name = 'flake8'
    default_pattern = '*.py'

    def is_usable(self):
        return in_path('flake8')

    def lint_files(self, files):
        command = ['flake8', '--filename', '*.py*']
        command += files
        _, output = run_command(command, split=True, cwd=self.working_dir)
        if not output:
            raise StopIteration()

        for line in output:
            filename, line, message = self._parse_line(line)
            yield Problem(filename, line, message, self.name)

    def _parse_line(self, line):
        """flake8 only generates results as stdout.
        Parse the output for real data."""
        parts = line.split(':', 3)
        if len(parts) == 3:
            message = parts[2].strip()
        else:
            message = parts[3].strip()
        return parts[0], int(parts[1]), message
<commit_msg>Auto merge pull request #11: flake8 允许指定 Python 版本<commit_after># -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import sys
import logging

from badwolf.lint import Problem
from badwolf.lint.linters import Linter
from badwolf.lint.utils import in_path, run_command


logger = logging.getLogger(__name__)


class Flake8Linter(Linter):
    name = 'flake8'
    default_pattern = '*.py'

    def is_usable(self):
        return in_path('flake8')

    def lint_files(self, files):
        python_version = int(self.options.get('python_version', sys.version_info.major))
        if python_version not in (2, 3):
            python_version = sys.version_info.major

        python = 'python{}'.format(python_version)
        command = [python, '-m', 'flake8', '--filename', '*.py*']
        command += files
        _, output = run_command(command, split=True, cwd=self.working_dir)
        if not output:
            raise StopIteration()

        for line in output:
            filename, line, message = self._parse_line(line)
            yield Problem(filename, line, message, self.name)

    def _parse_line(self, line):
        """flake8 only generates results as stdout.
        Parse the output for real data."""
        parts = line.split(':', 3)
        if len(parts) == 3:
            message = parts[2].strip()
        else:
            message = parts[3].strip()
        return parts[0], int(parts[1]), message

{'fraction_non_alphanumeric': 0.11606456571867795, 'fraction_numerical': 0.011145272867025366, 'mean_word_length': 2.6661971830985913, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6249656', 'n_tokens_mistral': 797, 'n_tokens_neox': 770, 'n_words': 217}