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

Detect log file format

parent 137f5f3f
No related branches found
No related tags found
No related merge requests found
from pathlib import Path 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]): def read_soh_packet_base(packet: List[str]):
pass pass
...@@ -51,11 +93,13 @@ class LogFileReader: ...@@ -51,11 +93,13 @@ class LogFileReader:
def __init__(self, file_path: Path): def __init__(self, file_path: Path):
self.file_path = file_path self.file_path = file_path
self.packet_reader = None self.packet_reader = None
self.log_file_type: LogFileFormat self.log_file_type: Optional[LogFileFormat] = None
def read(self): def read(self):
log_file = LogFile(self.file_path) log_file = LogFile(self.file_path)
for packet in log_file: for packet in log_file:
if self.log_file_type is None:
self.log_file_type = detect_log_file_packet_format(packet)
pass 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