Please explain this code to me

Good day,

I am using code from a hardware vendor (it is public domain). All this code does is to extract data from mf4 files and upload it to influxdb. It used to work like a charm until we experienced a number of power failures in succession (Ubuntu 21). All the hard drives got busted together with the UPS’s (and other things). I got new systems now Debian 12 (which I prefer).

The code can be found here link together with sample data and library requirements.

However, now when I run the same code I get the following output:

File "main.py", line 18, in <module>
    df_raw, device_id = proc.get_raw_data(log_file, inp.pw)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "utils.py", line 191, in get_raw_data
    device_id = self.get_device_id(mdf_file)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "utils.py", line 204, in get_device_id
    return mdf_file.get_metadata()["HDComment.Device Information.serial number"]["value_raw"]
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'HDComment.Device Information.serial number'

I have no idea what I am doing (not a python expert).

I’m not familiar with MF4 personally, but can partially explain the error you are seeing.

It looks like mdf_file.get_metadata() was returning a key/value data structure, probably a Python dictionary. It then tries to look up the value for the key HDComment.Device Information.serial number , but it isn’t present. The code expects the metadata to have a particular structure by requiring that key to be there. If you’re familiar with JSON, it is basically expecting this:

{
    "HDComment.Device Information.serial number": {
        "value_raw": <something>
    }
}

Do you know what file it is trying to read from, and is it possible to inspect manually? Do you have any examples of working files for comparison?

Sometimes there is a quick fix for problems like this.

As @flyinghyrax said, the problem (or at least the reason it crashes) is that the mdf_file metadata has a different structure from expected.

If you can insert a line before

return mdf_file.get_metadata()["HDComment.Device Information.serial number"]["value_raw"]

as

print(mdf_file.get_metadata())

that should print the actual value of the metadata, and therefore also its structure, just before the program raises the error.

If the serial number is simply stored behind a different combination of keys, you can patch it by replacing ["HDComment.Device Information.serial number"]["value_raw"] with the correct keys.

2 Likes