78 lines
2.3 KiB
Python
78 lines
2.3 KiB
Python
from amqp.adapter.logging_utils import logging_error
|
|
from amqp.model.model import AMQRoute
|
|
|
|
# record separator
|
|
RS = ":"
|
|
# null route, in lieu of None value
|
|
NULL_ROUTE = {
|
|
"componentName": "",
|
|
"exchange": None,
|
|
"queue": None,
|
|
"routingKey": "#",
|
|
"timeout": 1,
|
|
"validUntil": 0,
|
|
}
|
|
|
|
|
|
class AMQRouteFactory:
|
|
"""
|
|
Define the methods to create and validate AMQRoute objects.
|
|
"""
|
|
|
|
@staticmethod
|
|
def from_string(data):
|
|
"""
|
|
Builds the AMQRoute from its string form.
|
|
:param data: route string
|
|
:return: AMQRoute object
|
|
"""
|
|
try:
|
|
_keys = data.split(RS)
|
|
if len(_keys) >= 5:
|
|
_i = 0
|
|
_component_name = _keys[_i]
|
|
_i += 1
|
|
_exchange = _keys[_i]
|
|
_i += 1
|
|
_queue = _keys[_i] if _i < len(_keys) else ""
|
|
_i += 1
|
|
_routing_key = _keys[_i] if _i < len(_keys) else ""
|
|
_i += 1
|
|
_timeout = int(_keys[_i]) if _i < len(_keys) else 0
|
|
_i += 1
|
|
_valid_until = int(_keys[_i]) if _i < len(_keys) else 0
|
|
return AMQRoute(
|
|
component_name=_component_name,
|
|
exchange=_exchange,
|
|
queue=_queue,
|
|
key=_routing_key,
|
|
timeout=_timeout,
|
|
valid_until=_valid_until,
|
|
)
|
|
logging_error(
|
|
"AMQRoute incorrect format. AMQRoute expects input as: "
|
|
"'Component-Name:AMQExchange-Name:Queue-Name:Routing-Key:Timeout:Valid-Until', "
|
|
"got: ['%s']",
|
|
data,
|
|
)
|
|
except Exception as err:
|
|
logging_error(
|
|
"%s: AMQRoute incorrect format. AMQRoute expects input as: "
|
|
"'Component-Name:AMQExchange-Name:Queue-Name:Routing-Key:Timeout:Valid-Until', "
|
|
"got: ['%s']",
|
|
err,
|
|
data,
|
|
)
|
|
return NULL_ROUTE
|
|
|
|
@staticmethod
|
|
def validate(route_str):
|
|
"""
|
|
Validates the route string.
|
|
:param route_str: route string
|
|
:return: boolean validity indicator
|
|
"""
|
|
if route_str is not None:
|
|
return route_str.count(":") > 4
|
|
return False
|