deepakganesh78 / valluvar_kotam_3d_model

0 stars 0 forks source link

test 16 #16

Open deepakganesh78 opened 1 day ago

deepakganesh78 commented 1 day ago

import xml.etree.ElementTree as ET from datetime import datetime

class DummyRecord: def init(self, data): self.data = data

def __repr__(self):
    return f"DummyRecord({self.data})"

Function to read and parse the input file

def read_input_file(file_path): records = [] with open(file_path, 'r') as file: headers = file.readline().strip().split(';') # Read headers print(f"Headers: {headers}") # Debug: Print headers for line in file:

Strip whitespace and split by delimiter

        data = line.strip().split(';')
        print(f"Line Data: {data}")  # Debug: Print each line's data
        if len(data) == len(headers):  # Ensure data matches headers
            record_data = dict(zip(headers, data))
            record = DummyRecord(record_data)
            records.append(record)
        else:
            print(f"Skipping incomplete line: {line}")  # Debug: Handle incomplete lines
return records

Function to parse Type Tree XML and extract constraints

def parse_type_tree(type_tree_file): type_tree = ET.parse(type_tree_file).getroot() constraints = {} for item in type_tree.findall('ITEM'): name = item.get('SimpleTypeName') specifics = item.find('*') max_size = specifics.find('Size').get('Max') if specifics is not None and specifics.find('Size') is not None else None date_format = specifics.find('CharDatetimeFormatString').text.strip('()') if specifics is not None and specifics.find('CharDatetimeFormatString') is not None else None constraints[name] = {'max_size': int(max_size) if max_size else None, 'date_format': date_format} return constraints

Function to validate date format strictly

def validate_date_format(date_str, date_format): format_map = { 'CCYYMMDD': '%Y%m%d', 'YYMMDD': '%y%m%d', 'MMDDCCYY': '%m%d%Y', 'MMDDYY': '%m%d%y', 'CCYYDDD': '%Y%j', 'YYDDD': '%y%j', 'DDMMCCYY': '%d%m%Y', 'DDMMYY': '%d%m%y', 'DD/MM/CCYY': '%d/%m/%Y', 'MM-DD-YYYY': '%m-%d-%Y'

Add more custom formats as needed

}

try:
    if date_format in format_map:
        datetime.strptime(date_str, format_map[date_format])
    else:
        print(f"Unsupported date format: {date_format}")
        return False
    return True
except ValueError:
    return False

Function to validate data based on Type Tree constraints

def validate_data(records, constraints): validation_passed = True for record in records: for key, value in record.data.items(): if key in constraints: if constraints[key]['max_size'] and len(value) > constraints[key]['max_size']: print(f"Validation Error: '{key}' '{value}' exceeds max length of {constraints[key]['max_size']}") validation_passed = False if constraints[key]['date_format']: if not validate_date_format(value, constraints[key]['date_format']): print(f"Validation Error: '{key}' '{value}' does not match the format {constraints[key]['date_format']}") validation_passed = False return validation_passed

Function to create XML elements from records

def create_xml(records): root = ET.Element("Records") for record in records: record_elem = ET.SubElement(root, "Record") for key, value in record.data.items(): elem = ET.SubElement(record_elem, key) elem.text = value return root

Function to save XML to a file

def save_xml(root, output_file): tree = ET.ElementTree(root) tree.write(output_file, encoding="utf-8", xml_declaration=True)

Path to your input file

input_file_path = 'input_file.txt'

Path to your type tree XML

type_tree_file_path = 'dummy_typetree.xml'

Path to output XML file

output_file_path = 'output.xml'

Read and process the input file

dummy_records = read_input_file(input_file_path) print(f"Dummy Records: {dummy_records}") # Debug: Print the dummy records

Parse the type tree XML for validation

constraints = parse_type_tree(type_tree_file_path) print(f"Constraints: {constraints}") # Debug: Print the constraints

Validate the data

if validate_data(dummy_records, constraints): print("Data validation passed.")

# Create XML from records
xml_root = create_xml(dummy_records)
print(f"XML Root: {ET.tostring(xml_root)}")  # Debug: Print the XML root for verification

# Save XML to file
save_xml(xml_root, output_file_path)
print(f"XML saved to {output_file_path}")

else: print("Data validation failed.")