Debian Codegraph @

src/annotations.pyx

raw
1 #-------------------------------------------------------------------------
2 # Copyright (c) Microsoft Corporation. All rights reserved.
3 # Licensed under the MIT License. See License.txt in the project root for
4 # license information.
5 #--------------------------------------------------------------------------
6
7 # Python imports
8 import logging
9 import copy
10
11 # C imports
12 from libc cimport stdint
13
14 cimport c_amqpvalue
15 cimport c_amqp_definitions
16 cimport c_utils
17
18
19 _logger = logging.getLogger(__name__)
20
21
22 cdef annotations_factory(c_amqpvalue.AMQP_VALUE value):
23 try:
24 wrapped = value_factory(value)
25 except TypeError:
26 return None
27 if c_amqp_definitions.is_delivery_annotations_type_by_descriptor(<c_amqp_definitions.delivery_annotations>value):
28 new_obj = create_delivery_annotations(wrapped)
29 elif c_amqp_definitions.is_message_annotations_type_by_descriptor(<c_amqp_definitions.message_annotations>value):
30 new_obj = create_message_annotations(wrapped)
31 elif c_amqp_definitions.is_footer_type_by_descriptor(<c_amqp_definitions.footer>value):
32 new_obj = create_footer(wrapped)
33 else:
34 new_obj = create_annotations(wrapped)
35 return new_obj
36
37
38 cpdef create_annotations(AMQPValue value):
39 annotations = cAnnotations()
40 annotations.create(value)
41 return annotations
42
43
44 cpdef create_application_properties(AMQPValue value):
45 annotations = cApplicationProperties()
46 annotations.create(value)
47 return annotations
48
49
50 cpdef create_delivery_annotations(AMQPValue value):
51 annotations = cDeliveryAnnotations()
52 annotations.create(value)
53 return annotations
54
55
56 cpdef create_message_annotations(AMQPValue value):
57 annotations = cMessageAnnotations()
58 annotations.create(value)
59 return annotations
60
61
62 cpdef create_fields(AMQPValue value):
63 annotations = cFields()
64 annotations.create(value)
65 return annotations
66
67
68 cpdef create_footer(AMQPValue value):
69 annotations = cFooter()
70 annotations.create(value)
71 return annotations
72
73
74 cdef class cAnnotations(StructBase):
75
76 cdef c_amqpvalue.AMQP_VALUE _c_value
77
78 def __cinit__(self):
79 pass
80
81 def __dealloc__(self):
82 _logger.debug("Deallocating %r", self.__class__.__name__)
83 self.destroy()
84
85 cdef _validate(self):
86 if <void*>self._c_value is NULL:
87 self._memory_error()
88
89 cpdef destroy(self):
90 try:
91 if <void*>self._c_value is not NULL:
92 _logger.debug("Destroying %r", self.__class__.__name__)
93 c_amqpvalue.amqpvalue_destroy(<c_amqpvalue.AMQP_VALUE>self._c_value)
94 except KeyboardInterrupt:
95 pass
96 finally:
97 self._c_value = <c_amqpvalue.AMQP_VALUE>NULL
98
99 cpdef clone(self):
100 cdef c_amqp_definitions.annotations value
101 value = c_amqpvalue.amqpvalue_clone(<c_amqpvalue.AMQP_VALUE>self._c_value)
102 if <void*>value == NULL:
103 self._value_error()
104 amqp_value = value_factory(value)
105 return cAnnotations(amqp_value)
106
107 cpdef get_encoded_size(self):
108 cdef size_t length
109 if c_amqpvalue.amqpvalue_get_encoded_size(self._c_value, &length) != 0:
110 self._value_error("Failed to get encoded size.")
111 return length
112
113 cpdef create(self, AMQPValue value):
114 self.destroy()
115 self._c_value = c_amqp_definitions.amqpvalue_create_annotations(<c_amqpvalue.AMQP_VALUE>value._c_value)
116 self._validate()
117
118 cdef wrap(self, c_amqp_definitions.annotations value):
119 self.destroy()
120 self._c_value = value
121 self._validate()
122
123 @property
124 def value(self):
125 try:
126 return value_factory(self._c_value)
127 except TypeError:
128 return None
129
130 @property
131 def map(self):
132 cdef c_amqpvalue.AMQP_VALUE unmapped
133 cdef c_amqpvalue.AMQP_VALUE mapped
134 unmapped = c_amqpvalue.amqpvalue_clone(<c_amqpvalue.AMQP_VALUE>self._c_value)
135 if c_amqpvalue.amqpvalue_get_map(unmapped, &mapped) == 0:
136 if <void*>mapped == NULL:
137 return None
138 return copy.deepcopy(value_factory(mapped).value)
139 else:
140 return None
141
142
143 cdef class cApplicationProperties(cAnnotations):
144
145 cpdef create(self, AMQPValue value):
146 self.destroy()
147 self._c_value = c_amqp_definitions.amqpvalue_create_application_properties(
148 <c_amqp_definitions.application_properties>value._c_value)
149 self._validate()
150
151 @property
152 def map(self):
153 cdef c_amqpvalue.AMQP_VALUE unmapped
154 cdef c_amqpvalue.AMQP_VALUE mapped
155 cdef c_amqpvalue.AMQP_VALUE unextracted
156 cdef c_amqpvalue.AMQP_VALUE extracted
157 unextracted = c_amqpvalue.amqpvalue_clone(<c_amqpvalue.AMQP_VALUE>self._c_value)
158 extracted = c_amqpvalue.amqpvalue_get_inplace_described_value(unextracted)
159 unmapped = c_amqpvalue.amqpvalue_clone(extracted)
160 if <void*>unmapped == NULL:
161 result = None
162 elif c_amqpvalue.amqpvalue_get_map(unmapped, &mapped) == 0:
163 if <void*>mapped == NULL:
164 result = None
165 else:
166 result = copy.deepcopy(value_factory(mapped).value)
167 else:
168 result = None
169 c_amqpvalue.amqpvalue_destroy(unextracted)
170 return result
171
172
173 cdef class cDeliveryAnnotations(cAnnotations):
174
175 cpdef create(self, AMQPValue value):
176 self.destroy()
177 self._c_value = c_amqp_definitions.amqpvalue_create_delivery_annotations(
178 <c_amqp_definitions.delivery_annotations>value._c_value)
179 self._validate()
180
181
182 cdef class cMessageAnnotations(cAnnotations):
183
184 cpdef create(self, AMQPValue value):
185 self.destroy()
186 self._c_value = c_amqp_definitions.amqpvalue_create_message_annotations(
187 <c_amqp_definitions.message_annotations>value._c_value)
188 self._validate()
189
190
191 cdef class cFields(cAnnotations):
192
193 cpdef create(self, AMQPValue value):
194 self.destroy()
195 self._c_value = c_amqp_definitions.amqpvalue_create_fields(<c_amqp_definitions.fields>value._c_value)
196 self._validate()
197
198
199 cdef class cFooter(cAnnotations):
200
201 cpdef create(self, AMQPValue value):
202 self.destroy()
203 self._c_value = c_amqp_definitions.amqpvalue_create_footer(<c_amqp_definitions.footer>value._c_value)
204 self._validate()