2121import inspect
2222import os
2323import typing
24+ from dataclasses import dataclass
2425
2526from 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+
3041class 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+
85103class 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
0 commit comments