-
Notifications
You must be signed in to change notification settings - Fork 745
Expand file tree
/
Copy pathparser.py
More file actions
283 lines (247 loc) · 11.3 KB
/
parser.py
File metadata and controls
283 lines (247 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# Copyright 2018 The dm_control Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Functions for parsing XML into an MJCF object model."""
import io
import os
import sys
import zipfile
from dm_control.mjcf import constants
from dm_control.mjcf import debugging
from dm_control.mjcf import element
from lxml import etree
# Copybara placeholder for internal file handling dependency.
from dm_control.utils import io as resources
def from_xml_string(xml_string, escape_separators=False,
model_dir='', resolve_references=True, assets=None):
"""Parses an XML string into an MJCF object model.
Args:
xml_string: An XML string representing an MJCF model.
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: (optional) Path to the directory containing the model XML file.
This is used to prefix the paths of all asset files.
resolve_references: (optional) A boolean indicating whether the parser
should attempt to resolve reference attributes to a corresponding element.
assets: (optional) A dictionary of pre-loaded assets, of the form
`{filename: bytestring}`. If present, PyMJCF will search for assets in
this dictionary before attempting to load them from the filesystem.
Returns:
An `mjcf.RootElement`.
"""
xml_root = etree.fromstring(xml_string)
return _parse(xml_root, escape_separators,
model_dir=model_dir,
resolve_references=resolve_references,
assets=assets)
def from_file(file_handle, escape_separators=False,
model_dir='', resolve_references=True, assets=None):
"""Parses an XML file into an MJCF object model.
Args:
file_handle: A Python file-like handle.
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: (optional) Path to the directory containing the model XML file.
This is used to prefix the paths of all asset files.
resolve_references: (optional) A boolean indicating whether the parser
should attempt to resolve reference attributes to a corresponding element.
assets: (optional) A dictionary of pre-loaded assets, of the form
`{filename: bytestring}`. If present, PyMJCF will search for assets in
this dictionary before attempting to load them from the filesystem.
Returns:
An `mjcf.RootElement`.
"""
xml_root = etree.parse(file_handle).getroot()
return _parse(xml_root, escape_separators,
model_dir=model_dir,
resolve_references=resolve_references,
assets=assets)
def from_path(path, escape_separators=False, resolve_references=True,
assets=None):
"""Parses an XML file into an MJCF object model.
Args:
path: A path to an XML file. This path should be loadable using
`resources.GetResource`.
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.
resolve_references: (optional) A boolean indicating whether the parser
should attempt to resolve reference attributes to a corresponding element.
assets: (optional) A dictionary of pre-loaded assets, of the form
`{filename: bytestring}`. If present, PyMJCF will search for assets in
this dictionary before attempting to load them from the filesystem.
Returns:
An `mjcf.RootElement`.
"""
model_dir, _ = os.path.split(path)
contents = resources.GetResource(path)
xml_root = etree.fromstring(contents)
return _parse(xml_root, escape_separators,
model_dir=model_dir, resolve_references=resolve_references,
assets=assets)
def from_zip(path, model_file='model.xml', escape_separators=False,
resolve_references=True):
"""Parses a zipped XML file into an MJCF object model.
Args:
path: A path to a zip file containing an MJCF model and its assets.
model_file: If the zip contains multiple XML files, specify the name of the
main model file. Ignored if the zip only contains one XML file.
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.
resolve_references: (optional) A boolean indicating whether the parser
should attempt to resolve reference attributes to a corresponding element.
Returns:
An `mjcf.RootElement`.
Raises:
ValueError: If:
- the path does not point to a zip file
- the zip file contains no XML files
- the zip file contains more than one XML file and none of them have the
name specified in `model_file`.
"""
contents = resources.GetResource(path)
if not zipfile.is_zipfile(io.BytesIO(contents)):
raise ValueError(f'File {path} is not a zip file.')
with zipfile.ZipFile(io.BytesIO(contents), 'r') as zf:
xml_files = [f for f in zf.namelist() if f.endswith('.xml')]
if not xml_files:
raise ValueError(f'No XML file found in {path}.')
elif len(xml_files) > 1:
model_files = [f for f in xml_files if f == model_file]
if not model_files:
raise ValueError(
f'Multiple XML files found in {path}, but none named {model_file}.'
)
xml_path = model_files[0]
else:
xml_path = xml_files[0]
xml_string = zf.read(xml_path)
model_dir = os.path.dirname(xml_path)
assets = {
os.path.relpath(name, model_dir): zf.read(name)
for name in zf.namelist()
if not (name.endswith(os.path.sep) or name == xml_path)
}
xml_root = etree.fromstring(xml_string)
return _parse(xml_root, escape_separators,
resolve_references=resolve_references, assets=assets)
def _parse(xml_root, escape_separators=False,
model_dir='', resolve_references=True, assets=None):
"""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: (optional) Path to the directory containing the model XML file.
This is used to prefix the paths of all asset files.
resolve_references: (optional) A boolean indicating whether the parser
should attempt to resolve reference attributes to a corresponding element.
assets: (optional) A dictionary of pre-loaded assets, of the form
`{filename: bytestring}`. If present, PyMJCF will search for assets in
this dictionary before attempting to load them from the filesystem.
Returns:
An `mjcf.RootElement`.
Raises:
ValueError: If `xml_root`'s tag is not 'mujoco.*'.
"""
assets = assets or {}
if not xml_root.tag.startswith('mujoco'):
raise ValueError('Root element of the XML should be <mujoco.*>: got <{}>'
.format(xml_root.tag))
with debugging.freeze_current_stack_trace():
# Recursively parse any included XML files.
to_include = []
for include_tag in xml_root.findall('include'):
try:
# First look for the path to the included XML file in the assets dict.
path_or_xml_string = assets[include_tag.attrib['file']]
parsing_func = from_xml_string
except KeyError:
# If it's not present in the assets dict then attempt to load the XML
# from the filesystem.
path_or_xml_string = os.path.join(model_dir, include_tag.attrib['file'])
parsing_func = from_path
included_mjcf = parsing_func(
path_or_xml_string,
escape_separators=escape_separators,
resolve_references=resolve_references,
assets=assets)
to_include.append(included_mjcf)
# We must remove <include/> tags before parsing the main XML file, since
# these are a schema violation.
xml_root.remove(include_tag)
# Parse the main XML file.
try:
model = xml_root.attrib.pop('model')
except KeyError:
model = None
mjcf_root = element.RootElement(
model=model, model_dir=model_dir, assets=assets)
_parse_children(xml_root, mjcf_root, escape_separators)
# Merge in the included XML files.
for included_mjcf in to_include:
# The included MJCF might have been automatically assigned a model name
# that conficts with that of `mjcf_root`, so we override it here.
included_mjcf.model = mjcf_root.model
mjcf_root.include_copy(included_mjcf)
if resolve_references:
mjcf_root.resolve_references()
return mjcf_root
def _parse_children(xml_element, mjcf_element, escape_separators=False):
"""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`, any '/' present in the XML causes
a ValueError to be raised.
"""
for xml_child in xml_element:
if xml_child.tag is etree.Comment or xml_child.tag is etree.PI:
continue
try:
child_spec = mjcf_element.spec.children[xml_child.tag]
if escape_separators:
attributes = {}
for name, value in xml_child.attrib.items():
# skip flipping the slash for fields that may contain paths, like
# custom text and asset file.
if name in ['data', 'file', 'meshdir', 'assetdir', 'texturedir',
'content_type', 'fileleft', 'fileright', 'fileback',
'filefront', 'plugin', 'key', 'value']:
attributes[name] = value
else:
new_value = value.replace(
constants.PREFIX_SEPARATOR_ESCAPE,
constants.PREFIX_SEPARATOR_ESCAPE * 2)
new_value = new_value.replace(
constants.PREFIX_SEPARATOR, constants.PREFIX_SEPARATOR_ESCAPE)
attributes[name] = new_value
else:
attributes = dict(xml_child.attrib)
if child_spec.repeated or child_spec.on_demand:
mjcf_child = mjcf_element.add(xml_child.tag, **attributes)
else:
mjcf_child = getattr(mjcf_element, xml_child.tag)
mjcf_child.set_attributes(**attributes)
except: # pylint: disable=bare-except
err_type, err, traceback = sys.exc_info()
raise err_type( # pylint: disable=raise-missing-from
f'Line {xml_child.sourceline}: error while parsing element '
f'<{xml_child.tag}>: {err}').with_traceback(traceback)
_parse_children(xml_child, mjcf_child, escape_separators)