Skip to content

Commit 6e51b5c

Browse files
committed
Rewrite match_with_glob_pattern function to translate_glob_to_regex and match_pattern functions
1 parent 1bfaed0 commit 6e51b5c

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## [Unreleased]
44
### Added
5-
- `helpers.match_with_glob_pattern` function, by @HardNorth
5+
- `helpers.match_pattern` and `helpers.translate_glob_to_regex` functions, by @HardNorth
66
### Removed
77
- `Python 3.7` support, by @HardNorth
88

reportportal_client/helpers.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
"""This module contains common functions-helpers of the client and agents."""
1515

16-
import fnmatch
1716
import asyncio
17+
import fnmatch
1818
import inspect
1919
import logging
2020
import re
@@ -58,6 +58,8 @@
5858
'application/octet-stream': 'bin'
5959
})
6060

61+
PATTERN_MATCHES_EMPTY_STRING: re.Pattern = re.compile('^$')
62+
6163

6264
class LifoQueue(Generic[_T]):
6365
"""Primitive thread-safe Last-in-first-out queue implementation."""
@@ -489,7 +491,20 @@ def to_bool(value: Optional[Any]) -> Optional[bool]:
489491
raise ValueError(f'Invalid boolean value {value}.')
490492

491493

492-
def match_with_glob_pattern(pattern: Optional[str], line: Optional[str]) -> bool:
494+
def translate_glob_to_regex(pattern: Optional[str]) -> Optional[re.Pattern[str]]:
495+
"""Translate glob string pattern to regex Pattern.
496+
497+
:param pattern: glob pattern
498+
:return: regex pattern
499+
"""
500+
if pattern is None:
501+
return None
502+
if pattern == '':
503+
return PATTERN_MATCHES_EMPTY_STRING
504+
return re.compile(fnmatch.translate(pattern))
505+
506+
507+
def match_pattern(pattern: Optional[re.Pattern[str]], line: Optional[str]) -> bool:
493508
"""Check if the line matches given glob pattern.
494509
495510
:param pattern: glob pattern
@@ -503,5 +518,4 @@ def match_with_glob_pattern(pattern: Optional[str], line: Optional[str]) -> bool
503518
if line is None:
504519
return False
505520

506-
regex_pattern = fnmatch.translate(pattern)
507-
return re.fullmatch(regex_pattern, line) is not None
521+
return pattern.fullmatch(line) is not None

tests/test_helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from reportportal_client.helpers import (
2222
gen_attributes, get_launch_sys_attrs, to_bool,
2323
verify_value_length, ATTRIBUTE_LENGTH_LIMIT, TRUNCATE_REPLACEMENT, guess_content_type_from_bytes, is_binary,
24-
match_with_glob_pattern
24+
match_pattern, translate_glob_to_regex
2525
)
2626

2727

@@ -220,4 +220,4 @@ def test_to_bool_invalid_value():
220220
('?line', '1line', True),
221221
])
222222
def test_match_with_glob_pattern(pattern: Optional[str], line: Optional[str], expected: bool):
223-
assert match_with_glob_pattern(pattern, line) == expected
223+
assert match_pattern(translate_glob_to_regex(pattern), line) == expected

0 commit comments

Comments
 (0)