From e698e9d2385267fb0cb2b89c7297dd64973175af Mon Sep 17 00:00:00 2001 From: Geary-Layne Date: Fri, 6 Dec 2024 13:38:02 -0700 Subject: [PATCH] fixed linter and added some more data checks --- .../idsse_common/idsse/common/path_builder.py | 27 ++++++++++++------- .../idsse/common/rabbitmq_utils.py | 10 +++---- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/python/idsse_common/idsse/common/path_builder.py b/python/idsse_common/idsse/common/path_builder.py index 9ebe628a..0cc4cf7b 100644 --- a/python/idsse_common/idsse/common/path_builder.py +++ b/python/idsse_common/idsse/common/path_builder.py @@ -387,18 +387,25 @@ def _get_parsed_arg_parts(self, path: str, fmt_str: str) -> dict: parsed_arg_parts = {} for path_part, fmt_part in zip(path_parts, fmt_parts): expected_len, lookup_info = self._lookup_dict[fmt_part] - if len(path_part) != expected_len: - raise ValueError('Some part of the path is not expected length. Passed part ' + if (part_len := len(path_part)) != expected_len: + raise ValueError('Path is not expected length. Passed path part ' f"'{path_part}' doesn't match format '{fmt_part}'") for lookup in lookup_info: - match lookup.type: - case self.INT: - parsed_arg_parts[lookup.key] = int(path_part[lookup.start:lookup.end]) - case self.FLOAT: - parsed_arg_parts[lookup.key] = float(path_part[lookup.start:lookup.end]) - case self.STR: - parsed_arg_parts[lookup.key] = path_part[lookup.start:lookup.end] - + if not (0 <= lookup.start <= part_len and 0 <= lookup.end <= part_len): + raise ValueError('Parse indices are out of range for path') + try: + match lookup.type: + case self.INT: + parsed_arg_parts[lookup.key] = int(path_part[lookup.start:lookup.end]) + case self.FLOAT: + parsed_arg_parts[lookup.key] = float(path_part[lookup.start:lookup.end]) + case self.STR: + parsed_arg_parts[lookup.key] = path_part[lookup.start:lookup.end] + except ValueError: + arg_str = path_part[lookup.start:lookup.end] + error_str = {self.INT: f"int('{arg_str}')", + self.FLOAT: f"float('{arg_str}')"}[lookup.type] + raise ValueError(f'Unable to apply formatting: {error_str}') return parsed_arg_parts def _apply_format(self, fmt_str: str, **kwargs) -> str: diff --git a/python/idsse_common/idsse/common/rabbitmq_utils.py b/python/idsse_common/idsse/common/rabbitmq_utils.py index 770a2907..71696753 100644 --- a/python/idsse_common/idsse/common/rabbitmq_utils.py +++ b/python/idsse_common/idsse/common/rabbitmq_utils.py @@ -143,7 +143,7 @@ def __init__( self._consumer_tags.append( self.channel.basic_consume(queue.name, partial(self._on_message, func=func), - # RMQ requires auto_ack=True to consume from Direct Reply-to + # RMQ requires auto_ack=True for Direct Reply-to auto_ack=queue.name == DIRECT_REPLY_QUEUE) ) @@ -221,7 +221,7 @@ def __init__( 'x-message-ttl': 10 * 1000}) _setup_exch_and_queue(self.channel, self._exch, self._queue) - elif self._exch.name != '': # if using default exchange, skip declaring (not allowed by RMQ) + elif self._exch.name != '': # if using default exchange, skip declare (not allowed by RMQ) _setup_exch(self.channel, self._exch) if self._exch.delivery_conf: @@ -560,8 +560,6 @@ def threadsafe_nack( threadsafe_call(channel, lambda: channel.basic_nack(delivery_tag, requeue=requeue)) - - def _initialize_exchange_and_queue(channel: Channel, params: RabbitMqParams) -> str: """Declare and bind RabbitMQ exchange and queue using the provided channel. @@ -634,7 +632,7 @@ def _setup_exch_and_queue(channel: Channel, exch: Exch, queue: Queue): queue.arguments['x-queue-type'] == 'quorum' and queue.auto_delete: raise ValueError('Quorum queues can not be configured to auto delete') - if exch.name != '': # if using default exchange, skip declaring (not allowed by RMQ) + if exch.name != '': # if using default exchange, skip declaring (not allowed by RMQ) _setup_exch(channel, exch) if queue.name == DIRECT_REPLY_QUEUE: @@ -652,7 +650,7 @@ def _setup_exch_and_queue(channel: Channel, exch: Exch, queue: Queue): queue_name = result.method.queue logger.debug('Declared queue: %s', queue_name) - if exch.name != '': # if using default exchange, skip binding queues (not allowed by RMQ) + if exch.name != '': # if using default exchange, skip binding queues (not allowed by RMQ) if isinstance(queue.route_key, list): for route_key in queue.route_key: channel.queue_bind(