def extract_includes()

in mujoco_worldgen/parser/parser.py [0:0]


def extract_includes(xml_dict, root_xml_path, enforce_validation=True):
    '''
    extracts "include" xmls and substitutes them.
    '''
    def transform_include(node):
        if "include" in node:
            if isinstance(node["include"], OrderedDict):
                node["include"] = [node["include"]]
            include_xmls = []
            for include_dict in node["include"]:
                include_path = include_dict["@file"]
                if not exists(include_path):
                    include_path = join(dirname(abspath(root_xml_path)), include_path)
                assert exists(include_path), "Cannot include file: %s" % include_path
                with open(include_path) as f:
                    include_string = f.read()
                include_xml = xmltodict.parse(include_string.strip())
                closure_transform(transform_include)(include_xml)
                assert "mujocoinclude" in include_xml, "Missing <mujocoinclude>."
                include_xmls.append(include_xml["mujocoinclude"])
            del node["include"]
            for include_xml in include_xmls:
                preprocess(include_xml, root_xml_path, enforce_validation=enforce_validation)
                update_mujoco_dict(node, include_xml)
    closure_transform(transform_include)(xml_dict)