openrvdas.logger.transforms.regex_replace_transform

No module-level documentation available.
 1#!/usr/bin/env python3
 2
 3import re
 4
 5from logger.transforms.transform import Transform  # noqa: E402
 6
 7
 8################################################################################
 9class RegexReplaceTransform(Transform):
10    """Apply regex replacements to record."""
11    ############################
12
13    def __init__(self, patterns, count=0, flags=0, **kwargs):
14        """
15        patterns - a dict of {old:new, old:new} patterns to be searched and replaced.
16                   Note that replacement order is not guaranteed.
17        """
18        super().__init__(**kwargs)  # processes 'quiet' and type hints
19
20        self.patterns = patterns
21        self.count = count
22        self.flags = flags
23
24    ############################
25    def transform(self, record: str) -> str:
26        """Does record contain pattern?"""
27
28        # See if it's something we can process, and if not, try digesting
29        if not self.can_process_record(record):  # inherited from BaseModule()
30            return self.digest_record(record)  # inherited from BaseModule()
31
32        # Apply all patterns in order
33        result = record
34        for old_str, new_str in self.patterns.items():
35            # FIX: Pass count and flags as keyword arguments to avoid DeprecationWarning
36            result = re.sub(old_str, new_str, result, count=self.count, flags=self.flags)
37        return result
class RegexReplaceTransform(logger.transforms.transform.Transform):
10class RegexReplaceTransform(Transform):
11    """Apply regex replacements to record."""
12    ############################
13
14    def __init__(self, patterns, count=0, flags=0, **kwargs):
15        """
16        patterns - a dict of {old:new, old:new} patterns to be searched and replaced.
17                   Note that replacement order is not guaranteed.
18        """
19        super().__init__(**kwargs)  # processes 'quiet' and type hints
20
21        self.patterns = patterns
22        self.count = count
23        self.flags = flags
24
25    ############################
26    def transform(self, record: str) -> str:
27        """Does record contain pattern?"""
28
29        # See if it's something we can process, and if not, try digesting
30        if not self.can_process_record(record):  # inherited from BaseModule()
31            return self.digest_record(record)  # inherited from BaseModule()
32
33        # Apply all patterns in order
34        result = record
35        for old_str, new_str in self.patterns.items():
36            # FIX: Pass count and flags as keyword arguments to avoid DeprecationWarning
37            result = re.sub(old_str, new_str, result, count=self.count, flags=self.flags)
38        return result

Apply regex replacements to record.

RegexReplaceTransform(patterns, count=0, flags=0, **kwargs)
14    def __init__(self, patterns, count=0, flags=0, **kwargs):
15        """
16        patterns - a dict of {old:new, old:new} patterns to be searched and replaced.
17                   Note that replacement order is not guaranteed.
18        """
19        super().__init__(**kwargs)  # processes 'quiet' and type hints
20
21        self.patterns = patterns
22        self.count = count
23        self.flags = flags

patterns - a dict of {old:new, old:new} patterns to be searched and replaced. Note that replacement order is not guaranteed.

patterns
count
flags
def transform(self, record: str) -> str:
26    def transform(self, record: str) -> str:
27        """Does record contain pattern?"""
28
29        # See if it's something we can process, and if not, try digesting
30        if not self.can_process_record(record):  # inherited from BaseModule()
31            return self.digest_record(record)  # inherited from BaseModule()
32
33        # Apply all patterns in order
34        result = record
35        for old_str, new_str in self.patterns.items():
36            # FIX: Pass count and flags as keyword arguments to avoid DeprecationWarning
37            result = re.sub(old_str, new_str, result, count=self.count, flags=self.flags)
38        return result

Does record contain pattern?