diff --git a/sphinx/conf.py b/sphinx/conf.py index d5323e0..b7577cc 100644 --- a/sphinx/conf.py +++ b/sphinx/conf.py @@ -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) diff --git a/src/asyncgui_ext/queue.py b/src/asyncgui_ext/queue.py index eb3aff0..7c46060 100644 --- a/src/asyncgui_ext/queue.py +++ b/src/asyncgui_ext/queue.py @@ -3,7 +3,6 @@ 'Queue', 'Order', 'QueueState', ) import typing as T -import enum import heapq from functools import partial from collections import deque @@ -11,31 +10,12 @@ 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): @@ -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. ''' @@ -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 @@ -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: @@ -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 @@ -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: @@ -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 @@ -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: @@ -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