Skip to content

Commit

Permalink
fix leaking keywords names
Browse files Browse the repository at this point in the history
Fixes #146
  • Loading branch information
aaltat committed Apr 5, 2024
1 parent 8b756a4 commit e648ae2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
14 changes: 9 additions & 5 deletions atest/SmallLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
from robot.api import logger
from robotlibcore import DynamicCore, keyword

class KeywordClass:

@keyword(name="Execute SomeThing")
def execute_something(self):
"""This is old"""
print("Name is here")

class SmallLibrary(DynamicCore):
"""Library documentation."""

Expand All @@ -12,10 +19,7 @@ def __init__(self, translation: Optional[Path] = None):
if not isinstance(translation, Path):
logger.warn("Convert to Path")
translation = Path(translation)
logger.warn(translation.absolute())
logger.warn(type(translation))

DynamicCore.__init__(self, [], translation.absolute())
DynamicCore.__init__(self, [KeywordClass()], translation.absolute())

@keyword(tags=["tag1", "tag2"])
def normal_keyword(self, arg: int, other: str) -> str:
Expand All @@ -32,7 +36,7 @@ def not_keyword(self, data: str) -> str:
print(data)
return data

@keyword(name="This Is New Name", tags=["tag1", "tag2"])
@keyword(name="Name ChanGed", tags=["tag1", "tag2"])
def name_changed(self, some: int, other: int) -> int:
"""This one too"""
print(f"{some} {type(some)}, {other} {type(other)}")
Expand Down
4 changes: 4 additions & 0 deletions atest/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@
,
"kw_not_translated": {
"doc": "Here is new doc"
},
"execute_something": {
"name": "tee_jotain",
"doc": "Uusi kirja."
}
}
28 changes: 20 additions & 8 deletions src/robotlibcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,47 @@ def _translation(translation: Optional[Path] = None):
return {}


def _translated_keywords(translation_data: dict) -> list:
return [item.get("name") for item in translation_data.values() if item.get("name")]


class HybridCore:
def __init__(self, library_components: List, translation: Optional[Path] = None) -> None:
self.keywords = {}
self.keywords_spec = {}
self.attributes = {}
translation_data = _translation(translation)
self.add_library_components(library_components, translation_data)
self.add_library_components([self], translation_data)
translated_kw_names = _translated_keywords(translation_data)
self.add_library_components(library_components, translation_data, translated_kw_names)
self.add_library_components([self], translation_data, translated_kw_names)
self.__set_library_listeners(library_components)

def add_library_components(self, library_components: List, translation: Optional[dict] = None):
def add_library_components(
self,
library_components: List,
translation: Optional[dict] = None,
translated_kw_names: Optional[list] = None,
):
translation = translation if translation else {}
translated_kw_names = translated_kw_names if translated_kw_names else []
self.keywords_spec["__init__"] = KeywordBuilder.build(self.__init__, translation) # type: ignore
self.__replace_intro_doc(translation)
for component in library_components:
for name, func in self.__get_members(component):
if callable(func) and hasattr(func, "robot_name"):
kw = getattr(component, name)
kw_name = self.__get_keyword_name(func, name, translation)
kw_name = self.__get_keyword_name(func, name, translation, translated_kw_names)
self.keywords[kw_name] = kw
self.keywords_spec[kw_name] = KeywordBuilder.build(kw, translation)
# Expose keywords as attributes both using original
# method names as well as possible custom names.
self.attributes[name] = self.attributes[kw_name] = kw

def __get_keyword_name(self, func: Callable, name: str, translation: dict):
if name in translation: # noqa: SIM102
if new_name := translation[name].get("name"):
return new_name
def __get_keyword_name(self, func: Callable, name: str, translation: dict, translated_kw_names: list):
if name in translated_kw_names:
return name
if name in translation and translation[name].get("name"):
return translation[name].get("name")
return func.robot_name or name

def __replace_intro_doc(self, translation: dict):
Expand Down
8 changes: 8 additions & 0 deletions utest/test_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ def test_kw_not_translated_but_doc_is(lib: SmallLibrary):
assert "kw_not_translated" in keywords
doc = lib.get_keyword_documentation("kw_not_translated")
assert doc == "Here is new doc"


def test_rf_name_not_in_keywords():
translation = Path(__file__).parent.parent / "atest" / "translation.json"
lib = SmallLibrary(translation=translation)
kw = lib.keywords
assert "Execute SomeThing" not in kw, f"Execute SomeThing should not be present: {kw}"
assert len(kw) == 6, f"Too many keywords: {kw}"

0 comments on commit e648ae2

Please sign in to comment.