<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# PCEF - Python/Qt Code Editing Framework
# Copyright 2013, <NAME> <<EMAIL>>
#
# This software is released under the LGPLv3 license.
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
This module contains the pyFlakes checker mode
"""
from io import StringIO, BytesIO
import logging
import sys
from pcef.core import CheckerMode, CheckerMessage
from pcef.core import MSG_STATUS_ERROR, MSG_STATUS_WARNING
from pcef.qt import QtGui


class PEP8CheckerMode(CheckerMode):
    DESCRIPTION = "Check python code for PEP8 issues"
    IDENTIFIER = "pep8Checker"

    def __init__(self):
        CheckerMode.__init__(self, clearOnRequest=False)

    def _onInstall(self, editor):
        CheckerMode._onInstall(self, editor)
        try:
            import pep8
        except ImportError:
            logging.warning("Cannot import pep8.py, PEP8CheckerMode disabled")
            self.enabled = False
        else:
            logging.debug("pep8.py found!")

    def run(self, document, filePath):
        assert isinstance(document, QtGui.QTextDocument)
        old_stdout = sys.stdout
        if sys.version_info[0] == 3:
            sys.stdout = mystdout = StringIO()
        else:
            sys.stdout = mystdout = BytesIO()
        self.clearMessagesRequested.emit()
        try:
            self.check(document.toPlainText().splitlines(True), filePath)
            sys.stdout = old_stdout
            self.analyse(mystdout.getvalue().splitlines())
        except TypeError:
            print(document.toPlainText().splitlines(True), filePath)
            pass

    def analyse(self, lines):
        for line in lines:
            tokens = line.split(":")
            nbTokens = len(tokens)
            msg = tokens[nbTokens-1]
            status = MSG_STATUS_WARNING
            if msg.startswith("E"):
                MSG_STATUS_ERROR
            try:
                line = int(tokens[nbTokens-3])
            except IndexError:
                return
            self.addMessageRequested.emit(CheckerMessage(msg, status, line))

    def check(self, lines, filename):
        """
        Checks the python source code using PEP8 checker.

        :param codeString: The code string to check

        :param filename;

        .. note: This is a modified version of the check function found in the
                 pyFlakes script.
        """
        import pep8
        pep8style = pep8.StyleGuide(parse_argv=False, config_file=True)
        pep8style.input_file(filename, lines=lines)<commit_msg>Check for value error<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# PCEF - Python/Qt Code Editing Framework
# Copyright 2013, <NAME> <<EMAIL>>
#
# This software is released under the LGPLv3 license.
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
This module contains the pyFlakes checker mode
"""
from io import StringIO, BytesIO
import logging
import sys
from pcef.core import CheckerMode, CheckerMessage
from pcef.core import MSG_STATUS_ERROR, MSG_STATUS_WARNING
from pcef.qt import QtGui


class PEP8CheckerMode(CheckerMode):
    DESCRIPTION = "Check python code for PEP8 issues"
    IDENTIFIER = "pep8Checker"

    def __init__(self):
        CheckerMode.__init__(self, clearOnRequest=False)

    def _onInstall(self, editor):
        CheckerMode._onInstall(self, editor)
        try:
            import pep8
        except ImportError:
            logging.warning("Cannot import pep8.py, PEP8CheckerMode disabled")
            self.enabled = False
        else:
            logging.debug("pep8.py found!")

    def run(self, document, filePath):
        assert isinstance(document, QtGui.QTextDocument)
        old_stdout = sys.stdout
        if sys.version_info[0] == 3:
            sys.stdout = mystdout = StringIO()
        else:
            sys.stdout = mystdout = BytesIO()
        self.clearMessagesRequested.emit()
        try:
            self.check(document.toPlainText().splitlines(True), filePath)
            sys.stdout = old_stdout
            self.analyse(mystdout.getvalue().splitlines())
        except TypeError:
            print(document.toPlainText().splitlines(True), filePath)
            pass

    def analyse(self, lines):
        for line in lines:
            tokens = line.split(":")
            nbTokens = len(tokens)
            msg = tokens[nbTokens-1]
            status = MSG_STATUS_WARNING
            if msg.startswith("E"):
                MSG_STATUS_ERROR
            try:
                line = int(tokens[nbTokens-3])
            except IndexError:
                return
            except ValueError:
                return
            self.addMessageRequested.emit(CheckerMessage(msg, status, line))

    def check(self, lines, filename):
        """
        Checks the python source code using PEP8 checker.

        :param codeString: The code string to check

        :param filename;

        .. note: This is a modified version of the check function found in the
                 pyFlakes script.
        """
        import pep8
        pep8style = pep8.StyleGuide(parse_argv=False, config_file=True)
        pep8style.input_file(filename, lines=lines)
{'fraction_non_alphanumeric': 0.08964365256124722, 'fraction_numerical': 0.008166295471417966, 'mean_word_length': 2.279975654290931, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7537074', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1515, 'n_words': 421}