def test_reading_events_non_blocking():
    # http://amzn.github.io/ion-docs/guides/cookbook.html#reading-and-writing-ion-data
    reader = managed_reader(text_reader())
    event = reader.send(NEXT_EVENT)
    # No data has been provided, so the reader is at STREAM_END
    # and will wait for data.
    assert event.event_type == IonEventType.STREAM_END
    # Send an incomplete Ion value.
    event = reader.send(read_data_event(b'{hello:'))
    # Enough data was available for the reader to determine that
    # the start of a struct value has been encountered.
    assert event.event_type == IonEventType.CONTAINER_START
    assert event.ion_type == IonType.STRUCT
    # Advancing the reader causes it to step into the struct.
    event = reader.send(NEXT_EVENT)
    # The reader reached the end of the data before completing
    # a value. Therefore, an INCOMPLETE event is returned.
    assert event.event_type == IonEventType.INCOMPLETE
    # Send the rest of the value.
    event = reader.send(read_data_event(b'"world"}'))
    # The reader now finishes parsing the value within the struct.
    assert event.event_type == IonEventType.SCALAR
    assert event.ion_type == IonType.STRING
    hello = event.field_name.text
    world = event.value
    # Advance the reader past the string value.
    event = reader.send(NEXT_EVENT)
    # The reader has reached the end of the struct.
    assert event.event_type == IonEventType.CONTAINER_END
    # Advancing the reader causes it to step out of the struct.
    event = reader.send(NEXT_EVENT)
    # There is no more data and a value has been completed.
    # Therefore, the reader conveys STREAM_END.
    assert event.event_type == IonEventType.STREAM_END
    assert u'hello world' == u'%s %s' % (hello, world)
def test_sparse_reads_events():
    # http://amzn.github.io/ion-docs/guides/cookbook.html#performing-sparse-reads
    data = BytesIO(sparse_reads_data())
    reader = blocking_reader(managed_reader(binary_reader()), data)
    sum = 0
    event = reader.send(NEXT_EVENT)
    while event != ION_STREAM_END_EVENT:
        assert event.event_type == IonEventType.CONTAINER_START
        assert event.ion_type == IonType.STRUCT
        if u'foo' == event.annotations[0].text:
            # Step into the struct.
            event = reader.send(NEXT_EVENT)
            while event.event_type != IonEventType.CONTAINER_END:
                if u'quantity' == event.field_name.text:
                    sum += event.value
                event = reader.send(NEXT_EVENT)
            # Step out of the struct.
            event = reader.send(NEXT_EVENT)
        else:
            # Skip over the struct without parsing its values.
            event = reader.send(SKIP_EVENT)
            assert event.event_type == IonEventType.CONTAINER_END
            # Position the reader at the start of the next value.
            event = reader.send(NEXT_EVENT)
    assert 20 == sum
    def _f(algorithm):
        buf.seek(0)
        reader = hash_reader(
            ion_reader.blocking_reader(managed_reader(reader_provider(), None),
                                       buf),
            hash_function_provider(algorithm, _actual_updates,
                                   _actual_digests))

        _consume(reader)

        return reader.send(HashEvent.DIGEST)
    def _f(algorithm):
        buf.seek(0)
        reader = ion_reader.blocking_reader(
            managed_reader(reader_provider(), None), buf)

        writer = hash_writer(
            blocking_writer(raw_writer(), BytesIO()),
            hash_function_provider(algorithm, _actual_updates,
                                   _actual_digests))

        _consume(reader, writer)

        digest = writer.send(HashEvent.DIGEST)
        writer.send(IonEvent(IonEventType.STREAM_END))
        return digest
    def skipping_consumer(algorithm):
        buf.seek(0)
        reader = hash_reader(
            ion_reader.blocking_reader(
                managed_reader(_reader_provider("binary")(), None), buf),
            hash_function_provider(algorithm, _actual_updates,
                                   _actual_digests))

        event = reader.send(NEXT_EVENT)
        while event.event_type != IonEventType.STREAM_END:
            if event.event_type == IonEventType.CONTAINER_START:
                event = reader.send(SKIP_EVENT)
            else:
                event = reader.send(NEXT_EVENT)

        return reader.send(HashEvent.DIGEST)
