2018-12-09 13:35:39 +00:00
|
|
|
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
2021-04-11 13:29:34 +00:00
|
|
|
# For details: https://github.com/PyCQA/pylint/blob/master/LICENSE
|
2018-12-09 13:35:39 +00:00
|
|
|
|
|
|
|
import sys
|
|
|
|
|
2019-03-10 21:10:12 +00:00
|
|
|
from pylint.constants import MSG_TYPES
|
2018-12-09 13:35:39 +00:00
|
|
|
from pylint.exceptions import InvalidMessageError
|
2019-03-09 17:59:44 +00:00
|
|
|
from pylint.utils import normalize_text
|
2018-12-09 13:35:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MessageDefinition:
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
checker,
|
|
|
|
msgid,
|
|
|
|
msg,
|
2019-03-22 21:27:20 +00:00
|
|
|
description,
|
2018-12-09 13:35:39 +00:00
|
|
|
symbol,
|
|
|
|
scope,
|
|
|
|
minversion=None,
|
|
|
|
maxversion=None,
|
|
|
|
old_names=None,
|
|
|
|
):
|
2019-10-18 09:13:50 +00:00
|
|
|
self.checker_name = checker.name
|
2019-08-10 11:10:00 +00:00
|
|
|
self.check_msgid(msgid)
|
2018-12-09 13:35:39 +00:00
|
|
|
self.msgid = msgid
|
2019-06-25 21:25:02 +00:00
|
|
|
self.symbol = symbol
|
2018-12-09 13:35:39 +00:00
|
|
|
self.msg = msg
|
2019-03-22 21:27:20 +00:00
|
|
|
self.description = description
|
2018-12-09 13:35:39 +00:00
|
|
|
self.scope = scope
|
|
|
|
self.minversion = minversion
|
|
|
|
self.maxversion = maxversion
|
2019-08-10 11:10:00 +00:00
|
|
|
self.old_names = []
|
|
|
|
if old_names:
|
|
|
|
for old_msgid, old_symbol in old_names:
|
|
|
|
self.check_msgid(old_msgid)
|
|
|
|
self.old_names.append([old_msgid, old_symbol])
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def check_msgid(msgid: str) -> None:
|
|
|
|
if len(msgid) != 5:
|
2021-02-21 13:26:22 +00:00
|
|
|
raise InvalidMessageError(f"Invalid message id {msgid!r}")
|
2019-08-10 11:10:00 +00:00
|
|
|
if msgid[0] not in MSG_TYPES:
|
2021-02-21 13:26:22 +00:00
|
|
|
raise InvalidMessageError(f"Bad message type {msgid[0]} in {msgid!r}")
|
2018-12-09 13:35:39 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2021-02-21 12:27:11 +00:00
|
|
|
return f"MessageDefinition:{self.symbol} ({self.msgid})"
|
2019-08-04 09:53:19 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2021-02-21 13:26:22 +00:00
|
|
|
return f"{repr(self)}:\n{self.msg} {self.description}"
|
2018-12-09 13:35:39 +00:00
|
|
|
|
|
|
|
def may_be_emitted(self):
|
|
|
|
"""return True if message may be emitted using the current interpreter"""
|
|
|
|
if self.minversion is not None and self.minversion > sys.version_info:
|
|
|
|
return False
|
|
|
|
if self.maxversion is not None and self.maxversion <= sys.version_info:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def format_help(self, checkerref=False):
|
|
|
|
"""return the help string for the given message id"""
|
2019-03-22 21:27:20 +00:00
|
|
|
desc = self.description
|
2018-12-09 13:35:39 +00:00
|
|
|
if checkerref:
|
2019-10-18 09:13:50 +00:00
|
|
|
desc += " This message belongs to the %s checker." % self.checker_name
|
2018-12-09 13:35:39 +00:00
|
|
|
title = self.msg
|
|
|
|
if self.minversion or self.maxversion:
|
|
|
|
restr = []
|
|
|
|
if self.minversion:
|
2021-05-24 11:31:09 +00:00
|
|
|
restr.append("< %s" % ".".join(str(n) for n in self.minversion))
|
2018-12-09 13:35:39 +00:00
|
|
|
if self.maxversion:
|
2021-05-24 11:31:09 +00:00
|
|
|
restr.append(">= %s" % ".".join(str(n) for n in self.maxversion))
|
2018-12-09 13:35:39 +00:00
|
|
|
restr = " or ".join(restr)
|
|
|
|
if checkerref:
|
|
|
|
desc += " It can't be emitted when using Python %s." % restr
|
|
|
|
else:
|
|
|
|
desc += " This message can't be emitted when using Python %s." % restr
|
2019-06-25 21:25:02 +00:00
|
|
|
msg_help = normalize_text(" ".join(desc.split()), indent=" ")
|
2021-02-21 12:27:11 +00:00
|
|
|
message_id = f"{self.symbol} ({self.msgid})"
|
2018-12-09 13:35:39 +00:00
|
|
|
if title != "%s":
|
|
|
|
title = title.splitlines()[0]
|
2021-02-21 12:27:11 +00:00
|
|
|
return ":{}: *{}*\n{}".format(message_id, title.rstrip(" "), msg_help)
|
|
|
|
return f":{message_id}:\n{msg_help}"
|