78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
|
import re
|
||
|
from pprint import pprint
|
||
|
|
||
|
def parse_instrument_info(text):
|
||
|
instrument_info = {}
|
||
|
# Regex pattern for key-value pairs in the instrument info section
|
||
|
for line in text.split("\n")[2:]:
|
||
|
if line:
|
||
|
key, value = re.sub(r'\s\s+', '`', line.strip()).split('`')
|
||
|
instrument_info[key.strip()] = value.strip()
|
||
|
return instrument_info
|
||
|
|
||
|
|
||
|
def parse_ports(text):
|
||
|
text = f"{text}\n\n" # ensure extra newline to match on
|
||
|
pattern = r'(Test Port \d)'
|
||
|
matches = re.split(pattern, text)[1:]
|
||
|
m = dict(list(zip(matches[0::2], matches[1::2])))
|
||
|
calibration_data = {}
|
||
|
for port, calibration in m.items():
|
||
|
calibration_data[port] = parse_calibration_data(calibration)
|
||
|
return calibration_data
|
||
|
|
||
|
|
||
|
KEEP = {
|
||
|
"Mass Flow Trans": ("Instrument Flow", "Master Reading"),
|
||
|
"Pressure Transducer" : ("Instrument Pressure", "Master Value")
|
||
|
}
|
||
|
|
||
|
|
||
|
def parse_calibration_data(text):
|
||
|
# Regex pattern for each block in the calibration data section per Port
|
||
|
pattern = r'(Mass Flow Trans|Pressure Transducer)\n(.+?)\n\n'
|
||
|
matches = re.findall(pattern, text, re.DOTALL)
|
||
|
calibration_data = {}
|
||
|
for block_title, block_content in matches:
|
||
|
lines = block_content.strip().split('\n')
|
||
|
lines.pop(0) # Go away =======
|
||
|
device_name = lines.pop(0).strip().split(None, 1)[-1].strip()
|
||
|
device_data = {"name": device_name}
|
||
|
for line in lines:
|
||
|
key, value = re.sub(r'\s\s+', '`', line.strip()).split('`')
|
||
|
# Only keep the fields we want
|
||
|
for start in KEEP[block_title]:
|
||
|
key = key.strip()
|
||
|
if key.startswith(start):
|
||
|
device_data[key] = value.strip()
|
||
|
calibration_data[block_title] = device_data
|
||
|
return calibration_data
|
||
|
|
||
|
def parse_file(file_path):
|
||
|
with open(file_path, 'r') as file:
|
||
|
content = file.read()
|
||
|
|
||
|
# Split the content into instrument info and calibration data sections
|
||
|
info_section, calibration_section = content.split('|| Hardware Calibration Report ||')
|
||
|
|
||
|
instrument_info = parse_instrument_info(info_section)
|
||
|
calibration_data = parse_ports(calibration_section)
|
||
|
|
||
|
return instrument_info, calibration_data
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
file_path = "./hardware_calibration.txt" # Replace with the actual file path
|
||
|
instrument_info, calibration_data = parse_file(file_path)
|
||
|
|
||
|
print("Instrument Info:")
|
||
|
print(instrument_info)
|
||
|
|
||
|
print("\nCalibration Data:")
|
||
|
for device_name, device_data in calibration_data.items():
|
||
|
print(f"\nDevice: {device_name}")
|
||
|
for port, transducers in device_data.items():
|
||
|
print(port)
|
||
|
pprint(transducers)
|
||
|
|
||
|
|