-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathxmlformat.py
More file actions
407 lines (318 loc) · 11.4 KB
/
Copy pathxmlformat.py
File metadata and controls
407 lines (318 loc) · 11.4 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# -*- coding: utf-8 -*-
"""
XML
Basic implementation for an XML importer.
"""
import re
from io import BytesIO
from mathics.builtin.files_io.files import MathicsOpen
from mathics.core.atoms import String
from mathics.core.builtin import Builtin, MessageException
from mathics.core.convert.expression import to_expression, to_mathics_list
from mathics.core.convert.python import from_python
from mathics.core.expression import Evaluation, Expression
from mathics.core.symbols import Symbol
from mathics.core.systemsymbols import SymbolFailed
# use lxml, if available, as it has some additional features such as parsing XML
# versions, comments and cdata. fallback on python builtin xml parser otherwise.
try:
from lxml import etree as ET
lxml_available = True
except ImportError:
import xml.etree.ElementTree as ET
lxml_available = False
def xml_cdata(node):
if lxml_available:
return String("\n".join(node.itertext()))
def xml_comments(node):
if lxml_available:
return to_expression(
"List", *[String(s.text) for s in node.xpath("//comment()")]
)
_namespace_key = to_mathics_list(
"http://www.w3.org/2000/xmlns/", "xmlns", elements_conversion_fn=String
)
def node_to_xml_element(node, parent_namespace=None, strip_whitespace=True):
if lxml_available:
if isinstance(node, ET._Comment):
items = [
Expression(
to_expression("XMLObject", String("Comment")), String(node.text)
)
]
if node.tail is not None:
items.append(String(node.tail))
return items
# see https://reference.wolfram.com/language/XML/tutorial/RepresentingXML.html
default_namespace = node.get("xmlns")
if default_namespace is None:
default_namespace = parent_namespace
if lxml_available:
tag = ET.QName(node.tag)
localname = tag.localname
namespace = tag.namespace
else:
tag = node.tag
if not tag.startswith("{"):
namespace = None
localname = tag
else:
m = re.match(r"\{(.*)\}(.*)", node.tag)
namespace = m.group(1)
localname = m.group(2)
def children():
text = node.text
if text:
if strip_whitespace:
text = text.strip()
if text:
yield String(text)
for child in node:
for element in node_to_xml_element(
child, default_namespace, strip_whitespace
):
yield element
tail = node.tail
if tail:
if strip_whitespace:
tail = tail.strip()
if tail:
yield String(tail)
def attributes():
for name, value in node.attrib.items():
if name == "xmlns":
name = _namespace_key
else:
name = String(name)
yield to_expression("Rule", name, from_python(value))
if namespace is None or namespace == default_namespace:
name = String(localname)
else:
name = to_mathics_list(String(namespace), String(localname))
return [
to_expression(
"XMLElement",
name,
to_mathics_list(*list(attributes())),
to_mathics_list(*list(children())),
)
]
def xml_object(root):
if lxml_available:
tree = root.getroottree()
declaration = [
Expression(
to_expression("XMLObject", String("Declaration")),
to_expression(
"Rule", String("Version"), String(tree.docinfo.xml_version)
),
to_expression(
"Rule", String("Encoding"), String(tree.docinfo.encoding)
),
)
]
else:
declaration = []
return Expression(
to_expression("XMLObject", String("Document")),
to_mathics_list(*declaration),
*node_to_xml_element(root),
)
class ParseError(Exception):
pass
def parse_xml_stream(f):
def parse(iter): # inspired by http://effbot.org/zone/element-namespaces.htm
root = None
namespace = None
for event, elem in iter:
if event == "start-ns":
if not elem[0]: # setting default namespace?
namespace = elem[1]
elif event == "start":
if root is None:
root = elem
if namespace:
elem.set("xmlns", namespace)
namespace = None
return root
if lxml_available:
iterparse = ET.iterparse(
f,
("start", "start-ns"),
remove_comments=False,
strip_cdata=False,
recover=False,
)
try:
return parse(iterparse)
except ET.XMLSyntaxError as e:
# note: iterparse.error_log, e.g. iterparse.error_log[-1].type_name contains the exact error code. this
# might be useful for further error handling in the future.
msg = str(e)
# Different versions of lxml include different suffixes so trim.
m = re.search(r"line \d+, column \d+", msg)
if m is not None:
msg = msg[: m.end()]
raise ParseError(msg)
else:
try:
return parse(ET.iterparse(f, ("start", "start-ns")))
except ET.ParseError as e:
if e.code == 9: # XML_ERROR_JUNK_AFTER_DOC_ELEMENT
# for this specific error, we produce the error exactly as lxml would. useful for use in test cases.
line, column = e.position
raise ParseError(
"Extra content at the end of the document, line %d, column %d"
% (line, column + 1)
)
else:
raise ParseError(str(e))
def parse_xml_file(filename):
with MathicsOpen(filename, "rb") as f:
root = parse_xml_stream(f)
return root
def parse_xml(parse, text, evaluation: Evaluation):
try:
return parse(text.get_string_value())
except ParseError as e:
evaluation.message("XML`Parser`XMLGet", "prserr", str(e))
return SymbolFailed
except IOError:
evaluation.message("General", "noopen", text.get_string_value())
return SymbolFailed
except MessageException as e:
e.message(evaluation)
return SymbolFailed
class XMLObject(Builtin):
"""
<url>:
WMA link:
https://reference.wolfram.com/language/ref/XMLObject.html</url>
<dl>
<dt>'XMLObject["type"]'
<dd> represents the head of an XML object in symbolic XML.
</dl>
"""
summary_text = "the head of an xml object"
class XMLElement(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/XMLElement.html</url>
<dl>
<dt>'XMLElement'[$tag$, {$attr_1$, $val_1$, ...}, {$data$, ...}]
<dd> represents an element in symbolic XML.
</dl>
"""
summary_text = "an xml element"
class _Get(Builtin):
context = "XML`Parser`"
messages = {
"prserr": "``.",
}
def eval(self, text, evaluation: Evaluation):
"""%(name)s[text_String]"""
root = parse_xml(self._parse, text, evaluation)
if isinstance(root, Symbol): # $Failed?
return root
else:
return xml_object(root)
class XMLGet(_Get):
"""
## <url>:native internal:</url>
<dl>
<dt>'XMLGet[...]'
<dd> Internal. Document me.
</dl>
"""
summary_text = ""
def _parse(self, text):
return parse_xml_file(text)
class XMLGetString(_Get):
"""
## <url>:native internal:</url>
<dl>
<dt>'XML`Parser`XMLGetString["string"]'
<dd>parses "string" as XML code, and returns an XMLObject.
</dl>
>> Head[XML`Parser`XMLGetString["<a></a>"]]
= XMLObject[Document]
#> XML`Parser`XMLGetString["<a></a>xyz"]
= $Failed
: Extra content at the end of the document, line 1, column 8.
"""
summary_text = "parse a xml object"
def _parse(self, text):
with BytesIO() as f:
f.write(text.encode("utf8"))
f.seek(0)
return parse_xml_stream(f)
class PlaintextImport(Builtin):
"""
<url>
:WMA link:
https://reference.wolfram.com/language/ref/PlaintextImport.html</url>
<dl>
<dt>'XML`PlaintextImport["string"]'
<dd>parses "string" as XML code, and returns it as plain text.
</dl>
>> StringReplace[StringTake[Import["ExampleData/InventionNo1.xml", "Plaintext"],31],FromCharacterCode[10]->"/"]
= MuseScore 1.2/2012-09-12/5.7/40
"""
summary_text = "import plain text from xml"
context = "XML`"
def eval(self, text, evaluation: Evaluation):
"""%(name)s[text_String]"""
root = parse_xml(parse_xml_file, text, evaluation)
if isinstance(root, Symbol): # $Failed?
return root
def lines():
for line in root.itertext():
s = line.strip()
if s:
yield s
plaintext = String("\n".join(lines()))
return to_mathics_list(to_expression("Rule", "Plaintext", plaintext))
class TagsImport(Builtin):
"""
## <url>:native internal:</url>
<dl>
<dt>'XML`TagsImport["string"]'
<dd>parses "string" as XML code, and returns a list with the tags found.
</dl>
>> Take[Import["ExampleData/InventionNo1.xml", "Tags"], 10]
= {accidental, alter, arpeggiate, articulations, attributes, backup, bar-style, barline, beam, beat-type}
"""
summary_text = "import tags text from xml"
context = "XML`"
@staticmethod
def _tags(root):
tags = set()
def gather(node):
tags.add(node.tag)
for child in node:
gather(child)
gather(root)
return to_mathics_list(*[String(tag) for tag in sorted(list(tags))])
def eval(self, text, evaluation: Evaluation):
"""%(name)s[text_String]"""
root = parse_xml(parse_xml_file, text, evaluation)
if isinstance(root, Symbol): # $Failed?
return root
return to_mathics_list(to_expression("Rule", "Tags", self._tags(root)))
class XMLObjectImport(Builtin):
"""
## <url>:native internal:</url>
<dl>
<dt>'XML`XMLObjectImport["string"]'
<dd>parses "string" as XML code, and returns a list of XMLObjects found.
</dl>
>> Part[Import["ExampleData/InventionNo1.xml", "XMLObject"], 2, 3, 1]
= XMLElement[identification, {}, {XMLElement[encoding, {}, {XMLElement[software, {}, {MuseScore 1.2}], XMLElement[encoding-date, {}, {2012-09-12}]}]}]
>> Part[Import["ExampleData/Namespaces.xml"], 2]
= XMLElement[book, {{http://www.w3.org/2000/xmlns/, xmlns} ⇾ urn:loc.gov:books}, {XMLElement[title, {}, {Cheaper by the Dozen}], XMLElement[{urn:ISBN:0-395-36341-6, number}, {}, {1568491379}], XMLElement[notes, {}, {XMLElement[p, {{http://www.w3.org/2000/xmlns/, xmlns} ⇾ http://www.w3.org/1999/xhtml}, {This is a, XMLElement[i, {}, {funny, book!}]}]}]}]
"""
summary_text = "import elements from xml"
context = "XML`"
def eval(self, text, evaluation: Evaluation):
"""%(name)s[text_String]"""
xml = to_expression("XML`Parser`XMLGet", text).evaluate(evaluation)
return to_mathics_list(to_expression("Rule", "XMLObject", xml))