Skip to content

Commit 1bfaed0

Browse files
committed
Rewrite helpers.match_with_asterisks function to match_with_glob_pattern function
1 parent 5051fc6 commit 1bfaed0

File tree

3 files changed

+16
-24
lines changed

3 files changed

+16
-24
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_asterisks` function, by @HardNorth
5+
- `helpers.match_with_glob_pattern` function, by @HardNorth
66
### Removed
77
- `Python 3.7` support, by @HardNorth
88

reportportal_client/helpers.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313

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

16+
import fnmatch
1617
import asyncio
1718
import inspect
1819
import logging
20+
import re
1921
import threading
2022
import time
2123
import uuid
@@ -487,10 +489,10 @@ def to_bool(value: Optional[Any]) -> Optional[bool]:
487489
raise ValueError(f'Invalid boolean value {value}.')
488490

489491

490-
def match_with_asterisks(pattern: Optional[str], line: Optional[str]) -> bool:
491-
"""Check if the line matches the pattern with asterisks.
492+
def match_with_glob_pattern(pattern: Optional[str], line: Optional[str]) -> bool:
493+
"""Check if the line matches given glob pattern.
492494
493-
:param pattern: pattern with asterisks
495+
:param pattern: glob pattern
494496
:param line: line to check
495497
:return: True if the line matches the pattern with asterisks, False otherwise
496498
"""
@@ -501,20 +503,5 @@ def match_with_asterisks(pattern: Optional[str], line: Optional[str]) -> bool:
501503
if line is None:
502504
return False
503505

504-
if '*' not in pattern:
505-
return pattern == line
506-
507-
pattern_parts = pattern.split('*')
508-
if pattern_parts[0] and not line.startswith(pattern_parts[0]):
509-
return False
510-
511-
pos = len(pattern_parts[0])
512-
for part in pattern_parts[1:-1]:
513-
if not part:
514-
continue
515-
pos = line.find(part, pos)
516-
if pos < 0:
517-
return False
518-
pos += len(part)
519-
520-
return line.endswith(pattern_parts[-1], pos)
506+
regex_pattern = fnmatch.translate(pattern)
507+
return re.fullmatch(regex_pattern, line) is not None

tests/test_helpers.py

Lines changed: 8 additions & 3 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_asterisks
24+
match_with_glob_pattern
2525
)
2626

2727

@@ -213,6 +213,11 @@ def test_to_bool_invalid_value():
213213
('l*e*1', 'l1ne11', True),
214214
('l*e*1', 'l1ne1', True),
215215
('l*e*2', 'l1ne1', False),
216+
('line?', 'line', False),
217+
('line?', 'line1', True),
218+
('line?', 'line11', False),
219+
('?line', 'line', False),
220+
('?line', '1line', True),
216221
])
217-
def test_match_with_asterisks(pattern: Optional[str], line: Optional[str], expected: bool):
218-
assert match_with_asterisks(pattern, line) == expected
222+
def test_match_with_glob_pattern(pattern: Optional[str], line: Optional[str], expected: bool):
223+
assert match_with_glob_pattern(pattern, line) == expected

0 commit comments

Comments
 (0)