Skip to content

Commit

Permalink
new: [mcsettingsevents] new parser for MC Settings Events
Browse files Browse the repository at this point in the history
  • Loading branch information
cvandeplas committed Nov 5, 2024
1 parent 7814d8a commit a24f910
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/sysdiagnose/parsers/mcsettingsevents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#! /usr/bin/env python3

import os
from sysdiagnose.utils.base import BaseParserInterface
import sysdiagnose.utils.misc as misc
from datetime import datetime, timezone


class McSettingsEventsParser(BaseParserInterface):
description = "Parsing MC Settings Events plist file"
format = "jsonl" # by default json

def __init__(self, config: dict, case_id: str):
super().__init__(__file__, config, case_id)

def get_log_files(self) -> list:
log_files = [
"logs/MCState/Shared/MCSettingsEvents.plist"
]
return [os.path.join(self.case_data_subfolder, log_files) for log_files in log_files]

def execute(self) -> list | dict:
'''
this is the function that will be called
'''
result = []
log_files = self.get_log_files()
for log_file in log_files:
json_data = misc.load_plist_file_as_json(log_file)
for entry in McSettingsEventsParser.traverse_and_collect(json_data):
result.append(entry)

return result

def traverse_and_collect(data, path=""):
'''
recursively traverse json_data and search for dicts that contain the 'timestamp' key.
when found, convert the value of the 'timestamp' key to a datetime object and add it to the entry dict.
also add a field 'setting' with the path of the dict in the json_data where the 'timestamp' key was found. (each depth joined by a dot)
'''
for key, value in data.items():
current_path = f"{path}.{key}" if path else key
if 'timestamp' in value:
try:
timestamp = datetime.strptime(value['timestamp'], '%Y-%m-%dT%H:%M:%S.%f')
timestamp = timestamp.replace(tzinfo=timezone.utc) # ensure timezone is UTC
entry = value.copy()
entry['datetime'] = timestamp.isoformat(timespec='microseconds')
entry['timestamp'] = timestamp.timestamp()
entry['setting'] = current_path
yield entry
except ValueError:
pass
elif isinstance(value, dict):
for entry in McSettingsEventsParser.traverse_and_collect(value, current_path):
yield entry
24 changes: 24 additions & 0 deletions tests/test_parsers_mcsettingsevents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from sysdiagnose.parsers.mcsettingsevents import McSettingsEventsParser
from tests import SysdiagnoseTestCase
import unittest
import os


class TestParsersMcSettingsEvents(SysdiagnoseTestCase):

def test_parse_mcsettingsevents(self):
for case_id, case in self.sd.cases().items():
p = McSettingsEventsParser(self.sd.config, case_id=case_id)
files = p.get_log_files()
self.assertTrue(len(files) > 0)

p.save_result(force=True)
self.assertTrue(os.path.isfile(p.output_file))

result = p.get_result()
for item in result:
self.assertTrue('timestamp' in item)


if __name__ == '__main__':
unittest.main()

0 comments on commit a24f910

Please sign in to comment.