TestCenter Reference
MessageFilters.py
Go to the documentation of this file.
2# Copyright 2020, MeVis Medical Solutions AG
3#
4# The user may use this file in accordance with the license agreement provided with
5# the Software or, alternatively, in accordance with the terms contained in a
6# written agreement between the user and MeVis Medical Solutions AG.
7#
8# For further information use the contact form at https://www.mevislab.de/contact
9#
10
11from typing import Optional, Sequence
12from enum import Enum
13import re
14
15# We need to prevent reloading this module, as otherwise comparisons for MessageType and MessageHandling actually fail if auto-reloading of
16# python modules is enabled.
17if "___HasLoadedMessageFilters" not in globals():
18
19 ___HasLoadedMessageFilters = True
20
21 STRIP_HTML_TAGS_REGEXP = re.compile("<[^>]*>")
22
23 class MessageType(Enum):
24 INFO = 0
25 WARNING = 1
26 ERROR = 2
27
28 class MessageHandling(Enum):
29 NONE = 0
30 EXPECT = 1
31 IGNORE = 2
32
33 class MessageFilter(object):
34 """
35 Base class to implement filters for specific log messages, allowing e.g. to expect or ignore errors.
36 """
37
38 _typeIdentifiers: Sequence[str] = ()
39 _messageType: MessageType
40
41 def __init__(self, handling: MessageHandling, regEx: Optional[str]):
42 if isinstance(regEx, str):
43 regEx = re.compile(regEx)
44 self.__regEx = regEx
45 self.__handling = handling
46 self.hadMatch = False
47 self.hadNonMatch = False
49
50 def match(self, messageText: str) -> bool:
51 """
52 Returns True if the given messageText matches the regex, and also updates the fields
53 hadMatch and hadNonMatch
54 """
55 match = self.__isMatch(messageText)
56 self.hadMatch |= match
57 self.hadNonMatch |= not match
58 return match
59
60 def __isMatch(self, messageText: str) -> bool:
61 if self.__regEx:
62 strippedMessageText = STRIP_HTML_TAGS_REGEXP.sub("", messageText)
63 return self.__regEx.match(strippedMessageText) != None
64 return True
65
66 def getTypePostfix(self) -> str:
67 """
68 Returns the suitable postfix shown in the test report for the message type based on
69 self.__handling
70 """
71 # print( self.__handling, MessageHandling.EXPECT, self.__handling.__class__, MessageHandling.EXPECT.__class__, id( self.__handling ), id( MessageHandling.EXPECT) )
72 postFix = ""
73 if self.__handling == MessageHandling.IGNORE:
74 postFix = " (ignored)"
75 elif self.__handling == MessageHandling.EXPECT:
76 postFix = " (expected)"
77 return postFix
78
79 def __eq__(self, other) -> bool:
80 return (
81 isinstance(other, self.__class__)
82 and self.__regEx == other.__regEx
83 and self._messageType_messageType == other._messageType
84 and self.__handling == other.__handling
85 )
86
87 @classmethod
88 def GetMessageType(cls) -> MessageType:
90
91 @classmethod
92 def IsTypeMatch(cls, typeString: str) -> bool:
93 """
94 Returns True if any of the classes' type identifiers (e.g. 'cerr') occurs in the given typeString.
95 Note that the check is case insensitive.
96 """
97 for t in cls._typeIdentifiers:
98 if t in typeString.lower():
99 return True
100 return False
101
103 """
104 Filter for error messages
105 """
106
107 _typeIdentifiers = ("error", "cerr")
108 _messageType = MessageType.ERROR
109
110 def __init__(self, handling: MessageHandling, regEx: Optional[str]):
111 super().__init__(handling, regEx)
112
114 """
115 Filter for warning messages
116 """
117
118 _typeIdentifiers = ("warning",)
119 _messageType = MessageType.WARNING
120
121 def __init__(self, handling: MessageHandling, regEx: Optional[str]):
122 super().__init__(handling, regEx)
123
125 """
126 Filter for info messages
127 """
128
129 _typeIdentifiers = ("info", "cout", "other")
130 _messageType = MessageType.INFO
131
132 def __init__(self, handling: MessageHandling, regEx: Optional[str], allowUnexpectedMessages: bool):
133 super().__init__(handling, regEx)
__init__(self, MessageHandling handling, Optional[str] regEx)
__init__(self, MessageHandling handling, Optional[str] regEx, bool allowUnexpectedMessages)
Base class to implement filters for specific log messages, allowing e.g.
str getTypePostfix(self)
Returns the suitable postfix shown in the test report for the message type based on self....
bool IsTypeMatch(cls, str typeString)
Returns True if any of the classes' type identifiers (e.g.
bool __isMatch(self, str messageText)
__init__(self, MessageHandling handling, Optional[str] regEx)
bool match(self, str messageText)
Returns True if the given messageText matches the regex, and also updates the fields hadMatch and had...
__init__(self, MessageHandling handling, Optional[str] regEx)