Skip to content
Snippets Groups Projects
Commit 217397e7 authored by Ashwin Kumar Karnad's avatar Ashwin Kumar Karnad
Browse files

first implementation of read_metadata_from_logfile

parent 332d0118
No related branches found
No related tags found
1 merge request!28improve machine readable logging
......@@ -11,6 +11,7 @@ import sys
import time
from pathlib import Path
from typing import List, Tuple, Union
import re
# If 'rich' is available ("pip install rich" or "apt-get install python3-rich"),
# then use coloured output, otherwise proceed as before
......@@ -62,12 +63,13 @@ def log_metadata(key: str, value: str) -> None:
f"{config_vars['metadata_tag_open']}{key}:{value}{config_vars['metadata_tag_close']}"
)
def read_metadata_from_logfile(logfile: Union[str, Path]) -> dict:
"""Read metadata from the log file.
This function reads metadata from the log file. The metadata is
enclosed in a tag, so that it can be easily found in the log file.
Parameters
----------
logfile : str or Path
......@@ -75,6 +77,18 @@ def read_metadata_from_logfile(logfile: Union[str, Path]) -> dict:
returns : dict
dictionary containing the metadata
"""
with open(logfile, "r") as f:
log_text = f.read()
# check for all data that matches the regex
# metadata_tag_open {key}:{value} metadata_tag_close
# and return a dictionary with all the matches
return {
match.group(1): match.group(2)
for match in re.finditer(
f"{config_vars['metadata_tag_open']}(\w+):(\w+){config_vars['metadata_tag_close']}",
log_text,
)
}
def set_up_logging(loglevel="warning", filename=None):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment