Skip to content
Snippets Groups Projects
Commit 78128dbc authored by Kien Le's avatar Kien Le
Browse files

Detect log file format

parent 6931aea8
No related branches found
No related tags found
1 merge request!191Read log files generated by users
from pathlib import Path
from typing import List
from typing import List, Literal, Optional
LogFileFormat = Literal['rt2ms', 'logpeek', 'sohstationviewer']
PACKETS = ['SH', 'SC', 'OM', 'DS', 'AD', 'CD', 'FD', 'EH', 'ET']
def detect_log_file_packet_format(packet: List[str]) -> LogFileFormat:
"""
Detect the format of a log file packet. The format can be either rt2ms',
logpeek's, or SOHStationViewer's.
:param packet: a packet extracted from a log file.
:return: the format of packet. Can be either 'rt2ms', 'logpeek', or
'sohstationviewer'.
"""
# We want to take advantage of the metadata written by the various programs
# as much as possible.
if packet[0].startswith('logpeek'):
return 'logpeek'
elif packet[0].startswith('rt2ms'):
return 'rt2ms'
elif packet[0].startswith('sohstationviewer'):
return 'sohstationviewer'
packet_start = packet[0]
# The first line of a packet in a log file generated by rt2ms starts with
# a 2-letter packet type. That is then followed by an empty space and the
# string 'exp'.
if packet_start[:2] in PACKETS and packet_start[3:6] == 'exp':
return 'rt2ms'
# SOHStationViewer stores all events' info at the end of its log file, and
# so if we see the line
# Events:
# at the start of a packet, we know the log file is from SOHStationViewer.
if packet_start.startswith('Events:'):
return 'sohstationviewer'
# Logpeek write its events' info right after an SH packet.
packet_end = packet[-1]
packet_end_with_event_info = (packet_end.startswith('DAS') or
packet_end.startswith('WARNING'))
if (packet_start.startswith('State of Health') and
packet_end_with_event_info):
return 'logpeek'
def detect_log_file_packet_format(packet: List[str]):
pass
def read_soh_packet_base(packet: List[str]):
pass
......@@ -51,11 +93,13 @@ class LogFileReader:
def __init__(self, file_path: Path):
self.file_path = file_path
self.packet_reader = None
self.log_file_type: LogFileFormat
self.log_file_type: Optional[LogFileFormat] = None
def read(self):
log_file = LogFile(self.file_path)
for packet in log_file:
if self.log_file_type is None:
self.log_file_type = detect_log_file_packet_format(packet)
pass
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment