Skip to content

Parser

elva.parser

Module defining parsers for change events from Y data types.

Classes:

Attributes:

ParserState = create_component_state('ParserState') module-attribute

The states of the EventParser component.

EventParser

Bases: Component

Parser base class.

This class is supposed to be inherited from and extended.

Methods:

  • run

    Hook running after the RUNNING state has been set.

  • check

    Check for the correct event type.

  • parse

    Queue event for parsing asynchronously.

  • parse_nowait

    Queue event for parsing synchronously.

  • parse_event

    Hook called when an event has been queued for parsing and which performs further actions.

Attributes:

event_type instance-attribute

Event type this parser is supposed to handle.

states property

The states this component can have.

run() async

Hook running after the RUNNING state has been set.

It initializes the buffer and waits for incoming events to parse.

Source code in src/elva/parser.py
async def run(self):
    """
    Hook running after the `RUNNING` state has been set.

    It initializes the buffer and waits for incoming events to parse.
    """
    self.send_stream, self.receive_stream = anyio.create_memory_object_stream(
        max_buffer_size=65543
    )
    async with self.send_stream, self.receive_stream:
        self.log.info("awaiting events")
        async for event in self.receive_stream:
            await self.parse_event(event)

check(event)

Check for the correct event type.

Parameters:

Raises:

Source code in src/elva/parser.py
def check(self, event: TextEvent | ArrayEvent | MapEvent):
    """
    Check for the correct `event` type.

    Arguments:
        event: object holding event information of changes to a Y data type.

    Raises:
        TypeError: if `event` is not an instance of [`event_type`][elva.parser.EventParser.event_type].
    """
    if not isinstance(event, self.event_type):
        raise TypeError(
            f"The event '{event}' is of type {type(event)}, but needs to be {self.event_type}"
        )

parse(event) async

Queue event for parsing asynchronously.

Parameters:

Source code in src/elva/parser.py
async def parse(self, event: TextEvent | ArrayEvent | MapEvent):
    """
    Queue `event` for parsing asynchronously.

    Arguments:
        event: object holding event information of changes to a Y data type.
    """
    self.check(event)
    await self.send_stream.send(event)
    self.log.debug("sending event")

parse_nowait(event)

Queue event for parsing synchronously.

Parameters:

Source code in src/elva/parser.py
def parse_nowait(self, event: TextEvent | ArrayEvent | MapEvent):
    """
    Queue `event` for parsing synchronously.

    Arguments:
        event: object holding event information of changes to a Y data type.
    """
    self.check(event)
    self.send_stream.send_nowait(event)

parse_event(event) async

Hook called when an event has been queued for parsing and which performs further actions.

This method is defined as a no-op and supposed to be implemented in the inheriting subclass.

Parameters:

Source code in src/elva/parser.py
async def parse_event(self, event: TextEvent | ArrayEvent | MapEvent):
    """
    Hook called when an `event` has been queued for parsing and which performs further actions.

    This method is defined as a no-op and supposed to be implemented in the inheriting subclass.

    Arguments:
        event: object holding event information of changes to a Y data type.
    """
    ...

TextEventParser

Bases: EventParser

TextEvent parser base class.

Methods:

  • parse_event

    Hook called when an event has been queued for parsing and which performs further actions.

  • on_retain

    Hook called on action retain.

  • on_insert

    Hook called on action insert.

  • on_delete

    Hook called on action delete.

Attributes:

  • event_type

    Event type this parser is supposed to handle.

event_type = TextEvent class-attribute instance-attribute

Event type this parser is supposed to handle.

parse_event(event) async

Hook called when an event has been queued for parsing and which performs further actions.

Parameters:

  • event (TextEvent) –

    object holding event information of changes to a Y text data type.

Source code in src/elva/parser.py
async def parse_event(self, event: TextEvent):
    """
    Hook called when an `event` has been queued for parsing and which performs further actions.

    Arguments:
        event: object holding event information of changes to a Y text data type.
    """
    deltas = event.delta

    range_offset = 0
    for delta in deltas:
        for action, var in delta.items():
            if action == "retain":
                range_offset = var
                await self.on_retain(range_offset)
            elif action == "insert":
                insert_value = var
                await self.on_insert(range_offset, insert_value)
            elif action == "delete":
                range_length = var
                await self.on_delete(range_offset, range_length)

on_retain(range_offset) async

Hook called on action retain.

This method is defined as a no-op and supposed to be implemented in the inheriting subclass.

Parameters:

  • range_offset (int) –

    byte offset in the Y text data type.

Source code in src/elva/parser.py
async def on_retain(self, range_offset: int):
    """
    Hook called on action `retain`.

    This method is defined as a no-op and supposed to be implemented in the inheriting subclass.

    Arguments:
        range_offset: byte offset in the Y text data type.
    """
    ...

on_insert(range_offset, insert_value) async

Hook called on action insert.

This method is defined as a no-op and supposed to be implemented in the inheriting subclass.

Parameters:

  • range_offset (int) –

    byte offset in the Y text data type.

  • insert_value (Any) –

    value that was inserted at range_offset

Source code in src/elva/parser.py
async def on_insert(self, range_offset: int, insert_value: Any):
    """
    Hook called on action `insert`.

    This method is defined as a no-op and supposed to be implemented in the inheriting subclass.

    Arguments:
        range_offset: byte offset in the Y text data type.
        insert_value: value that was inserted at `range_offset`
    """
    ...

on_delete(range_offset, range_length) async

Hook called on action delete.

This method is defined as a no-op and supposed to be implemented in the inheriting subclass.

Parameters:

  • range_offset (int) –

    byte offset in the Y text data type.

  • range_length (int) –

    number of bytes deleted starting at range_offset

