forked from OceanDataTools/openrvdas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefix_transform.py
74 lines (59 loc) · 2.87 KB
/
prefix_transform.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
import logging
import re
import sys
from os.path import dirname, realpath
sys.path.append(dirname(dirname(dirname(realpath(__file__)))))
from logger.transforms.transform import Transform # noqa: E402
################################################################################
class PrefixTransform(Transform):
"""Prepend a prefix to a text record."""
def __init__(self, prefix, sep=' ', quiet=False):
"""Prepend the specified prefix to the record, using space as the default
separator. If prefix is a <regex>:prefix map, go through in order and use
the prefix of the first regex that matches. If no prefix matches, raise a
warning (if quiet != True) and return None.
Note: order of map evaluation is supposed to be guaranteed as of Python 3.7,
but trust at your own risk.
Note: the empty string '' as a regex will match all non-empty strings, so
if you trust Python ordering, it can be used as a backstop default match for
all records that don't match other strings.
prefix Either a simple string prefix or a dict mapping. If a string, it
will be used as the prefix. If a dict, it will be interpreted as
a <regex>:prefix mapping, and the prefix corresponding to the
first matching regex will be used. If no regex matches, None will
be returned.
sep A separator to be used between prefix and record.
quiet If true, do not log a warning if no regex matches.
"""
if type(prefix) is str:
self.prefix = prefix + sep
elif type(prefix) is dict:
self.prefix = {re.compile(regex): pre + sep for regex, pre in prefix.items()}
else:
raise TypeError(f'prefix argument in PrefixTransform must be either a str or '
f'dict. Received type "{type(prefix)}": {prefix}')
self.quiet = quiet
############################
def transform(self, record):
"""Prepend a prefix."""
if record is None:
return None
# If we've got a list, hope it's a list of records. Recurse,
# calling transform() on each of the list elements in order and
# return the resulting list.
if type(record) is list:
results = []
for single_record in record:
results.append(self.transform(single_record))
return results
if type(self.prefix) is str:
return self.prefix + record
# If here, we've got a dict
for regex, pre in self.prefix.items():
if regex.search(record) is not None:
return pre + record
# If here, nothing matched
if not self.quiet:
logging.warning(f'PrefixTransform: no prefix pattern matched record "{record}"')
return None