From 8f9d491ca26516597f5f1ab36beca8a94f76db8c Mon Sep 17 00:00:00 2001 From: Christophe Vandeplas Date: Wed, 12 Jun 2024 11:55:25 +0200 Subject: [PATCH] fix: [parser] fix powerlogs not containing the data --- parsers/powerlogs.py | 10 ++++++---- tests/test_parsers_powerlogs.py | 1 + utils/misc.py | 14 ++++++++++++++ utils/sqlite2json.py | 2 +- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/parsers/powerlogs.py b/parsers/powerlogs.py index c7d53af..40f616b 100644 --- a/parsers/powerlogs.py +++ b/parsers/powerlogs.py @@ -7,6 +7,7 @@ from utils import sqlite2json import glob import os +from utils.misc import merge_dicts parser_description = "Parsing powerlogs database" @@ -18,7 +19,7 @@ def get_log_files(log_root_path: str) -> list: """ log_files_globs = [ 'logs/powerlogs/powerlog_*', - # 'logs/powerlogs/log_*' # LATER is this file of interest? + 'logs/powerlogs/log_*' # LATER is this file of interest? ] log_files = [] for log_files_glob in log_files_globs: @@ -27,8 +28,9 @@ def get_log_files(log_root_path: str) -> list: return log_files -def parse_path(path: str) -> list: - result = [] +def parse_path(path: str) -> dict: + result = {} for logfile in get_log_files(path): - result.extend(sqlite2json.sqlite2struct(logfile)) + db_json = sqlite2json.sqlite2struct(logfile) + result = merge_dicts(result, db_json) # merge both return result diff --git a/tests/test_parsers_powerlogs.py b/tests/test_parsers_powerlogs.py index 8c597ea..558e01c 100644 --- a/tests/test_parsers_powerlogs.py +++ b/tests/test_parsers_powerlogs.py @@ -12,6 +12,7 @@ def test_get_powerlogs(self): result = parse_path(log_root_path) if result: # some files are empty self.assertTrue('sqlite_sequence' in result) + self.assertIsInstance(result['sqlite_sequence'], list) if __name__ == '__main__': diff --git a/utils/misc.py b/utils/misc.py index 83b1cb8..dad43a2 100644 --- a/utils/misc.py +++ b/utils/misc.py @@ -11,6 +11,20 @@ from pathlib import Path +def merge_dicts(a: dict, b: dict) -> dict: + for key, value in b.items(): + if key in a: + if isinstance(value, dict): + a[key] = merge_dicts(a[key], value) + elif isinstance(value, list): + a[key].extend(value) + else: + a[key] = value + else: + a[key] = value + return a + + def get_version(filename="VERSION.txt"): """Read the program version from VERSION.txt""" try: diff --git a/utils/sqlite2json.py b/utils/sqlite2json.py index 740a19d..86ef234 100644 --- a/utils/sqlite2json.py +++ b/utils/sqlite2json.py @@ -19,7 +19,7 @@ # --------------------------------------------------------------------------- # -def sqlite2struct(dbpath): +def sqlite2struct(dbpath) -> dict: """ Transform a SQLite DB to a Python struct. If any exception, return None