Source code in src/elva/parser.py
async def on_delete(self, range_offset: int, range_length: int):
    """
    Hook called on action `delete`.

    This method is defined as a no-op and supposed to be implemented in the inheriting subclass.

    Arguments:
        range_offset: byte offset in the Y text data type.
        range_length: number of bytes deleted starting at `range_offset`
    """
    ...

ArrayEventParser

Bases: EventParser

ArrayEvent parser base class.

Methods:

  • parse_event

    Hook called when an event has been queued for parsing and which performs further actions.

  • on_retain

    Hook called on action retain.

  • on_insert

    Hook called on action insert.

  • on_delete

    Hook called on action delete.

Attributes:

  • event_type

    Event type this parser is supposed to handle.

event_type = ArrayEvent class-attribute instance-attribute

Event type this parser is supposed to handle.

parse_event(event) async

Hook called when an event has been queued for parsing and which performs further actions.

Parameters:

  • event (ArrayEvent) –

    object holding event information of changes to a Y array data type.

Source code in src/elva/parser.py
async def parse_event(self, event: ArrayEvent):
    """
    Hook called when an `event` has been queued for parsing and which performs further actions.

    Arguments:
        event: object holding event information of changes to a Y array data type.
    """
    deltas = event.delta

    range_offset = 0
    for delta in deltas:
        for action, var in delta.items():
            if action == "retain":
                range_offset = var
                await self.on_retain(range_offset)
            elif action == "insert":
                insert_value = var
                await self.on_insert(range_offset, insert_value)
            elif action == "delete":
                range_length = var
                await self.on_delete(range_offset, range_length)

on_retain(range_offset) async

Hook called on action retain.

This method is defined as a no-op and supposed to be implemented in the inheriting subclass.

Parameters:

  • range_offset (int) –

    byte offset in the Y array data type.

Source code in src/elva/parser.py
async def on_retain(self, range_offset: int):
    """
    Hook called on action `retain`.

    This method is defined as a no-op and supposed to be implemented in the inheriting subclass.

    Arguments:
        range_offset: byte offset in the Y array data type.
    """
    ...

on_insert(range_offset, insert_value) async

Hook called on action insert.

This method is defined as a no-op and supposed to be implemented in the inheriting subclass.

Parameters:

  • range_offset (int) –

    byte offset in the Y array data type.

  • insert_value (Any) –

    value that was inserted at range_offset

Source code in src/elva/parser.py
async def on_insert(self, range_offset: int, insert_value: Any):
    """
    Hook called on action `insert`.

    This method is defined as a no-op and supposed to be implemented in the inheriting subclass.

    Arguments:
        range_offset: byte offset in the Y array data type.
        insert_value: value that was inserted at `range_offset`
    """
    ...

on_delete(range_offset, range_length) async

Hook called on action delete.

This method is defined as a no-op and supposed to be implemented in the inheriting subclass.

Parameters:

  • range_offset (int) –

    byte offset in the Y array data type.

  • range_length (int) –

    number of items deleted starting at range_offset

Source code in src/elva/parser.py
async def on_delete(self, range_offset: int, range_length: int):
    """
    Hook called on action `delete`.

    This method is defined as a no-op and supposed to be implemented in the inheriting subclass.

    Arguments:
        range_offset: byte offset in the Y array data type.
        range_length: number of items deleted starting at `range_offset`
    """
    ...

MapEventParser

Bases: EventParser

MapEvent parser base class.

Methods:

  • parse_event

    Hook called when an event has been queued for parsing and which performs further actions.

  • on_add

    Hook called on action add.

  • on_delete

    Hook called on action delete.

Attributes:

  • event_type

    Event type this parser is supposed to handle.

event_type = MapEvent class-attribute instance-attribute

Event type this parser is supposed to handle.

parse_event(event) async

Hook called when an event has been queued for parsing and which performs further actions.

Parameters:

  • event (MapEvent) –

    object holding event information of changes to a Y map data type.

Source code in src/elva/parser.py
async def parse_event(self, event: MapEvent):
    """
    Hook called when an `event` has been queued for parsing and which performs further actions.

    Arguments:
        event: object holding event information of changes to a Y map data type.
    """
    keys = event.keys

    for key, delta in keys.items():
        print(delta)
        action = delta["action"]
        if action == "add":
            new_value = delta["newValue"]
            await self.on_add(key, new_value)
        elif action == "delete":
            old_value = delta["oldValue"]
            await self.on_delete(key, old_value)

on_add(key, new_value) async

Hook called on action add.

This method is defined as a no-op and supposed to be implemented in the inheriting subclass.

Parameters:

  • key (str) –

    key added to the Y map data type.

  • new_value (Any) –

    value associated with key in the Y map data type.

Source code in src/elva/parser.py
async def on_add(self, key: str, new_value: Any):
    """
    Hook called on action `add`.

    This method is defined as a no-op and supposed to be implemented in the inheriting subclass.

    Arguments:
        key: key added to the Y map data type.
        new_value: value associated with `key` in the Y map data type.
    """
    ...

on_delete(key, old_value) async

Hook called on action delete.

This method is defined as a no-op and supposed to be implemented in the inheriting subclass.

Parameters:

  • key (str) –

    key deleted from the Y map data type.

  • old_value (Any) –

    value which was associated with key in the Y map data type.

Source code in src/elva/parser.py
async def on_delete(self, key: str, old_value: Any):
    """
    Hook called on action `delete`.

    This method is defined as a no-op and supposed to be implemented in the inheriting subclass.

    Arguments:
        key: key deleted from the Y map data type.
        old_value: value which was associated with `key` in the Y map data type.
    """
    ...