|
12 | 12 | XMLOID: etree.ElementTree, |
13 | 13 | } |
14 | 14 |
|
15 | | -def xml_pack(xml, tostr = etree.tostring, et = etree.ElementTree): |
16 | | - if isinstance(xml, str): |
17 | | - # If it's a string, encode and return. |
18 | | - return xml |
19 | | - elif isinstance(xml, tuple): |
20 | | - # If it's a tuple, encode and return the joined items. |
21 | | - # We do not accept lists here--emphasizing lists being used for ARRAY |
22 | | - # bounds. |
23 | | - return ''.join((x if isinstance(x, str) else tostr(x) for x in xml)) |
24 | | - return tostr(xml) |
25 | | - |
26 | 15 | def xml_unpack(xmldata, XML = etree.XML): |
27 | 16 | try: |
28 | 17 | return XML(xmldata) |
29 | 18 | except Exception: |
30 | 19 | # try it again, but return the sequence of children. |
31 | 20 | return tuple(XML('<x>' + xmldata + '</x>')) |
32 | 21 |
|
33 | | -def xml_io_factory(typoid, typio, c = compose): |
34 | | - return ( |
35 | | - c((xml_pack, typio.encode)), |
36 | | - c((typio.decode, xml_unpack)), |
37 | | - etree.ElementTree, |
38 | | - ) |
| 22 | +if not hasattr(etree, 'tostringlist'): |
| 23 | + # Python 3.1 support. |
| 24 | + def xml_pack(xml, tostr = etree.tostring, et = etree.ElementTree, |
| 25 | + str = str, isinstance = isinstance, tuple = tuple |
| 26 | + ): |
| 27 | + if isinstance(xml, str): |
| 28 | + # If it's a string, encode and return. |
| 29 | + return xml |
| 30 | + elif isinstance(xml, tuple): |
| 31 | + # If it's a tuple, encode and return the joined items. |
| 32 | + # We do not accept lists here--emphasizing lists being used for ARRAY |
| 33 | + # bounds. |
| 34 | + return ''.join((x if isinstance(x, str) else tostr(x) for x in xml)) |
| 35 | + return tostr(xml) |
| 36 | + |
| 37 | + def xml_io_factory(typoid, typio, c = compose): |
| 38 | + return ( |
| 39 | + c((xml_pack, typio.encode)), |
| 40 | + c((typio.decode, xml_unpack)), |
| 41 | + etree.ElementTree, |
| 42 | + ) |
| 43 | +else: |
| 44 | + # New etree tostring API. |
| 45 | + def xml_pack(xml, encoding, |
| 46 | + tostr = etree.tostringlist, et = etree.ElementTree, |
| 47 | + str = str, isinstance = isinstance, tuple = tuple, |
| 48 | + ): |
| 49 | + if isinstance(xml, str): |
| 50 | + # If it's a string, encode and return. |
| 51 | + return xml.encode(encoding) |
| 52 | + elif isinstance(xml, tuple): |
| 53 | + # If it's a tuple, encode and return the joined items. |
| 54 | + # We do not accept lists here--emphasizing lists being used for ARRAY |
| 55 | + # bounds. |
| 56 | + return b''.join(( |
| 57 | + x.encode(encoding) if isinstance(x, str) else |
| 58 | + b''.join(tostr(x, encoding = encoding)[1:]) for x in xml |
| 59 | + )) |
| 60 | + return b''.join(tostr(xml, encoding = encoding)[1:]) |
| 61 | + |
| 62 | + def xml_io_factory(typoid, typio, c = compose): |
| 63 | + def local_xml_pack(x, typio = typio): |
| 64 | + return xml_pack(x, typio.encoding) |
| 65 | + return ( |
| 66 | + local_xml_pack, |
| 67 | + c((typio.decode, xml_unpack)), |
| 68 | + etree.ElementTree, |
| 69 | + ) |
39 | 70 |
|
40 | 71 | oid_to_io = { |
41 | 72 | XMLOID : xml_io_factory |
|
0 commit comments