This is an automated email from the ASF dual-hosted git repository. astitcher pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/qpid-proton.git
commit 4c5adb518ed20e47001c269a7445151e68dae379 Author: Andrew Stitcher <[email protected]> AuthorDate: Fri May 31 18:37:54 2024 -0400 PROTON-2844: [Python] Replace custom NamedInt with standard IntEnum Updated python code to take advantage of newer python library features. --- python/proton/_delivery.py | 82 ++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 47 deletions(-) diff --git a/python/proton/_delivery.py b/python/proton/_delivery.py index bdfd42b37..5d62d86b6 100644 --- a/python/proton/_delivery.py +++ b/python/proton/_delivery.py @@ -31,7 +31,9 @@ from ._condition import cond2obj, obj2cond from ._data import dat2obj, obj2dat from ._wrapper import Wrapper -from typing import Dict, List, Optional, Type, Union, TYPE_CHECKING +from enum import IntEnum +from typing import Dict, List, Optional, Union, TYPE_CHECKING + if TYPE_CHECKING: from ._condition import Condition @@ -40,57 +42,21 @@ if TYPE_CHECKING: from ._reactor import Connection, Session, Transport -class NamedInt(int): - values: Dict[int, 'DispositionType'] = {} - - def __new__(cls: Type['DispositionType'], i: int, name: str) -> 'DispositionType': - ni = super(NamedInt, cls).__new__(cls, i) - cls.values[i] = ni - return ni - - def __init__(self, i: int, name: str) -> None: - self.name = name - - def __repr__(self) -> str: - return self.name - - def __str__(self) -> str: - return self.name - - @classmethod - def get(cls, i: int) -> Union[int, 'DispositionType']: - return cls.values.get(i, i) - - -class DispositionType(NamedInt): - values = {} - - -class Disposition(object): - """ - A delivery state. - - Dispositions record the current state or final outcome of a - transfer. Every delivery contains both a local and remote - disposition. The local disposition holds the local state of the - delivery, and the remote disposition holds the last known remote - state of the delivery. - """ - - RECEIVED = DispositionType(PN_RECEIVED, "RECEIVED") +class DispositionType(IntEnum): + RECEIVED = PN_RECEIVED """ A non terminal state indicating how much (if any) message data has been received for a delivery. """ - ACCEPTED = DispositionType(PN_ACCEPTED, "ACCEPTED") + ACCEPTED = PN_ACCEPTED """ A terminal state indicating that the delivery was successfully processed. Once in this state there will be no further state changes prior to the delivery being settled. """ - REJECTED = DispositionType(PN_REJECTED, "REJECTED") + REJECTED = PN_REJECTED """ A terminal state indicating that the delivery could not be processed due to some error condition. Once in this state @@ -98,14 +64,14 @@ class Disposition(object): being settled. """ - RELEASED = DispositionType(PN_RELEASED, "RELEASED") + RELEASED = PN_RELEASED """ A terminal state indicating that the delivery is being returned to the sender. Once in this state there will be no further state changes prior to the delivery being settled. """ - MODIFIED = DispositionType(PN_MODIFIED, "MODIFIED") + MODIFIED = PN_MODIFIED """ A terminal state indicating that the delivery is being returned to the sender and should be annotated by the @@ -114,6 +80,28 @@ class Disposition(object): delivery being settled. """ + @classmethod + def or_int(cls, i: int) -> Union[int, 'DispositionType']: + return cls(i) if i in cls._value2member_map_ else i + + +class Disposition(object): + """ + A delivery state. + + Dispositions record the current state or final outcome of a + transfer. Every delivery contains both a local and remote + disposition. The local disposition holds the local state of the + delivery, and the remote disposition holds the last known remote + state of the delivery. + """ + + RECEIVED = DispositionType.RECEIVED + ACCEPTED = DispositionType.ACCEPTED + REJECTED = DispositionType.REJECTED + RELEASED = DispositionType.RELEASED + MODIFIED = DispositionType.MODIFIED + def __init__(self, impl, local): self._impl = impl self.local = local @@ -134,7 +122,7 @@ class Disposition(object): * :const:`RELEASED` * :const:`MODIFIED` """ - return DispositionType.get(pn_disposition_type(self._impl)) + return DispositionType.or_int(pn_disposition_type(self._impl)) @property def section_number(self) -> int: @@ -363,14 +351,14 @@ class Delivery(Wrapper): return pn_delivery_partial(self._impl) @property - def local_state(self) -> DispositionType: + def local_state(self) -> Union[int, DispositionType]: """A local state of the delivery.""" - return DispositionType.get(pn_delivery_local_state(self._impl)) + return DispositionType.or_int(pn_delivery_local_state(self._impl)) @property def remote_state(self) -> Union[int, DispositionType]: """A remote state of the delivery as indicated by the remote peer.""" - return DispositionType.get(pn_delivery_remote_state(self._impl)) + return DispositionType.or_int(pn_delivery_remote_state(self._impl)) @property def settled(self) -> bool: --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
