21
21
import inspect
22
22
import os
23
23
import typing
24
+ from dataclasses import dataclass
24
25
25
26
from robot .api .deco import keyword # noqa F401
27
+ from robot .utils import Importer # noqa F401
28
+ from robot .errors import DataError
26
29
27
30
__version__ = "3.0.1.dev1"
28
31
29
32
33
+ class PythonLibCoreException (Exception ):
34
+ pass
35
+
36
+
37
+ class PluginError (PythonLibCoreException ):
38
+ pass
39
+
40
+
30
41
class HybridCore :
31
42
def __init__ (self , library_components ):
32
43
self .keywords = {}
@@ -82,7 +93,15 @@ def get_keyword_names(self):
82
93
return sorted (self .keywords )
83
94
84
95
96
+ @dataclass
97
+ class Module :
98
+ module : str
99
+ args : list
100
+ kw_args : dict
101
+
102
+
85
103
class DynamicCore (HybridCore ):
104
+
86
105
def run_keyword (self , name , args , kwargs = None ):
87
106
return self .keywords [name ](* args , ** (kwargs or {}))
88
107
@@ -105,6 +124,9 @@ def get_keyword_types(self, name):
105
124
raise ValueError ('Keyword "%s" not found.' % name )
106
125
return spec .argument_types
107
126
127
+ def parse_plugins (self , plugins : str ) -> typing .List :
128
+ pass
129
+
108
130
def __get_keyword (self , keyword_name ):
109
131
if keyword_name == "__init__" :
110
132
return self .__init__
@@ -260,3 +282,44 @@ def __init__(self, argument_specification=None, documentation=None, argument_typ
260
282
self .argument_specification = argument_specification
261
283
self .documentation = documentation
262
284
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