Skip to content

Commit 7cc9747

Browse files
committed
Refactoring: move keyword matches to model.py
1 parent dfbe52e commit 7cc9747

File tree

3 files changed

+73
-69
lines changed

3 files changed

+73
-69
lines changed

robotframework_reportportal/helpers.py

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
import binascii
1818
import fnmatch
1919
import re
20-
from abc import ABC, abstractmethod
21-
from typing import Callable, Iterable, Optional, Tuple
22-
23-
from robotframework_reportportal.model import Keyword
20+
from typing import Iterable, Optional, Tuple
2421

2522

2623
def translate_glob_to_regex(pattern: Optional[str]) -> Optional[re.Pattern]:
@@ -51,66 +48,6 @@ def match_pattern(pattern: Optional[re.Pattern], line: Optional[str]) -> bool:
5148
return pattern.fullmatch(line) is not None
5249

5350

54-
class KeywordMatch(ABC):
55-
"""Base class for keyword matchers."""
56-
57-
@abstractmethod
58-
def match(self, kw: Keyword) -> bool:
59-
"""Check if the keyword matches the criteria."""
60-
61-
62-
class KeywordEqual(KeywordMatch):
63-
"""Match keyword based on a predicate."""
64-
65-
predicate: Callable[[Keyword], bool]
66-
67-
def __init__(self, predicate: Callable[[Keyword], bool] = None) -> None:
68-
"""Initialize the matcher with the predicate."""
69-
self.predicate = predicate
70-
71-
def match(self, kw: Keyword) -> bool:
72-
"""Check if the keyword matches the criteria."""
73-
return self.predicate(kw)
74-
75-
76-
class KeywordNameMatch(KeywordEqual):
77-
"""Match keyword based on the name pattern."""
78-
79-
def __init__(self, pattern: Optional[str]) -> None:
80-
"""Initialize the matcher with the pattern."""
81-
super().__init__(lambda kw: match_pattern(pattern, kw.name))
82-
83-
84-
class KeywordTypeEqual(KeywordEqual):
85-
"""Match keyword based on the type."""
86-
87-
def __init__(self, expected_value: Optional[str]) -> None:
88-
"""Initialize the matcher with the expected value."""
89-
super().__init__(lambda kw: kw.keyword_type == expected_value)
90-
91-
92-
class KeywordTagMatch(KeywordMatch):
93-
"""Match keyword based on the tag pattern."""
94-
95-
pattern: Optional[re.Pattern]
96-
97-
def __init__(self, pattern: Optional[str]) -> None:
98-
"""Initialize the matcher with the pattern."""
99-
self.pattern = translate_glob_to_regex(pattern)
100-
101-
def match(self, kw: Keyword) -> bool:
102-
"""Check if the keyword matches the criteria."""
103-
return next((True for t in kw.tags if match_pattern(self.pattern, t)), False)
104-
105-
106-
class KeywordStatusEqual(KeywordEqual):
107-
"""Match keyword based on the status."""
108-
109-
def __init__(self, status: str) -> None:
110-
"""Initialize the matcher with the status."""
111-
super().__init__(lambda kw: kw.status == status)
112-
113-
11451
def replace_patterns(text: str, patterns: Iterable[Tuple[re.Pattern, str]]) -> str:
11552
"""Replace given patterns in the text."""
11653
result = text

robotframework_reportportal/listener.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,19 @@
2525

2626
from reportportal_client.helpers import LifoQueue, guess_content_type_from_bytes, is_binary
2727

28-
from robotframework_reportportal.helpers import (
28+
from robotframework_reportportal.helpers import _unescape
29+
from robotframework_reportportal.model import (
30+
Entity,
31+
Keyword,
2932
KeywordMatch,
3033
KeywordNameMatch,
3134
KeywordTagMatch,
3235
KeywordTypeEqual,
33-
_unescape,
36+
Launch,
37+
LogMessage,
38+
Suite,
39+
Test,
3440
)
35-
from robotframework_reportportal.model import Entity, Keyword, Launch, LogMessage, Suite, Test
3641
from robotframework_reportportal.service import RobotService
3742
from robotframework_reportportal.static import MAIN_SUITE_ID, PABOT_WITHOUT_LAUNCH_ID_MSG
3843
from robotframework_reportportal.variables import Variables

robotframework_reportportal/model.py

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
"""This module contains models representing Robot Framework test items."""
1616

1717
import os
18-
from typing import Any, Dict, List, Optional, Union
18+
import re
19+
from abc import ABC, abstractmethod
20+
from typing import Any, Callable, Dict, List, Optional, Union
1921

2022
from reportportal_client.helpers import gen_attributes
2123

22-
from robotframework_reportportal.helpers import robot_markup_to_markdown
24+
from robotframework_reportportal.helpers import match_pattern, robot_markup_to_markdown, translate_glob_to_regex
2325

2426
TEST_CASE_ID_SIGN = "test_case_id:"
2527

@@ -332,3 +334,63 @@ def update(self, attributes: Dict[str, Any]) -> "Test":
332334
self.message = attributes.get("message")
333335
self.status = attributes.get("status")
334336
return self
337+
338+
339+
class KeywordMatch(ABC):
340+
"""Base class for keyword matchers."""
341+
342+
@abstractmethod
343+
def match(self, kw: Keyword) -> bool:
344+
"""Check if the keyword matches the criteria."""
345+
346+
347+
class KeywordEqual(KeywordMatch):
348+
"""Match keyword based on a predicate."""
349+
350+
predicate: Callable[[Keyword], bool]
351+
352+
def __init__(self, predicate: Callable[[Keyword], bool] = None) -> None:
353+
"""Initialize the matcher with the predicate."""
354+
self.predicate = predicate
355+
356+
def match(self, kw: Keyword) -> bool:
357+
"""Check if the keyword matches the criteria."""
358+
return self.predicate(kw)
359+
360+
361+
class KeywordNameMatch(KeywordEqual):
362+
"""Match keyword based on the name pattern."""
363+
364+
def __init__(self, pattern: Optional[str]) -> None:
365+
"""Initialize the matcher with the pattern."""
366+
super().__init__(lambda kw: match_pattern(pattern, kw.name))
367+
368+
369+
class KeywordTypeEqual(KeywordEqual):
370+
"""Match keyword based on the type."""
371+
372+
def __init__(self, expected_value: Optional[str]) -> None:
373+
"""Initialize the matcher with the expected value."""
374+
super().__init__(lambda kw: kw.keyword_type == expected_value)
375+
376+
377+
class KeywordTagMatch(KeywordMatch):
378+
"""Match keyword based on the tag pattern."""
379+
380+
pattern: Optional[re.Pattern]
381+
382+
def __init__(self, pattern: Optional[str]) -> None:
383+
"""Initialize the matcher with the pattern."""
384+
self.pattern = translate_glob_to_regex(pattern)
385+
386+
def match(self, kw: Keyword) -> bool:
387+
"""Check if the keyword matches the criteria."""
388+
return next((True for t in kw.tags if match_pattern(self.pattern, t)), False)
389+
390+
391+
class KeywordStatusEqual(KeywordEqual):
392+
"""Match keyword based on the status."""
393+
394+
def __init__(self, status: str) -> None:
395+
"""Initialize the matcher with the status."""
396+
super().__init__(lambda kw: kw.status == status)

0 commit comments

Comments
 (0)