Skip to content

Commit 86eae82

Browse files
committed
Plugin str to modules
1 parent 0877cbf commit 86eae82

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

src/robotlibcore.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,23 @@
2121
import inspect
2222
import os
2323
import typing
24+
from dataclasses import dataclass
2425

2526
from robot.api.deco import keyword # noqa F401
27+
from robot.utils import Importer # noqa F401
28+
from robot.errors import DataError
2629

2730
__version__ = "3.0.1.dev1"
2831

2932

33+
class PythonLibCoreException(Exception):
34+
pass
35+
36+
37+
class PluginError(PythonLibCoreException):
38+
pass
39+
40+
3041
class HybridCore:
3142
def __init__(self, library_components):
3243
self.keywords = {}
@@ -82,7 +93,15 @@ def get_keyword_names(self):
8293
return sorted(self.keywords)
8394

8495

96+
@dataclass
97+
class Module:
98+
module: str
99+
args: list
100+
kw_args: dict
101+
102+
85103
class DynamicCore(HybridCore):
104+
86105
def run_keyword(self, name, args, kwargs=None):
87106
return self.keywords[name](*args, **(kwargs or {}))
88107

@@ -105,6 +124,9 @@ def get_keyword_types(self, name):
105124
raise ValueError('Keyword "%s" not found.' % name)
106125
return spec.argument_types
107126

127+
def parse_plugins(self, plugins: str) -> typing.List:
128+
pass
129+
108130
def __get_keyword(self, keyword_name):
109131
if keyword_name == "__init__":
110132
return self.__init__
@@ -260,3 +282,44 @@ def __init__(self, argument_specification=None, documentation=None, argument_typ
260282
self.argument_specification = argument_specification
261283
self.documentation = documentation
262284
self.argument_types = argument_types
285+
286+
287+
class PluginParser:
288+
289+
def parse_plugins(self, plugins: str) -> typing.List:
290+
libraries = []
291+
importer = Importer("test library")
292+
for parsed_plugin in self._string_to_modules(plugins):
293+
plugin = importer.import_class_or_module(parsed_plugin.module)
294+
if not inspect.isclass(plugin):
295+
message = f"Importing test library: '{parsed_plugin.module}' failed."
296+
raise DataError(message)
297+
plugin = plugin(self, *parsed_plugin.args, **parsed_plugin.kw_args)
298+
if not isinstance(plugin, LibraryComponent):
299+
message = (
300+
"Plugin does not inherit SeleniumLibrary.base.LibraryComponent"
301+
)
302+
raise PluginError(message)
303+
self._store_plugin_keywords(plugin)
304+
libraries.append(plugin)
305+
return libraries
306+
307+
def _string_to_modules(self, modules):
308+
parsed_modules = []
309+
if not modules:
310+
return parsed_modules
311+
for module in modules.split(","):
312+
module = module.strip()
313+
module_and_args = module.split(";")
314+
module_name = module_and_args.pop(0)
315+
kw_args = {}
316+
args = []
317+
for argument in module_and_args:
318+
if "=" in argument:
319+
key, value = argument.split("=")
320+
kw_args[key] = value
321+
else:
322+
args.append(argument)
323+
module = Module(module=module_name, args=args, kw_args=kw_args)
324+
parsed_modules.append(module)
325+
return parsed_modules

utest/test_plugin_api.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import pytest
2+
3+
from robotlibcore import Module, PluginParser
4+
5+
6+
@pytest.fixture(scope="module")
7+
def plugin_parser() -> PluginParser:
8+
return PluginParser()
9+
10+
11+
def test_no_plugins_parsing(plugin_parser):
12+
for item in [None, ""]:
13+
assert plugin_parser._string_to_modules(item) == []
14+
15+
16+
def test_plugins_string_to_modules(plugin_parser):
17+
result = plugin_parser._string_to_modules("foo/bar.by")
18+
assert result == [Module("foo/bar.by", [], {})]
19+
result = plugin_parser._string_to_modules("path.to.MyLibrary,path.to.OtherLibrary")
20+
assert result == [
21+
Module("path.to.MyLibrary", [], {}),
22+
Module("path.to.OtherLibrary", [], {})
23+
]
24+
result = plugin_parser._string_to_modules("path.to.MyLibrary , path.to.OtherLibrary")
25+
assert result == [
26+
Module("path.to.MyLibrary", [], {}),
27+
Module("path.to.OtherLibrary", [], {})
28+
]
29+
result = plugin_parser._string_to_modules("path.to.MyLibrary;foo;bar , path.to.OtherLibrary;1")
30+
assert result == [
31+
Module("path.to.MyLibrary", ["foo", "bar"], {}),
32+
Module("path.to.OtherLibrary", ["1"], {})
33+
]
34+
result = plugin_parser._string_to_modules("PluginWithKwArgs.py;kw1=Text1;kw2=Text2")
35+
assert result == [
36+
Module("PluginWithKwArgs.py", [], {"kw1": "Text1", "kw2": "Text2"}),
37+
]

0 commit comments

Comments
 (0)