forked from OceanDataTools/openrvdas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimestamp_transform.py
44 lines (34 loc) · 1.58 KB
/
timestamp_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
#!/usr/bin/env python3
import sys
from os.path import dirname, realpath
sys.path.append(dirname(dirname(dirname(realpath(__file__)))))
from logger.utils import formats # noqa: E402
from logger.utils import timestamp # noqa: E402
from logger.transforms.transform import Transform # noqa: E402
################################################################################
"""Prepend a timestamp to a text record."""
class TimestampTransform(Transform):
def __init__(self, time_format=timestamp.TIME_FORMAT,
time_zone=timestamp.timezone.utc, sep=' '):
"""If timestamp_format is not specified, use default format"""
super().__init__(input_format=formats.Text, output_format=formats.Text)
self.time_format = time_format
self.time_zone = time_zone
self.sep = sep
############################
def transform(self, record, ts=None):
"""Prepend a timestamp"""
if record is None:
return None
# First off, grab a current timestamp
ts = ts or timestamp.time_str(time_format=self.time_format,
time_zone=self.time_zone)
# 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. Give each element the same timestamp.
if type(record) is list:
results = []
for single_record in record:
results.append(self.transform(single_record, ts=ts))
return results
return ts + self.sep + record