2024-04-21 05:22:36 -03:00
|
|
|
import re
|
|
|
|
import json
|
|
|
|
import pandas as pd
|
2024-04-22 04:21:25 -03:00
|
|
|
import pprint
|
|
|
|
from copy import deepcopy
|
|
|
|
|
|
|
|
|
|
|
|
def debug_print_db_struct(db_struct):
|
|
|
|
pprint.pprint(
|
|
|
|
db_struct, width=80, indent=2
|
|
|
|
) # Ajusta el parámetro 'width' según necesidad
|
|
|
|
|
2024-04-21 05:22:36 -03:00
|
|
|
|
|
|
|
def expand_udt_references(db_struct, udts):
|
|
|
|
"""
|
|
|
|
Recursively expand UDT references in the given DB structure using the UDT definitions.
|
|
|
|
This function specifically expands fields designated as 'type' which reference UDTs.
|
|
|
|
"""
|
|
|
|
if isinstance(db_struct, dict):
|
|
|
|
for key, value in list(db_struct.items()):
|
|
|
|
if isinstance(value, dict):
|
|
|
|
# Recurse into dictionaries
|
|
|
|
expand_udt_references(value, udts)
|
|
|
|
elif isinstance(value, str) and key == "type": # Only expand 'type' fields
|
|
|
|
type_name = value.strip(
|
|
|
|
'"'
|
|
|
|
) # Remove quotes which may wrap UDT names with spaces
|
|
|
|
if type_name in udts:
|
2024-04-22 04:21:25 -03:00
|
|
|
# Replace the UDT reference with its deeply copied definition
|
2024-04-22 17:13:43 -03:00
|
|
|
db_struct["is_udt_definition"] = True
|
2024-04-22 04:21:25 -03:00
|
|
|
db_struct["fields"] = deepcopy(udts[type_name])
|
2024-04-22 17:13:43 -03:00
|
|
|
|
2024-04-22 04:21:25 -03:00
|
|
|
print(f"Expanded UDT '{type_name}' at field '{key}'")
|
2024-04-21 05:22:36 -03:00
|
|
|
elif isinstance(db_struct, list):
|
|
|
|
for item in db_struct:
|
|
|
|
expand_udt_references(item, udts)
|
|
|
|
|
|
|
|
|
|
|
|
def handle_array_types(db_struct):
|
|
|
|
"""
|
2024-04-21 14:55:11 -03:00
|
|
|
Handle array types to expand them into multiple fields as sub-elements while preserving comments.
|
|
|
|
Modifies the structure in place by expanding array definitions within their original field.
|
2024-04-21 05:22:36 -03:00
|
|
|
"""
|
|
|
|
if isinstance(db_struct, dict):
|
|
|
|
for key, value in list(db_struct.items()):
|
|
|
|
if isinstance(value, dict):
|
2024-04-21 14:55:11 -03:00
|
|
|
# Recursively process nested dictionaries
|
2024-04-21 05:22:36 -03:00
|
|
|
handle_array_types(value)
|
2024-04-21 14:55:11 -03:00
|
|
|
|
2024-04-22 04:21:25 -03:00
|
|
|
if isinstance(value, dict) and "type" in value:
|
|
|
|
match = re.match(r"Array\[(\d+)\.\.(\d+)\] of (\w+)", value["type"])
|
2024-04-21 05:22:36 -03:00
|
|
|
if match:
|
2024-04-22 04:21:25 -03:00
|
|
|
lower_bound, upper_bound, base_type = (
|
|
|
|
int(match.group(1)),
|
|
|
|
int(match.group(2)),
|
|
|
|
match.group(3),
|
|
|
|
)
|
|
|
|
comment = value.get("comment", "")
|
2024-04-22 17:13:43 -03:00
|
|
|
value["array_definition"] = True
|
2024-04-21 14:55:11 -03:00
|
|
|
# Instead of popping the original key, initialize a sub-dictionary
|
2024-04-22 04:21:25 -03:00
|
|
|
value["Array"] = (
|
|
|
|
{}
|
|
|
|
) # Initialize a sub-dictionary for array elements
|
2024-04-21 14:55:11 -03:00
|
|
|
|
2024-04-21 05:22:36 -03:00
|
|
|
for i in range(lower_bound, upper_bound + 1):
|
2024-04-21 14:55:11 -03:00
|
|
|
element_key = f"[{i}]"
|
2024-04-22 04:21:25 -03:00
|
|
|
value["Array"][element_key] = {
|
|
|
|
"type": base_type,
|
|
|
|
"comment": comment,
|
|
|
|
"is_array_element": True,
|
2024-04-21 14:55:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Optionally, modify or remove the original type designation if necessary
|
|
|
|
# value.pop('type', None) # Uncomment this if you want to remove the 'type' from the original
|
|
|
|
|
|
|
|
# Continue the recursive handling if it's not an array definition
|
|
|
|
elif isinstance(value, dict):
|
|
|
|
handle_array_types(value)
|
|
|
|
|
|
|
|
|
2024-04-21 05:22:36 -03:00
|
|
|
|
|
|
|
def expand_dbs(udts, dbs):
|
|
|
|
"""
|
|
|
|
Expand all UDT references in all DBs and then handle array types.
|
|
|
|
"""
|
|
|
|
for db_name, db_content in dbs.items():
|
|
|
|
print(f"Expanding DB: {db_name}")
|
|
|
|
expand_udt_references(db_content, udts)
|
|
|
|
handle_array_types(db_content)
|
|
|
|
print(f"Completed expansion for DB: {db_name}")
|