forked from robotframework/PythonLibCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
74 lines (65 loc) · 2.91 KB
/
parser.py
File metadata and controls
74 lines (65 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Copyright 2017- Robot Framework Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import inspect
from typing import Any
from robot.errors import DataError # type: ignore
from robot.utils import Importer # type: ignore
from robotlibcore.core import DynamicCore
from robotlibcore.utils import Module, PluginError
class PluginParser:
def __init__(self, base_class: Any | None = None, python_object=None) -> None:
self._base_class = base_class
self._python_object = python_object if python_object else []
def parse_plugins(self, plugins: str | list[str]) -> list:
imported_plugins = []
importer = Importer("test library")
for parsed_plugin in self._string_to_modules(plugins):
plugin = importer.import_class_or_module(parsed_plugin.module)
if not inspect.isclass(plugin):
message = f"Importing test library: '{parsed_plugin.module}' failed."
raise DataError(message)
args = self._python_object + parsed_plugin.args
plugin = plugin(*args, **parsed_plugin.kw_args)
if self._base_class and not isinstance(plugin, self._base_class):
message = f"Plugin does not inherit {self._base_class}"
raise PluginError(message)
imported_plugins.append(plugin)
return imported_plugins
def get_plugin_keywords(self, plugins: list):
return DynamicCore(plugins).get_keyword_names()
def _string_to_modules(self, modules: str | list[str]):
parsed_modules: list = []
if not modules:
return parsed_modules
for module in self._modules_splitter(modules):
module_and_args = module.strip().split(";")
module_name = module_and_args.pop(0)
kw_args = {}
args = []
for argument in module_and_args:
if "=" in argument:
key, value = argument.split("=")
kw_args[key] = value
else:
args.append(argument)
parsed_modules.append(Module(module=module_name, args=args, kw_args=kw_args))
return parsed_modules
def _modules_splitter(self, modules: str | list[str]):
if isinstance(modules, str):
for module in modules.split(","):
yield module
else:
for module in modules:
yield module