-
Notifications
You must be signed in to change notification settings - Fork 0
/
LineRemover.py
53 lines (41 loc) · 1.79 KB
/
LineRemover.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import re
from Logger import logger
class LineRemoverRule:
def __init__(self):
pass
def apply(self, full_lines: str):
logger.error("LineRemoverRule.apply() not implemented")
pass
class LineRemoverRegexRule(LineRemoverRule):
def __init__(self, condition: str, pattern: str, replacement: str):
super(LineRemoverRegexRule, self).__init__()
self.condition = condition
self.pattern = pattern
self.replacement = replacement
def apply(self, full_lines: str):
if self.condition in full_lines:
return re.sub(self.pattern, self.replacement, full_lines)
return full_lines
class LineRemoverLogicRule(LineRemoverRule):
def __init__(self, condition: str, logic):
super(LineRemoverLogicRule, self).__init__()
self.condition = condition
self.logic = logic
def apply(self, full_lines: str):
if self.condition in full_lines:
return self.logic(full_lines)
return full_lines
class LineRemover:
def __init__(self):
self.rules = [
LineRemoverLogicRule("830 for SSH connections",
lambda x: "\n".join([line for line in x.split("\n") if ">" in line or "<" in line])),
LineRemoverRegexRule("Session 0: Sending message",
r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z Dbg: .*? Session \d+: (?:Sending|Received) message:",
""),
LineRemoverRegexRule("Sending",
r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{7} INF (?:Sending|Received) message: ", "")]
def remove_unwanted_parts(self, full_lines: str):
for rule in self.rules:
full_lines = rule.apply(full_lines)
return full_lines