|
10 | 10 | ) |
11 | 11 | from wolframclient.serializers.wxfencoder.streaming import ExactSizeReader, ZipCompressedReader |
12 | 12 | from wolframclient.utils import six |
| 13 | +from wolframclient.utils.dispatch import Dispatch |
| 14 | + |
| 15 | +wxf_input_to_buffer = Dispatch() |
| 16 | + |
| 17 | +@wxf_input_to_buffer.dispatch((six.binary_type, six.buffer_types)) |
| 18 | +def encode_buffer(wxf_input): |
| 19 | + return six.BytesIO(wxf_input) |
| 20 | + |
| 21 | +if six.PY2: |
| 22 | + @wxf_input_to_buffer.dispatch(memoryview, replace_existing = True) |
| 23 | + def encode_buffer(wxf_input): |
| 24 | + return six.BytesIO(wxf_input.tobytes()) |
| 25 | + |
| 26 | +@wxf_input_to_buffer.dispatch(object) |
| 27 | +def encode_default(wxf_input): |
| 28 | + if hasattr(wxf_input, 'read'): |
| 29 | + return wxf_input |
| 30 | + raise TypeError( |
| 31 | + "Class %s neither implements a read method nor is a binary type." |
| 32 | + % wxf_input.__class__.__name__ |
| 33 | + ) |
| 34 | + |
13 | 35 |
|
14 | 36 |
|
15 | 37 | class WXFParser(object): |
@@ -58,15 +80,8 @@ def __init__(self, wxf_input): |
58 | 80 | """WXF parser returning Python object from a WXF encoded byte sequence. |
59 | 81 | """ |
60 | 82 | self.context = SerializationContext() |
61 | | - if isinstance(wxf_input, (six.binary_type, six.buffer_types)): |
62 | | - self.reader = six.BytesIO(wxf_input) |
63 | | - elif hasattr(wxf_input, "read"): |
64 | | - self.reader = wxf_input |
65 | | - else: |
66 | | - raise TypeError( |
67 | | - "Class %s neither implements a read method nor is a binary type." |
68 | | - % wxf_input.__class__.__name__ |
69 | | - ) |
| 83 | + self.reader = wxf_input_to_buffer(wxf_input) |
| 84 | + |
70 | 85 | version, compress = self.parse_header() |
71 | 86 | if compress == True: |
72 | 87 | self.reader = ZipCompressedReader(self.reader) |
|
0 commit comments