def test_read_numerics_events():
    # http://amzn.github.io/ion-docs/guides/cookbook.html#reading-numeric-types
    data = BytesIO(b'1.23456 1.2345e6 123456 12345678901234567890')
    reader = blocking_reader(managed_reader(text_reader()), data)
    event = reader.send(NEXT_EVENT)
    assert event.ion_type == IonType.DECIMAL
    assert isinstance(event.value, Decimal)
    event = reader.send(NEXT_EVENT)
    assert event.ion_type == IonType.FLOAT
    assert isinstance(event.value, float)
    event = reader.send(NEXT_EVENT)
    assert event.ion_type == IonType.INT
    assert isinstance(event.value, int)
    event = reader.send(NEXT_EVENT)
    assert event.ion_type == IonType.INT
    assert isinstance(event.value, int)
def test_reading_events_blocking():
    # http://amzn.github.io/ion-docs/guides/cookbook.html#reading-and-writing-ion-data
    data = BytesIO(b'{hello: "world"}')
    reader = blocking_reader(managed_reader(text_reader()), data)
    event = reader.send(NEXT_EVENT)
    assert event.event_type == IonEventType.CONTAINER_START
    assert event.ion_type == IonType.STRUCT
    # Advancing the reader causes it to step into the struct.
    event = reader.send(NEXT_EVENT)
    assert event.event_type == IonEventType.SCALAR
    assert event.ion_type == IonType.STRING
    hello = event.field_name.text
    world = event.value
    # Advance the reader past the string value.
    event = reader.send(NEXT_EVENT)
    # The reader has reached the end of the struct.
    assert event.event_type == IonEventType.CONTAINER_END
    # Advancing the reader causes it to step out of the struct.
    event = reader.send(NEXT_EVENT)
    # There is no more data and a value has been completed.
    # Therefore, the reader conveys STREAM_END.
    assert event.event_type == IonEventType.STREAM_END
    assert u'hello world' == u'%s %s' % (hello, world)
def test_read_with_shared_symbol_table_events():
    # http://amzn.github.io/ion-docs/guides/cookbook.html#using-a-shared-symbol-table
    table = shared_symbol_table(u'test.csv.columns', 1,
                                (u'id', u'type', u'state'))
    catalog = SymbolTableCatalog()
    catalog.register(table)
    data = BytesIO(write_with_shared_symbol_table_simpleion())
    reader = blocking_reader(managed_reader(binary_reader(), catalog=catalog),
                             data)
    # Position the reader at the first struct.
    reader.send(NEXT_EVENT)
    # Skip over the struct.
    reader.send(SKIP_EVENT)
    # Position the reader at the second struct.
    reader.send(NEXT_EVENT)
    # Skip over the struct.
    reader.send(SKIP_EVENT)
    # Position the reader at the third struct.
    event = reader.send(NEXT_EVENT)
    assert event.ion_type == IonType.STRUCT
    # Step into the struct
    event = reader.send(NEXT_EVENT)
    assert u'id' == event.field_name.text
    assert 3 == event.value
Exemple #9
0
def binary_reader_over(ion_str):
    value = ion.loads(ion_str)
    _bytes = ion.dumps(value, binary=True)
    return ion_reader.blocking_reader(managed_reader(binary_reader(), None),
                                      BytesIO(_bytes))
def test_managed_reader(p):
    reader_scaffold(managed_reader(_predefined_reader(p.inner), p.catalog),
                    p.outer)
def test_managed_reader(p):
    reader_scaffold(managed_reader(_predefined_reader(p.inner), p.catalog), p.outer)