| 1 |
# |
| 2 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
# or more contributor license agreements. See the NOTICE file |
| 4 |
# distributed with this work for additional information |
| 5 |
# regarding copyright ownership. The ASF licenses this file |
| 6 |
# to you under the Apache License, Version 2.0 (the |
| 7 |
# "License"); you may not use this file except in compliance |
| 8 |
# with the License. You may obtain a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, |
| 13 |
# software distributed under the License is distributed on an |
| 14 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
# KIND, either express or implied. See the License for the |
| 16 |
# specific language governing permissions and limitations |
| 17 |
# under the License. |
| 18 |
# |
| 19 |
|
| 20 |
from typing import Optional, TYPE_CHECKING |
| 21 |
|
| 22 |
from cproton import pn_condition_clear, pn_condition_set_name, pn_condition_set_description, pn_condition_info, \ |
| 23 |
pn_condition_is_set, pn_condition_get_name, pn_condition_get_description |
| 24 |
|
| 25 |
from ._data import Data, dat2obj |
| 26 |
|
| 27 |
if TYPE_CHECKING: |
| 28 |
from ._data import PythonAMQPData |
| 29 |
|
| 30 |
|
| 31 |
class Condition: |
| 32 |
""" |
| 33 |
An AMQP Condition object. Conditions hold exception information |
| 34 |
pertaining to the closing of an AMQP endpoint such as a :class:`Connection`, |
| 35 |
:class:`Session`, or :class:`Link`. Conditions also hold similar information |
| 36 |
pertaining to deliveries that have reached terminal states. |
| 37 |
Connections, Sessions, Links, and Deliveries may all have local and |
| 38 |
remote conditions associated with them. |
| 39 |
|
| 40 |
The local condition may be modified by the local endpoint to signal |
| 41 |
a particular condition to the remote peer. The remote condition may |
| 42 |
be examined by the local endpoint to detect whatever condition the |
| 43 |
remote peer may be signaling. Although often conditions are used to |
| 44 |
indicate errors, not all conditions are errors *per/se*, e.g. |
| 45 |
conditions may be used to redirect a connection from one host to |
| 46 |
another. |
| 47 |
|
| 48 |
Every condition has a short symbolic name, a longer description, |
| 49 |
and an additional info map associated with it. The name identifies |
| 50 |
the formally defined condition, and the map contains additional |
| 51 |
information relevant to the identified condition. |
| 52 |
|
| 53 |
:ivar ~.name: The name of the condition. |
| 54 |
:ivar ~.description: A description of the condition. |
| 55 |
:ivar ~.info: A data object that holds the additional information associated |
| 56 |
with the condition. The data object may be used both to access and to |
| 57 |
modify the additional information associated with the condition. |
| 58 |
""" |
| 59 |
|
| 60 |
def __init__( |
| 61 |
self, |
| 62 |
name: str, |
| 63 |
description: Optional[str] = None, |
| 64 |
info: Optional['PythonAMQPData'] = None |
| 65 |
) -> None: |
| 66 |
self.name = name |
| 67 |
self.description = description |
| 68 |
self.info = info |
| 69 |
|
| 70 |
def __repr__(self) -> str: |
| 71 |
return "Condition(%s)" % ", ".join([repr(x) for x in |
| 72 |
(self.name, self.description, self.info) |
| 73 |
if x]) |
| 74 |
|
| 75 |
def __eq__(self, o: 'Condition') -> bool: |
| 76 |
if not isinstance(o, Condition): |
| 77 |
return False |
| 78 |
return self.name == o.name and \ |
| 79 |
self.description == o.description and \ |
| 80 |
self.info == o.info |
| 81 |
|
| 82 |
|
| 83 |
def obj2cond(obj, cond: Condition) -> None: |
| 84 |
pn_condition_clear(cond) |
| 85 |
if obj: |
| 86 |
pn_condition_set_name(cond, str(obj.name)) |
| 87 |
pn_condition_set_description(cond, obj.description) |
| 88 |
info = Data(pn_condition_info(cond)) |
| 89 |
if obj.info: |
| 90 |
info.put_object(obj.info) |
| 91 |
|
| 92 |
|
| 93 |
def cond2obj(cond) -> Optional[Condition]: |
| 94 |
if pn_condition_is_set(cond): |
| 95 |
return Condition(pn_condition_get_name(cond), |
| 96 |
pn_condition_get_description(cond), |
| 97 |
dat2obj(pn_condition_info(cond))) |
| 98 |
else: |
| 99 |
return None |