Skip to content

Commit 76a9670

Browse files
Add Utils class with method to retrieve entrypoint function from call stack; refactor WebappInternal and PouiInternal classes to utilize new utility methods.
1 parent 31d446a commit 76a9670

File tree

4 files changed

+43
-31
lines changed

4 files changed

+43
-31
lines changed

tir/technologies/core/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from tir.technologies.core.config import ConfigLoader
2222
from tir.technologies.core.language import LanguagePack
2323
from tir.technologies.core.third_party.xpath_soup import xpath_soup
24+
from tir.technologies.core.utils import Utils
2425
from selenium.webdriver.firefox.options import Options as FirefoxOpt
2526
from selenium.webdriver.firefox.service import Service as FirefoxService
2627
from selenium.webdriver.chrome.options import Options as ChromeOpt
@@ -116,6 +117,7 @@ def __init__(self, config_path="", autostart=True):
116117
self.language = LanguagePack(self.config.language) if self.config.language else ""
117118
self.log = Log(folder=self.config.log_folder, config_path=self.config_path)
118119
self.log.station = socket.gethostname()
120+
self.utils = Utils()
119121
self.test_case = []
120122
self.last_test_case = None
121123
self.message = ""

tir/technologies/core/utils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import inspect
2+
import os
3+
from typing import Iterable, Optional, Set
4+
5+
6+
class Utils:
7+
"""Shared utility methods for TIR technologies."""
8+
9+
def get_main_entrypoint_from_stack(
10+
self,
11+
target_modules: Optional[Iterable[str]] = None,
12+
ignored_functions: Optional[Set[str]] = None,
13+
fallback: str = "function_name"
14+
) -> str:
15+
"""Return the external entrypoint function from call stack.
16+
17+
Looks for the first stack frame whose filename ends with one of target modules
18+
and whose function is not in ignored_functions.
19+
"""
20+
modules = tuple(target_modules) if target_modules else ("tir/main.py",)
21+
ignored = ignored_functions or {"__getattribute__", "_subscribe_routes", "__init__", "<lambda>"}
22+
23+
normalized_modules = tuple(
24+
os.path.normpath(module).replace("\\", "/").lower()
25+
for module in modules
26+
)
27+
28+
for stack_item in inspect.stack():
29+
filename = os.path.normpath(stack_item.filename).replace("\\", "/").lower()
30+
if any(filename.endswith(module) for module in normalized_modules) and stack_item.function not in ignored:
31+
return stack_item.function
32+
33+
return fallback

tir/technologies/poui_internal.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,20 +2056,6 @@ def search_for_errors(self, check_help=True):
20562056
self.restart_counter += 1
20572057
self.log_error(message)
20582058

2059-
def get_function_from_stack(self):
2060-
"""
2061-
[Internal]
2062-
2063-
Gets the function name that called the Webapp class from the call stack.
2064-
2065-
Usage:
2066-
2067-
>>> # Calling the method:
2068-
>>> self.get_function_from_stack()
2069-
"""
2070-
stack_item = next(iter(filter(lambda x: x.filename == self.config.routine, inspect.stack())), None)
2071-
return stack_item.function if stack_item and stack_item.function else "function_name"
2072-
20732059
def create_message(self, args, message_type=enum.MessageType.CORRECT):
20742060
"""
20752061
[Internal]

tir/technologies/webapp_internal.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,7 +1985,10 @@ def escape_to_main_menu(self):
19851985
success = menu_screen and container_layers
19861986

19871987
logger().debug(f'Check Menu Screen: {menu_screen}')
1988-
logger().debug(f'wa-dialog layers: {container_layers}')
1988+
logger().debug(f'wa-dialog layers: {container_layers}')
1989+
1990+
if not success:
1991+
self.log_error('Home screen not found!')
19891992

19901993
# wait trasitions between screens to avoid errors in layers number
19911994
self.wait_element_timeout(term=container_term, scrap_type=enum.ScrapType.CSS_SELECTOR,
@@ -4264,20 +4267,6 @@ def search_for_errors(self, check_help=True):
42644267
self.restart_counter += 1
42654268
self.log_error(message)
42664269

4267-
def get_function_from_stack(self):
4268-
"""
4269-
[Internal]
4270-
4271-
Gets the function name that called the Webapp class from the call stack.
4272-
4273-
Usage:
4274-
4275-
>>> # Calling the method:
4276-
>>> self.get_function_from_stack()
4277-
"""
4278-
stack_item = next(iter(filter(lambda x: x.filename == self.config.routine, inspect.stack())), None)
4279-
return stack_item.function if stack_item and stack_item.function else "function_name"
4280-
42814270
def create_message(self, args, message_type=enum.MessageType.CORRECT):
42824271
"""
42834272
[Internal]
@@ -8693,8 +8682,10 @@ def log_error(self, message, new_log_line=True, skip_restart=False, restart_coun
86938682
system_info()
86948683

86958684
stack_item = self.log.get_testcase_stack()
8696-
test_number = f"{stack_item.split('_')[-1]} -" if stack_item else ""
8697-
log_message = f"{test_number} {message}"
8685+
entrypoint_function = self.utils.get_main_entrypoint_from_stack()
8686+
entrypoint_prefix = f"[{entrypoint_function}] " if entrypoint_function and entrypoint_function != "function_name" else ""
8687+
test_number = f"{stack_item.split('_')[-1]} - " if stack_item else ""
8688+
log_message = f"{entrypoint_prefix}{test_number}{message}"
86988689
self.message = log_message
86998690
self.expected = False
87008691
self.log.seconds = self.log.set_seconds(self.log.initial_time)

0 commit comments

Comments
 (0)