def untldict2py(untl_dict):
    """Convert a UNTL dictionary into a Python object."""
    # Create the root element.
    untl_root = PYUNTL_DISPATCH['metadata']()
    untl_py_list = []
    for element_name, element_list in untl_dict.items():
        # Loop through the element dictionaries in the element list.
        for element_dict in element_list:
            qualifier = element_dict.get('qualifier', None)
            content = element_dict.get('content', None)
            child_list = []
            # Handle content that is children elements.
            if isinstance(content, dict):
                for key, value in content.items():
                    child_list.append(
                        PYUNTL_DISPATCH[key](content=value),
                    )
                # Create the UNTL element that will have children elements
                # added to it.
                if qualifier is not None:
                    untl_element = PYUNTL_DISPATCH[element_name](
                        qualifier=qualifier
                    )
                else:
                    untl_element = PYUNTL_DISPATCH[element_name]()
                # Add the element's children to the element.
                for child in child_list:
                    untl_element.add_child(child)
            # If not child element, create the element and
            # add qualifier and content as available.
            elif content is not None and qualifier is not None:
                untl_element = PYUNTL_DISPATCH[element_name](
                    qualifier=qualifier,
                    content=content,
                )
            elif qualifier is not None:
                untl_element = PYUNTL_DISPATCH[element_name](
                    qualifier=qualifier,
                )
            elif content is not None:
                untl_element = PYUNTL_DISPATCH[element_name](
                    content=content,
                )
            # Create element that only has children.
            elif len(child_list) > 0:
                untl_element = PYUNTL_DISPATCH[element_name]()
            # Add the UNTL element to the Python element list.
            untl_py_list.append(untl_element)
    # Add the UNTL elements to the root element.
    for untl_element in untl_py_list:
        untl_root.add_child(untl_element)
    return untl_root
def untldict2py ( untl_dict ) : untl_root = PYUNTL_DISPATCH [ 'metadata' ] ( ) untl_py_list = [ ] for element_name , element_list in untl_dict . items ( ) : for element_dict in element_list : qualifier = element_dict . get ( 'qualifier' , None ) content = element_dict . get ( 'content' , None ) child_list = [ ] if isinstance ( content , dict ) : for key , value in content . items ( ) : child_list . append ( PYUNTL_DISPATCH [ key ] ( content = value ) , ) if qualifier is not None : untl_element = PYUNTL_DISPATCH [ element_name ] ( qualifier = qualifier ) else : untl_element = PYUNTL_DISPATCH [ element_name ] ( ) for child in child_list : untl_element . add_child ( child ) elif content is not None and qualifier is not None : untl_element = PYUNTL_DISPATCH [ element_name ] ( qualifier = qualifier , content = content , ) elif qualifier is not None : untl_element = PYUNTL_DISPATCH [ element_name ] ( qualifier = qualifier , ) elif content is not None : untl_element = PYUNTL_DISPATCH [ element_name ] ( content = content , ) elif len ( child_list ) > 0 : untl_element = PYUNTL_DISPATCH [ element_name ] ( ) untl_py_list . append ( untl_element ) for untl_element in untl_py_list : untl_root . add_child ( untl_element ) return untl_root
Convert a UNTL dictionary into a Python object.