<commit_before>#     Copyright 2015, <NAME>, mailto:<EMAIL>
#
#     Part of "Nuitka", an optimizing Python compiler that is compatible and
#     integrates with CPython, but also works on its own.
#
#     Licensed under the Apache License, Version 2.0 (the "License");
#     you may not use this file except in compliance with the License.
#     You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#     Unless required by applicable law or agreed to in writing, software
#     distributed under the License is distributed on an "AS IS" BASIS,
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#     See the License for the specific language governing permissions and
#     limitations under the License.
#
""" Standard plug-in to take advantage of pylint or PyDev annotations.

Nuitka can detect some things that PyLint and PyDev will complain about too,
and sometimes it's a false alarm, so people add disable markers into their
source code. Nuitka does it itself.

This tries to parse the code for these markers and uses hooks to prevent Nuitka
from warning about things, disabled to PyLint or Eclipse. The idea is that we
won't have another mechanism for Nuitka, but use existing ones instead.

The code for this is very incomplete, barely good enough to cover Nuitka's own
usage of PyLint markers. PyDev is still largely to be started. You are welcome
to grow both.

"""



import re

from nuitka.plugins.PluginBase import NuitkaPluginBase


class NuitkaPluginPylintEclipseAnnotations(NuitkaPluginBase):
    plugin_name = "pylint-warnings"

    def __init__(self):
        self.line_annotations = {}

    def onModuleSourceCode(self, module_name, source_code):
        self.line_annotations[module_name] = {}

        for count, line in enumerate(source_code.split('\n')):
            match = re.search(r"#.*pylint:\s*disable=\s*([\w,]+)", line)

            if match:
                comment_only = line[:line.find('#')-1].strip() == ""

                if comment_only:
                    # TODO: Parse block wide annotations too.
                    pass
                else:
                    self.line_annotations[module_name][count+1] = set(
                        match.strip()
                        for match in
                        match.group(1).split(',')
                    )

        # Do nothing to it.
        return source_code

    def suppressUnknownImportWarning(self, importing, module_name, source_ref):
        annotations = self.line_annotations[importing.getFullName()].get(source_ref.getLineNumber(), ())

        if "F0401" in annotations:
            return True

        return False


class NuitkaPluginDetectorPylintEclipseAnnotations(NuitkaPluginBase):
    plugin_name = "pylint-warnings"

    @staticmethod
    def isRelevant():
        return True

    def onModuleSourceCode(self, module_name, source_code):
        if re.search(r"#\s*pylint:\s*disable=\s*(\w+)", source_code):
            self.warnUnusedPlugin("Understand PyLint/PyDev annotations for warnings.")

        # Do nothing to it.
        return source_code
<commit_msg>Plugins: The PyLint plugin could crash in some cases.<commit_after>#     Copyright 2015, <NAME>, mailto:<EMAIL>
#
#     Part of "Nuitka", an optimizing Python compiler that is compatible and
#     integrates with CPython, but also works on its own.
#
#     Licensed under the Apache License, Version 2.0 (the "License");
#     you may not use this file except in compliance with the License.
#     You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#     Unless required by applicable law or agreed to in writing, software
#     distributed under the License is distributed on an "AS IS" BASIS,
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#     See the License for the specific language governing permissions and
#     limitations under the License.
#
""" Standard plug-in to take advantage of pylint or PyDev annotations.

Nuitka can detect some things that PyLint and PyDev will complain about too,
and sometimes it's a false alarm, so people add disable markers into their
source code. Nuitka does it itself.

This tries to parse the code for these markers and uses hooks to prevent Nuitka
from warning about things, disabled to PyLint or Eclipse. The idea is that we
won't have another mechanism for Nuitka, but use existing ones instead.

The code for this is very incomplete, barely good enough to cover Nuitka's own
usage of PyLint markers. PyDev is still largely to be started. You are welcome
to grow both.

"""



import re

from nuitka.plugins.PluginBase import NuitkaPluginBase


class NuitkaPluginPylintEclipseAnnotations(NuitkaPluginBase):
    plugin_name = "pylint-warnings"

    def __init__(self):
        self.line_annotations = {}

    def onModuleSourceCode(self, module_name, source_code):
        annotations = {}

        for count, line in enumerate(source_code.split('\n')):
            match = re.search(r"#.*pylint:\s*disable=\s*([\w,]+)", line)

            if match:
                comment_only = line[:line.find('#')-1].strip() == ""

                if comment_only:
                    # TODO: Parse block wide annotations too.
                    pass
                else:
                    annotations[count+1] = set(
                        match.strip()
                        for match in
                        match.group(1).split(',')
                    )

        # Only remember them if there were any.
        if annotations:
            self.line_annotations[module_name] = annotations

        # Do nothing to it.
        return source_code

    def suppressUnknownImportWarning(self, importing, module_name, source_ref):
        annotations = self.line_annotations.get(importing.getFullName(), {})

        line_annotations = annotations.get(source_ref.getLineNumber(), ())

        if "F0401" in line_annotations:
            return True

        return False


class NuitkaPluginDetectorPylintEclipseAnnotations(NuitkaPluginBase):
    plugin_name = "pylint-warnings"

    @staticmethod
    def isRelevant():
        return True

    def onModuleSourceCode(self, module_name, source_code):
        if re.search(r"#\s*pylint:\s*disable=\s*(\w+)", source_code):
            self.warnUnusedPlugin("Understand PyLint/PyDev annotations for warnings.")

        # Do nothing to it.
        return source_code

{'fraction_non_alphanumeric': 0.08008624672724472, 'fraction_numerical': 0.004620360388110273, 'mean_word_length': 2.6606538895152196, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1215280', 'n_tokens_mistral': 1814, 'n_tokens_neox': 1722, 'n_words': 643}