From 8767f92cfe1660ca624b678a9597f44093f67495 Mon Sep 17 00:00:00 2001 From: Jonathan Diamond Date: Thu, 28 Sep 2023 15:52:04 -0700 Subject: [PATCH] Add integer to find_matching_message_types check. --- python/fusion_engine_client/messages/defs.py | 73 +++++++++++--------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/python/fusion_engine_client/messages/defs.py b/python/fusion_engine_client/messages/defs.py index 80f6fea6..73a676b9 100644 --- a/python/fusion_engine_client/messages/defs.py +++ b/python/fusion_engine_client/messages/defs.py @@ -439,40 +439,45 @@ def find_matching_message_types(cls, pattern: Union[str, List[str]], return_clas # Now find matches to each pattern. result = set() for pattern in requested_types: - allow_multiple = '*' in pattern - re_pattern = pattern.replace('*', '.*') - # if pattern[0] != '^': - # re_pattern = r'.*' + re_pattern - # if pattern[-1] != '$': - # re_pattern += '.*' - - # Check for matches. - matched_types = [v for k, v in cls.message_type_by_name.items() - if re.match(re_pattern, k, flags=re.IGNORECASE)] - if len(matched_types) == 0: - _logger.warning("No message types matching pattern '%s'." % pattern) - continue - - # Check for exact matches with "Message" and "Measurement" suffixes removed. - if len(matched_types) > 1 and not allow_multiple: - def _remove_message_suffixes(s): - return _remove_suffixes(s.lower(), ['message', 'measurement', 'input', 'output']) - pattern_no_suffix = _remove_message_suffixes(pattern) - exact_matches = [t for t in matched_types - if _remove_message_suffixes(cls.message_type_to_class[t].__name__) == - pattern_no_suffix] - if len(exact_matches) == 1: - matched_types = exact_matches - - # If there are still too many matches, fail. - if len(matched_types) > 1 and not allow_multiple: - class_names = [cls.message_type_to_class[t].__name__ for t in matched_types] - raise ValueError("Pattern '%s' matches multiple message types:%s\n\nAdd a wildcard (%s*) to display " - "all matching types." % - (pattern, ''.join(['\n %s' % c for c in class_names]), pattern)) - # Otherwise, update the set of message types. - else: - result.update(matched_types) + # Check if pattern is the message integer value. + try: + int_val = int(pattern) + result.add(MessageType(int_val)) + except: + allow_multiple = '*' in pattern + re_pattern = pattern.replace('*', '.*') + # if pattern[0] != '^': + # re_pattern = r'.*' + re_pattern + # if pattern[-1] != '$': + # re_pattern += '.*' + + # Check for matches. + matched_types = [v for k, v in cls.message_type_by_name.items() + if re.match(re_pattern, k, flags=re.IGNORECASE)] + if len(matched_types) == 0: + _logger.warning("No message types matching pattern '%s'." % pattern) + continue + + # Check for exact matches with "Message" and "Measurement" suffixes removed. + if len(matched_types) > 1 and not allow_multiple: + def _remove_message_suffixes(s): + return _remove_suffixes(s.lower(), ['message', 'measurement', 'input', 'output']) + pattern_no_suffix = _remove_message_suffixes(pattern) + exact_matches = [t for t in matched_types + if _remove_message_suffixes(cls.message_type_to_class[t].__name__) == + pattern_no_suffix] + if len(exact_matches) == 1: + matched_types = exact_matches + + # If there are still too many matches, fail. + if len(matched_types) > 1 and not allow_multiple: + class_names = [cls.message_type_to_class[t].__name__ for t in matched_types] + raise ValueError("Pattern '%s' matches multiple message types:%s\n\nAdd a wildcard (%s*) to display " + "all matching types." % + (pattern, ''.join(['\n %s' % c for c in class_names]), pattern)) + # Otherwise, update the set of message types. + else: + result.update(matched_types) if return_class: result = {cls.message_type_to_class[t] for t in result}