MCPcopy Index your code
hub / github.com/google-deepmind/dm_control / _parse_children

Function _parse_children

dm_control/mjcf/parser.py:240–283  ·  view source on GitHub ↗

Parses all children of a given XML element into an MJCF element. Args: xml_element: The source `etree.Element` object. mjcf_element: The target `mjcf.Element` object. escape_separators: (optional) A boolean, whether to replace '/' characters in element identifiers. If `False`, a

(xml_element, mjcf_element, escape_separators=False)

Source from the content-addressed store, hash-verified

238
239
240def _parse_children(xml_element, mjcf_element, escape_separators=False):
241 """Parses all children of a given XML element into an MJCF element.
242
243 Args:
244 xml_element: The source `etree.Element` object.
245 mjcf_element: The target `mjcf.Element` object.
246 escape_separators: (optional) A boolean, whether to replace '/' characters
247 in element identifiers. If `False`, any '/' present in the XML causes
248 a ValueError to be raised.
249 """
250 for xml_child in xml_element:
251 if xml_child.tag is etree.Comment or xml_child.tag is etree.PI:
252 continue
253 try:
254 child_spec = mjcf_element.spec.children[xml_child.tag]
255 if escape_separators:
256 attributes = {}
257 for name, value in xml_child.attrib.items():
258 # skip flipping the slash for fields that may contain paths, like
259 # custom text and asset file.
260 if name in ['data', 'file', 'meshdir', 'assetdir', 'texturedir',
261 'content_type', 'fileleft', 'fileright', 'fileback',
262 'filefront', 'plugin', 'key', 'value']:
263 attributes[name] = value
264 else:
265 new_value = value.replace(
266 constants.PREFIX_SEPARATOR_ESCAPE,
267 constants.PREFIX_SEPARATOR_ESCAPE * 2)
268 new_value = new_value.replace(
269 constants.PREFIX_SEPARATOR, constants.PREFIX_SEPARATOR_ESCAPE)
270 attributes[name] = new_value
271 else:
272 attributes = dict(xml_child.attrib)
273 if child_spec.repeated or child_spec.on_demand:
274 mjcf_child = mjcf_element.add(xml_child.tag, **attributes)
275 else:
276 mjcf_child = getattr(mjcf_element, xml_child.tag)
277 mjcf_child.set_attributes(**attributes)
278 except: # pylint: disable=bare-except
279 err_type, err, traceback = sys.exc_info()
280 raise err_type( # pylint: disable=raise-missing-from
281 f'Line {xml_child.sourceline}: error while parsing element '
282 f'<{xml_child.tag}>: {err}').with_traceback(traceback)
283 _parse_children(xml_child, mjcf_child, escape_separators)

Callers 1

_parseFunction · 0.85

Calls 4

itemsMethod · 0.80
replaceMethod · 0.80
addMethod · 0.45
set_attributesMethod · 0.45

Tested by

no test coverage detected