TestCenter Reference
MessageFilters.py
Go to the documentation of this file.
1 #
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 
11 from typing import Optional, Sequence
12 from enum import Enum
13 import 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.
17 if "___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 
34  class MessageFilter( object ):
35  """
36  Base class to implement filters for specific log messages, allowing e.g. to expect or ignore errors.
37  """
38 
39  _typeIdentifiers:Sequence[str] = ()
40  _messageType:MessageType
41 
42  def __init__( self, handling:MessageHandling, regEx:Optional[str] ):
43  if isinstance( regEx, str ):
44  regEx = re.compile( regEx )
45  self.__regEx__regEx = regEx
46  self.__handling__handling = handling
47  self.hadMatchhadMatch = False
48  self.hadNonMatchhadNonMatch = 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__isMatch( messageText )
56  self.hadMatchhadMatch |= match
57  self.hadNonMatchhadNonMatch |= not match
58  return match
59 
60  def __isMatch( self, messageText:str ) -> bool:
61  if self.__regEx__regEx:
62  strippedMessageText = STRIP_HTML_TAGS_REGEXP.sub('', messageText)
63  return ( self.__regEx__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__handling == MessageHandling.IGNORE:
74  postFix = " (ignored)"
75  elif self.__handling__handling == MessageHandling.EXPECT:
76  postFix = " (expected)"
77  return postFix
78 
79  def __eq__( self, other ) -> bool:
80  return isinstance( other, self.__class__ ) and \
81  self.__regEx__regEx == other.__regEx and \
82  self._messageType_messageType == other._messageType and \
83  self.__handling__handling == other.__handling
84 
85  @classmethod
86  def GetMessageType( cls ) -> MessageType:
87  return cls._messageType_messageType
88 
89 
90  @classmethod
91  def IsTypeMatch( cls, typeString:str ) -> bool:
92  """
93  Returns True if any of the classes' type identifiers (e.g. 'cerr') occurs in the given typeString.
94  Note that the check is case insensitive.
95  """
96  for t in cls._typeIdentifiers:
97  if t in typeString.lower():
98  return True
99  return False
100 
101 
103  """
104  Filter for error messages
105  """
106  _typeIdentifiers = ( "error", "cerr" )
107  _messageType = MessageType.ERROR
108  def __init__( self, handling:MessageHandling, regEx:Optional[str] ):
109  super().__init__( handling, regEx )
110 
112  """
113  Filter for warning messages
114  """
115  _typeIdentifiers = ( "warning", )
116  _messageType = MessageType.WARNING
117  def __init__( self, handling:MessageHandling, regEx:Optional[str] ):
118  super().__init__( handling, regEx )
119 
121  """
122  Filter for info messages
123  """
124  _typeIdentifiers = ( "info", "cout", "other")
125  _messageType = MessageType.INFO
126  def __init__( self, handling:MessageHandling, regEx:Optional[str] ):
127  super().__init__( handling, regEx )
def __init__(self, MessageHandling handling, Optional[str] regEx)
def __init__(self, MessageHandling handling, Optional[str] regEx)
bool IsTypeMatch(cls, str typeString)
bool __isMatch(self, str messageText)
def __init__(self, MessageHandling handling, Optional[str] regEx)
bool match(self, str messageText)
def __init__(self, MessageHandling handling, Optional[str] regEx)