Exemple #1
0
    def _present_warnings(self):
        warnings = [w for ws in self._accepted_warning_sets for w in ws]
        self.text.direct_delete("end-2l linestart", "end-1c lineend")

        if not warnings:
            return

        if self._exception_info is None:
            intro = "May be ignored if you are happy with your program."
        else:
            intro = "May help you find the cause of the error."

        rst = (
            self._get_rst_prelude()
            + rst_utils.create_title("Warnings")
            + ":remark:`%s`\n\n" % intro
        )

        by_file = {}
        for warning in warnings:
            if warning["filename"] not in by_file:
                by_file[warning["filename"]] = []
            if warning not in by_file[warning["filename"]]:
                # Pylint may give double warnings (eg. when module imports itself)
                by_file[warning["filename"]].append(warning)

        for filename in by_file:
            rst += "`%s <%s>`__\n\n" % (
                os.path.basename(filename),
                self._format_file_url(dict(filename=filename)),
            )
            file_warnings = sorted(
                by_file[filename], key=lambda x: (x.get("lineno", 0), -x.get("relevance", 1))
            )

            for i, warning in enumerate(file_warnings):
                rst += self._format_warning(warning, i == len(file_warnings) - 1) + "\n"

            rst += "\n"

        self.text.append_rst(rst)

        # save snapshot
        self._current_snapshot["warnings_rst"] = rst
        self._current_snapshot["warnings"] = warnings

        if get_workbench().get_option("assistance.open_assistant_on_warnings"):
            get_workbench().show_view("AssistantView")
Exemple #2
0
    def _explain_exception(self, error_info):
        rst = (
            self._get_rst_prelude() +
            rst_utils.create_title(error_info["type_name"] + ": " +
                                   rst_utils.escape(error_info["message"])) +
            "\n")

        if (error_info.get("lineno") is not None and error_info.get("filename")
                and os.path.exists(error_info["filename"])):
            rst += "`%s, line %d <%s>`__\n\n" % (
                os.path.basename(error_info["filename"]),
                error_info["lineno"],
                self._format_file_url(error_info),
            )

        helpers = [
            helper_class(error_info) for helper_class in (
                _error_helper_classes.get(error_info["type_name"], []) +
                _error_helper_classes["*"])
        ]

        best_intro = helpers[0]
        for helper in helpers:
            if helper.intro_confidence > best_intro.intro_confidence:
                best_intro = helper

        # intro
        if best_intro.intro_text:
            rst += (".. note::\n" + "    " +
                    best_intro.intro_text.strip().replace("\n", "\n\n    ") +
                    "\n\n")

        suggestions = [
            suggestion for helper in helpers
            for suggestion in helper.suggestions if suggestion is not None
        ]
        suggestions = sorted(suggestions,
                             key=lambda s: s.relevance,
                             reverse=True)

        if suggestions[0].relevance > 1 or best_intro.intro_confidence > 1:
            relevance_threshold = 2
        else:
            # use relevance 1 only when there is nothing better
            relevance_threshold = 1

        suggestions = [
            s for s in suggestions if s.relevance >= relevance_threshold
        ]

        for i, suggestion in enumerate(suggestions):
            rst += self._format_suggestion(
                suggestion,
                i == len(suggestions) - 1,
                # TODO: is it good if first is preopened?
                # It looks cleaner if it is not.
                False,  # i==0
            )

        self._current_snapshot["exception_suggestions"] = [
            dict(sug._asdict()) for sug in suggestions
        ]

        self.text.append_rst(rst)
        self._append_text("\n")

        self._current_snapshot["exception_type_name"] = error_info["type_name"]
        self._current_snapshot["exception_message"] = error_info["message"]
        self._current_snapshot["exception_file_path"] = error_info["filename"]
        self._current_snapshot["exception_lineno"] = error_info["lineno"]
        self._current_snapshot["exception_rst"] = rst  # for debugging purposes