Coverage for /Users/davegaeddert/Development/dropseed/plain/plain/plain/preflight/messages.py: 56%

1# Levels

2 DEBUG = 10

3 INFO = 20

4 WARNING = 30

5 ERROR = 40

6 CRITICAL = 50

7

8

9 class CheckMessage:

10 def __init__(self, level, msg, hint=None, obj=None, id=None):

11 if not isinstance(level, int):

12 raise TypeError("The first argument should be level.")

13 self.level = level

14 self.msg = msg

15 self.hint = hint

16 self.obj = obj

17 self.id = id

18

19 def __eq__(self, other):

20 return isinstance(other, self.__class__) and all(

21 getattr(self, attr) == getattr(other, attr)

22 for attr in ["level", "msg", "hint", "obj", "id"]

23 )

24

25 def __str__(self):

26 try:

27 from plain import models

28

29 ModelBase = models.base.ModelBase

30 using_db = True

31 except ImportError:

32 using_db = False

33 ModelBase = object

34

35 if self.obj is None:

36 obj = "?"

37 elif using_db and isinstance(self.obj, ModelBase):

38 # We need to hardcode ModelBase and Field cases because its __str__

39 # method doesn't return "applabel.modellabel" and cannot be changed.

40 obj = self.obj._meta.label

41 else:

42 obj = str(self.obj)

43 id = "(%s) " % self.id if self.id else ""

44 hint = "\n\tHINT: %s" % self.hint if self.hint else ""

45 return f"{obj}: {id}{self.msg}{hint}"

46

47 def __repr__(self):

48 return "<{}: level={!r}, msg={!r}, hint={!r}, obj={!r}, id={!r}>".format(

49 self.__class__.__name__,

50 self.level,

51 self.msg,

52 self.hint,

53 self.obj,

54 self.id,

55 )

56

57 def is_serious(self, level=ERROR):

58 return self.level >= level

59

60 def is_silenced(self):

61 from plain.runtime import settings

62

63 return self.id in settings.SILENCED_PREFLIGHT_CHECKS

64

65

66 class Debug(CheckMessage):

67 def __init__(self, *args, **kwargs):

68 super().__init__(DEBUG, *args, **kwargs)

69

70

71 class Info(CheckMessage):

72 def __init__(self, *args, **kwargs):

73 super().__init__(INFO, *args, **kwargs)

74

75

76 class Warning(CheckMessage):

77 def __init__(self, *args, **kwargs):

78 super().__init__(WARNING, *args, **kwargs)

79

80

81 class Error(CheckMessage):

82 def __init__(self, *args, **kwargs):

83 super().__init__(ERROR, *args, **kwargs)

84

85

86 class Critical(CheckMessage):

87 def __init__(self, *args, **kwargs):

88 super().__init__(CRITICAL, *args, **kwargs)