Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions sphinx/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,3 @@
# -- Options for tabs extension ---------------------------------------
# https://sphinx-tabs.readthedocs.io/en/latest/
sphinx_tabs_disable_tab_closing = True


def modify_signature(app, what: str, name: str, obj, options, signature, return_annotation: str,
prefix="asyncgui_ext.queue.",
len_prefix=len("asyncgui_ext.queue."),
):
if not name.startswith(prefix):
return (signature, return_annotation, )
name = name[len_prefix:]
if name == "QueueState":
print(f"Hide the signature of {name!r}")
return ('', None)
return (signature, return_annotation, )


def setup(app):
app.connect('autodoc-process-signature', modify_signature)
64 changes: 24 additions & 40 deletions src/asyncgui_ext/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,19 @@
'Queue', 'Order', 'QueueState',
)
import typing as T
import enum
import heapq
from functools import partial
from collections import deque

from asyncgui import ExclusiveEvent


class QueueState(enum.Enum):
'''
Enum class that represents the state of the Queue.
'''

OPENED = enum.auto()
'''
All operations are allowed.

:meta hide-value:
'''

HALF_CLOSED = enum.auto()
'''
Putting an item is not allowed.

:meta hide-value:
'''

CLOSED = enum.auto()
'''
Putting or getting an item is not allowed.

:meta hide-value:
'''
QueueState: T.TypeAlias = T.Literal["opened", "half-closed", "closed"]
'''
* opened ... All operations are allowed.
* half-closed ... Putting an item is not allowed.
* closed ... Putting or getting an item is not allowed.
'''


class QueueException(Exception):
Expand All @@ -50,9 +30,9 @@ class Closed(QueueException):
'''
Occurs when:

* trying to **get** an item from a queue that is in the ``CLOSED`` state.
* trying to **get** an item from an **empty** queue that is in the ``HALF_CLOSED`` state.
* trying to **put** an item into a queue that is in the ``CLOSED`` or ``HALF_CLOSED`` state.
* Trying to **get** an item from a queue that is in the ``closed`` state.
* Trying to **get** an item from an **empty** queue that is in the ``half-closed`` state.
* Trying to **put** an item into a queue that is in the ``closed`` or ``half-closed`` state.
'''


Expand All @@ -79,7 +59,7 @@ def __init__(self, *, capacity: int | None=None, order: Order='fifo'):
elif (not isinstance(capacity, int)) or capacity < 1:
raise ValueError(f"'capacity' must be either a positive integer or None. (was {capacity!r})")
self._init_container(capacity, order)
self._state = QueueState.OPENED
self._state: QueueState = "opened"
self._putters = deque[tuple[ExclusiveEvent, Item]]()
self._getters = deque[ExclusiveEvent]()
self._capacity = capacity
Expand Down Expand Up @@ -130,15 +110,19 @@ def is_full(self) -> bool:
def order(self) -> Order:
return self._order

@property
def state(self) -> QueueState:
return self._state

async def get(self) -> T.Awaitable[Item]:
'''
.. code-block::

item = await queue.get()
'''
if self._state is QueueState.CLOSED:
if self._state == "closed":
raise Closed
if self._state is QueueState.HALF_CLOSED and self.is_empty:
if self._state == "half-closed" and self.is_empty:
raise Closed

if self._is_transferring or self.is_empty:
Expand All @@ -160,10 +144,10 @@ def get_nowait(self) -> Item:

item = queue.get_nowait()
'''
if self._state is QueueState.CLOSED:
if self._state == "closed":
raise Closed
if self.is_empty:
if self._state is QueueState.HALF_CLOSED:
if self._state == "half-closed":
raise Closed
raise WouldBlock

Expand All @@ -178,7 +162,7 @@ async def put(self, item) -> T.Awaitable:

await queue.put(item)
'''
if self._state is not QueueState.OPENED:
if self._state != "opened":
raise Closed

if self._is_transferring or self.is_full:
Expand All @@ -199,7 +183,7 @@ def put_nowait(self, item):

queue.put_nowait(item)
'''
if self._state is not QueueState.OPENED:
if self._state != "opened":
raise Closed
if self.is_full:
raise WouldBlock
Expand All @@ -213,9 +197,9 @@ def half_close(self):
Partially closes the queue.
Putting an item is no longer allowed.
'''
if self._state is not QueueState.OPENED:
if self._state != "opened":
return
self._state = QueueState.HALF_CLOSED
self._state = "half-closed"

Closed_ = Closed
for putter, __ in self._putters:
Expand All @@ -230,9 +214,9 @@ def close(self):
Fully closes the queue.
Putting or getting items are no longer allowed, and any items currently held will be discarded.
'''
if self._state is QueueState.CLOSED:
if self._state == "closed":
return
self._state = QueueState.CLOSED
self._state = "closed"
self._c.clear()

Closed_ = Closed
Expand Down
Loading