From 6e51b5cfaa6ddd8d6341a7ad14fdae6c4dbd3c2d Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Wed, 11 Dec 2024 12:16:23 +0300 Subject: [PATCH] Rewrite `match_with_glob_pattern` function to `translate_glob_to_regex` and `match_pattern` functions --- CHANGELOG.md | 2 +- reportportal_client/helpers.py | 22 ++++++++++++++++++---- tests/test_helpers.py | 4 ++-- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b46b7a2..3c9162f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## [Unreleased] ### Added -- `helpers.match_with_glob_pattern` function, by @HardNorth +- `helpers.match_pattern` and `helpers.translate_glob_to_regex` functions, by @HardNorth ### Removed - `Python 3.7` support, by @HardNorth diff --git a/reportportal_client/helpers.py b/reportportal_client/helpers.py index 76b2805..4cd4974 100644 --- a/reportportal_client/helpers.py +++ b/reportportal_client/helpers.py @@ -13,8 +13,8 @@ """This module contains common functions-helpers of the client and agents.""" -import fnmatch import asyncio +import fnmatch import inspect import logging import re @@ -58,6 +58,8 @@ 'application/octet-stream': 'bin' }) +PATTERN_MATCHES_EMPTY_STRING: re.Pattern = re.compile('^$') + class LifoQueue(Generic[_T]): """Primitive thread-safe Last-in-first-out queue implementation.""" @@ -489,7 +491,20 @@ def to_bool(value: Optional[Any]) -> Optional[bool]: raise ValueError(f'Invalid boolean value {value}.') -def match_with_glob_pattern(pattern: Optional[str], line: Optional[str]) -> bool: +def translate_glob_to_regex(pattern: Optional[str]) -> Optional[re.Pattern[str]]: + """Translate glob string pattern to regex Pattern. + + :param pattern: glob pattern + :return: regex pattern + """ + if pattern is None: + return None + if pattern == '': + return PATTERN_MATCHES_EMPTY_STRING + return re.compile(fnmatch.translate(pattern)) + + +def match_pattern(pattern: Optional[re.Pattern[str]], line: Optional[str]) -> bool: """Check if the line matches given glob pattern. :param pattern: glob pattern @@ -503,5 +518,4 @@ def match_with_glob_pattern(pattern: Optional[str], line: Optional[str]) -> bool if line is None: return False - regex_pattern = fnmatch.translate(pattern) - return re.fullmatch(regex_pattern, line) is not None + return pattern.fullmatch(line) is not None diff --git a/tests/test_helpers.py b/tests/test_helpers.py index db6aca9..314167a 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -21,7 +21,7 @@ from reportportal_client.helpers import ( gen_attributes, get_launch_sys_attrs, to_bool, verify_value_length, ATTRIBUTE_LENGTH_LIMIT, TRUNCATE_REPLACEMENT, guess_content_type_from_bytes, is_binary, - match_with_glob_pattern + match_pattern, translate_glob_to_regex ) @@ -220,4 +220,4 @@ def test_to_bool_invalid_value(): ('?line', '1line', True), ]) def test_match_with_glob_pattern(pattern: Optional[str], line: Optional[str], expected: bool): - assert match_with_glob_pattern(pattern, line) == expected + assert match_pattern(translate_glob_to_regex(pattern), line) == expected