Skip to content

Commit a1bdee3

Browse files
committed
Add possibility to provide Python objects
1 parent b4a0f00 commit a1bdee3

File tree

6 files changed

+70
-2
lines changed

6 files changed

+70
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from robot.api.deco import keyword # noqa F401
2+
3+
from PluginWithPythonObjectsLib import BaseWithPython
4+
5+
6+
class MyPluginWithPythonObjects(BaseWithPython):
7+
8+
def __init__(self, py1, py2, rf1, rf2):
9+
self.rf1 = int(rf1)
10+
self.rf2 = int(rf2)
11+
super().__init__(py1, py2)
12+
13+
@keyword
14+
def plugin_keyword_with_python(self):
15+
return self.rf1 + self.rf2 + self.py1 + self.py2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from robot.api.deco import keyword # noqa F401
2+
3+
from robotlibcore import DynamicCore, PluginParser
4+
5+
6+
class BaseWithPython:
7+
def __init__(self, py1, py2):
8+
self.py1 = py1
9+
self.py2 = py2
10+
11+
12+
class PluginWithPythonObjectsLib(DynamicCore):
13+
14+
def __init__(self, plugins):
15+
plugin_parser = PluginParser(BaseWithPython, [8, 9])
16+
parsed_plugins = plugin_parser.parse_plugins(plugins)
17+
self._plugin_keywords = plugin_parser.get_plugin_keywords(plugins)
18+
DynamicCore.__init__(self, parsed_plugins)
19+
20+
@keyword
21+
def keyword_with_python(self):
22+
return "123"

atest/plugin_api/plugin_api.robot

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ Plugins With Base Class
1313
${value} = Base Keyword
1414
Should Be Equal ${value} 42
1515

16+
Plugins With Internal Python Objects
17+
Import Library ${CURDIR}/PluginWithPythonObjectsLib.py plugins=${CURDIR}/MyPluginWithPythonObjects.py;123;98
18+
${value} = Keyword With Python
19+
Should Be Equal ${value} 123
20+
${value} = Plugin Keyword With Python
21+
Should Be Equal ${value} ${238}
22+
1623
Pugins With No Base Class
1724
Run Keyword And Expect Error
1825
... *PluginError: Plugin does not inherit <class 'PluginWithBaseLib.BaseClass'>

src/robotlibcore.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,9 @@ def __init__(self, argument_specification=None, documentation=None, argument_typ
281281

282282

283283
class PluginParser:
284-
def __init__(self, base_class: Optional[Any] = None):
284+
def __init__(self, base_class: Optional[Any] = None, python_object: List[Any] = []):
285285
self._base_class = base_class
286+
self._python_object = python_object
286287

287288
def parse_plugins(self, plugins: str) -> List:
288289
imported_plugins = []
@@ -292,7 +293,8 @@ def parse_plugins(self, plugins: str) -> List:
292293
if not inspect.isclass(plugin):
293294
message = f"Importing test library: '{parsed_plugin.module}' failed."
294295
raise DataError(message)
295-
plugin = plugin(*parsed_plugin.args, **parsed_plugin.kw_args)
296+
args = self._python_object + parsed_plugin.args
297+
plugin = plugin(*args, **parsed_plugin.kw_args)
296298
if self._base_class and not isinstance(plugin, self._base_class):
297299
message = f"Plugin does not inherit {self._base_class}"
298300
raise PluginError(message)

utest/helpers/my_plugin_test.py

+12
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,15 @@ def another_keyword(self) -> int:
2828

2929
def normal_method(self):
3030
return "xxx"
31+
32+
33+
class TestPluginWithPythonArgs(LibraryBase):
34+
35+
def __init__(self, python_class, rf_arg):
36+
self.python_class = python_class
37+
self.rf_arg = rf_arg
38+
super().__init__()
39+
40+
@keyword
41+
def include_python_object(self):
42+
return self.python_class.x + self.python_class.y + int(self.rf_arg)

utest/test_plugin_api.py

+10
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,13 @@ def test_plugin_keywords(plugin_parser):
6464
assert len(keywords) == 2
6565
assert keywords[0] == "another_keyword"
6666
assert keywords[1] == "new_keyword"
67+
68+
69+
def test_plugin_python_objects():
70+
class PythonObject:
71+
x = 1
72+
y = 2
73+
python_object = PythonObject()
74+
parser = PluginParser(my_plugin_test.LibraryBase, [python_object])
75+
plugins = parser.parse_plugins("my_plugin_test.TestPluginWithPythonArgs;4")
76+
assert len(plugins)

0 commit comments

Comments
 (0)