import re def format_ui_objects_extended(text): lines = text.strip().split('\n') formatted_lines = [] for line in lines: # Extract the variable name and the content inside the curly braces match = re.match(r'^(.*?)\s*=\s*\{(.*)\}', line.strip()) if match: var_name = match.group(1).strip() content = match.group(2).strip() # Extract individual key-value pairs items = [item.strip() for item in content.split(',') if item.strip()] # Map specific keys for alignment parts = {"container": "", "id": "", "objectName": "", "type": "", "unnamed": ""} for item in items: if '"container"' in item: parts["container"] = item + "," elif '"id"' in item: parts["id"] = item + "," elif '"objectName"' in item: parts["objectName"] = item + "," elif '"type"' in item: parts["type"] = item + "," elif '"unnamed"' in item: parts["unnamed"] = item # Construction with specific column offsets: # 1. '=' at 45 (index 44) line_out = f"{var_name:<44}= {{" # 2. 'container' starts immediately after '{ ' line_out += parts["container"] # 3. 'id' at 105 (index 104) line_out = line_out.ljust(104) + parts["id"] # 4. 'objectName' at 129 (index 128) line_out = line_out.ljust(128) + parts["objectName"] # 5. 'type' at 173 (index 172) line_out = line_out.ljust(172) + parts["type"] # 6. 'unnamed' at 205 (index 204) line_out = line_out.ljust(204) + parts["unnamed"] # 7. '}' at 221 (index 220) line_out = line_out.ljust(220) + "}" formatted_lines.append(line_out) return '\n'.join(formatted_lines) # Input data raw_text = """ o_preTreatmentStack_PreTreatmentSampleStack_PreTreatmentWaterSample = {"container": o_preTreatmentStack_PreTreatmentStack, "objectName": "_PreTreatmentSampleStack", "type": "PreTreatmentWaterSample", "visible": True} o_PreTreatmentSampleStack_failContainer_WaterSampleContainer = {"container": o_preTreatmentStack_PreTreatmentSampleStack_PreTreatmentWaterSample, "id": "_failContainer", "type": "WaterSampleContainer", "unnamed": 1, "visible": True} o_arterialWindow_LabelUnitValueAdjuster = {"container": o_Overlay, "objectName": "_arterialWindow", "type": "LabelUnitValueAdjuster", "visible": True} o_venousWindow_LabelUnitValueAdjuster = {"container": o_Overlay, "objectName": "_venousWindow", "type": "LabelUnitValueAdjuster", "visible": True} o_venousAsymmetricWindow_LabelUnitValueAdjuster = {"container": o_Overlay, "objectName": "_venousAsymmetricWindow", "type": "LabelUnitValueAdjuster", "visible": True} o_tmpWindow_LabelUnitValueAdjuster = {"container": o_Overlay, "objectName": "_tmpWindow", "type": "LabelUnitValueAdjuster", "visible": True} o_PreTreatmentSampleStack_button_TouchRect = {"container": o_preTreatmentStack_PreTreatmentSampleStack_PreTreatmentWaterSample, "id": "_button", "occurrence": 2, "type": "TouchRect", "unnamed": 1, "visible": True} o_preTreatmentStack_repeater_Repeater = {"container": o_preTreatmentStack_PreTreatmentStack, "id": "repeater", "type": "Repeater", "unnamed": 1, "visible": True} o_preTreatmentStack_gridSteps_Grid = {"container": o_preTreatmentStack_PreTreatmentStack, "id": "_gridSteps", "type": "Grid", "unnamed": 1, "visible": True} o_preTreatmentStack_gridSteps_Grid_2 = {"container": o_preTreatmentStack_PreTreatmentStack, "id": "_gridSteps", "occurrence": 2, "type": "Grid", "unnamed": 1, "visible": True} o_PreTreatmentCreate_bpMeasurementIntervalControl_ValueAdjusterCustom = {"container": o_preTreatmentStack_PreTreatmentCreate_PreTreatmentCreate, "id": "_bpMeasurementIntervalControl", "type": "ValueAdjusterCustom", "unnamed": 1} o_preTreatmentStack_Validate_Text = {"container": o_preTreatmentStack_PreTreatmentStack, "text": "Validate", "type": "Text", "unnamed": 1, "visible": True} o_notification_bar ={"container": o_Overlay, "objectName": "NotificationBar", "type": "NotificationBarSmall"} o_treatmentHome_editButton_IconButton = {"container": mainTreatmentScreen, "id": "_editButton", "type": "IconButton", "unnamed": 1 } o_treatmentHome_treatmentPressures_TreatmentPressures = {"container": mainTreatmentScreen, "objectName": "treatmentPressures", "type": "TreatmentPressures", "visible": True} o_valueAdjuster_ValueAdjuster = {"container": o_Overlay, "objectName": "_valueAdjuster", "type": "ValueAdjuster", "visible": True} o_treatmentHome_editButton_IconButton_2 = {"container": mainTreatmentScreen, "id": "_editButton", "type": "IconButton", "unnamed": 1 } o_treatmentHome_treatmentFlows_TreatmentFlows = {"container": mainTreatmentScreen, "objectName": "treatmentFlows", "type": "TreatmentFlows", "visible": True} o_dialysateFlowRate_LabelUnitValueAdjuster = {"container": o_Overlay, "objectName": "_dialysateFlowRate", "type": "LabelUnitValueAdjuster", "visible": True} o_bloodFlowRate_LabelUnitValueAdjuster = {"container": o_Overlay, "objectName": "_bloodFlowRate", "type": "LabelUnitValueAdjuster", "visible": True} o_dialysateTemperature_LabelUnitValueAdjuster = {"container": o_Overlay, "objectName": "_dialysateTemperature", "type": "LabelUnitValueAdjuster", "visible": True} o_treatmentHome_upArrowIcon_ArrowButton = {"container": mainTreatmentScreen, "objectName": "upArrowIcon", "type": "ArrowButton", "visible": True} o_treatmentHome_downArrowIcon_ArrowButton = {"container": mainTreatmentScreen, "objectName": "downArrowIcon", "type": "ArrowButton", "visible": True} o_treatmentHome_notification_NotificationBarSmall = {"container": mainTreatmentScreen, "objectName": "_notification", "type": "NotificationBarSmall"} o_ufVolumeRemoved_LabelUnitText = {"container": o_Overlay, "objectName": "_ufVolumeRemoved", "type": "LabelUnitText", "visible": True} o_ufVolumeGoal_LabelUnitText = {"container": o_Overlay, "objectName": "_ufVolumeGoal", "type": "LabelUnitText", "visible": True} o_timeElapsed_LabelUnitText = {"container": o_Overlay, "objectName": "_timeElapsed", "type": "LabelUnitText", "visible": True} o_newTreatmentDuration_LabelUnitValueAdjuster = {"container": o_Overlay, "objectName": "_newTreatmentDuration", "type": "LabelUnitValueAdjuster", "visible": True} o_preTreatmentStack_confirmButton_ConfirmButton = {"container": o_preTreatmentStack_PreTreatmentStack, "objectName": "_confirmButton", "type": "ConfirmButton", "visible": True} o_preTreatmentStack_titleBar_StepNavigationTitleBar = {"container": o_preTreatmentStack_PreTreatmentStack, "id": "_titleBar", "type": "StepNavigationTitleBar", "unnamed": 1} o_preTreatmentStack_mouseArea_MouseArea = {"container": o_preTreatmentStack_PreTreatmentStack, "id": "_mouseArea", "occurrence": 2, "type": "MouseArea", "unnamed": 1, "visible": True} o_ufRate_LabelUnitText = {"container": o_Overlay, "objectName": "_ufRate", "type": "LabelUnitText", "visible": True} o_newTreatmentDuration_LabelUnitText = {"container": o_Overlay, "objectName": "_newTreatmentDuration", "type": "LabelUnitText", "visible": True} # o_treatmentHome_upArrowIcon_ArrowButton_2 = {"container": mainTreatmentScreen, "objectName": "upArrowIcon", "occurrence": 2, "type": "ArrowButton", "visible": True} """ print(format_ui_objects_extended(raw_text))