Parses a complete MJCF model from an XML. Args: xml_root: An `etree.Element` object. escape_separators: (optional) A boolean, whether to replace '/' characters in element identifiers. If `False`, any '/' present in the XML causes a ValueError to be raised. model_dir: (opti
(xml_root, escape_separators=False,
model_dir='', resolve_references=True, assets=None)
| 164 | |
| 165 | |
| 166 | def _parse(xml_root, escape_separators=False, |
| 167 | model_dir='', resolve_references=True, assets=None): |
| 168 | """Parses a complete MJCF model from an XML. |
| 169 | |
| 170 | Args: |
| 171 | xml_root: An `etree.Element` object. |
| 172 | escape_separators: (optional) A boolean, whether to replace '/' characters |
| 173 | in element identifiers. If `False`, any '/' present in the XML causes |
| 174 | a ValueError to be raised. |
| 175 | model_dir: (optional) Path to the directory containing the model XML file. |
| 176 | This is used to prefix the paths of all asset files. |
| 177 | resolve_references: (optional) A boolean indicating whether the parser |
| 178 | should attempt to resolve reference attributes to a corresponding element. |
| 179 | assets: (optional) A dictionary of pre-loaded assets, of the form |
| 180 | `{filename: bytestring}`. If present, PyMJCF will search for assets in |
| 181 | this dictionary before attempting to load them from the filesystem. |
| 182 | |
| 183 | Returns: |
| 184 | An `mjcf.RootElement`. |
| 185 | |
| 186 | Raises: |
| 187 | ValueError: If `xml_root`'s tag is not 'mujoco.*'. |
| 188 | """ |
| 189 | |
| 190 | assets = assets or {} |
| 191 | |
| 192 | if not xml_root.tag.startswith('mujoco'): |
| 193 | raise ValueError('Root element of the XML should be <mujoco.*>: got <{}>' |
| 194 | .format(xml_root.tag)) |
| 195 | |
| 196 | with debugging.freeze_current_stack_trace(): |
| 197 | # Recursively parse any included XML files. |
| 198 | to_include = [] |
| 199 | for include_tag in xml_root.findall('include'): |
| 200 | try: |
| 201 | # First look for the path to the included XML file in the assets dict. |
| 202 | path_or_xml_string = assets[include_tag.attrib['file']] |
| 203 | parsing_func = from_xml_string |
| 204 | except KeyError: |
| 205 | # If it's not present in the assets dict then attempt to load the XML |
| 206 | # from the filesystem. |
| 207 | path_or_xml_string = os.path.join(model_dir, include_tag.attrib['file']) |
| 208 | parsing_func = from_path |
| 209 | included_mjcf = parsing_func( |
| 210 | path_or_xml_string, |
| 211 | escape_separators=escape_separators, |
| 212 | resolve_references=resolve_references, |
| 213 | assets=assets) |
| 214 | to_include.append(included_mjcf) |
| 215 | # We must remove <include/> tags before parsing the main XML file, since |
| 216 | # these are a schema violation. |
| 217 | xml_root.remove(include_tag) |
| 218 | |
| 219 | # Parse the main XML file. |
| 220 | try: |
| 221 | model = xml_root.attrib.pop('model') |
| 222 | except KeyError: |
| 223 | model = None |
no test coverage detected