Skip to content

Commit

Permalink
Rewrite match_with_glob_pattern function to `translate_glob_to_rege…
Browse files Browse the repository at this point in the history
…x` and `match_pattern` functions
  • Loading branch information
HardNorth committed Dec 11, 2024
1 parent 1bfaed0 commit 6e51b5c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 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_glob_pattern` function, by @HardNorth
- `helpers.match_pattern` and `helpers.translate_glob_to_regex` functions, by @HardNorth
### Removed
- `Python 3.7` support, by @HardNorth

Expand Down
22 changes: 18 additions & 4 deletions reportportal_client/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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
Expand All @@ -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
4 changes: 2 additions & 2 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_glob_pattern
match_pattern, translate_glob_to_regex
)


Expand Down Expand Up @@ -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

0 comments on commit 6e51b5c

Please sign in to comment.