Skip to content

Commit

Permalink
Rewrite helpers.match_with_asterisks function to `match_with_glob_p…
Browse files Browse the repository at this point in the history
…attern` function
  • Loading branch information
HardNorth committed Dec 10, 2024
1 parent 5051fc6 commit 1bfaed0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## [Unreleased]
### Added
- `helpers.match_with_asterisks` function, by @HardNorth
- `helpers.match_with_glob_pattern` function, by @HardNorth
### Removed
- `Python 3.7` support, by @HardNorth

Expand Down
27 changes: 7 additions & 20 deletions reportportal_client/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@

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

import fnmatch
import asyncio
import inspect
import logging
import re
import threading
import time
import uuid
Expand Down Expand Up @@ -487,10 +489,10 @@ def to_bool(value: Optional[Any]) -> Optional[bool]:
raise ValueError(f'Invalid boolean value {value}.')


def match_with_asterisks(pattern: Optional[str], line: Optional[str]) -> bool:
"""Check if the line matches the pattern with asterisks.
def match_with_glob_pattern(pattern: Optional[str], line: Optional[str]) -> bool:
"""Check if the line matches given glob pattern.
:param pattern: pattern with asterisks
:param pattern: glob pattern
:param line: line to check
:return: True if the line matches the pattern with asterisks, False otherwise
"""
Expand All @@ -501,20 +503,5 @@ def match_with_asterisks(pattern: Optional[str], line: Optional[str]) -> bool:
if line is None:
return False

if '*' not in pattern:
return pattern == line

pattern_parts = pattern.split('*')
if pattern_parts[0] and not line.startswith(pattern_parts[0]):
return False

pos = len(pattern_parts[0])
for part in pattern_parts[1:-1]:
if not part:
continue
pos = line.find(part, pos)
if pos < 0:
return False
pos += len(part)

return line.endswith(pattern_parts[-1], pos)
regex_pattern = fnmatch.translate(pattern)
return re.fullmatch(regex_pattern, line) is not None
11 changes: 8 additions & 3 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_asterisks
match_with_glob_pattern
)


Expand Down Expand Up @@ -213,6 +213,11 @@ def test_to_bool_invalid_value():
('l*e*1', 'l1ne11', True),
('l*e*1', 'l1ne1', True),
('l*e*2', 'l1ne1', False),
('line?', 'line', False),
('line?', 'line1', True),
('line?', 'line11', False),
('?line', 'line', False),
('?line', '1line', True),
])
def test_match_with_asterisks(pattern: Optional[str], line: Optional[str], expected: bool):
assert match_with_asterisks(pattern, line) == expected
def test_match_with_glob_pattern(pattern: Optional[str], line: Optional[str], expected: bool):
assert match_with_glob_pattern(pattern, line) == expected

0 comments on commit 1bfaed0

Please sign in to comment.