forked from robotframework/PythonLibCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.py
More file actions
149 lines (130 loc) · 5.29 KB
/
builder.py
File metadata and controls
149 lines (130 loc) · 5.29 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# 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 Callable, get_type_hints
from .specification import KeywordSpecification
class KeywordBuilder:
@classmethod
def build(cls, function, translation: dict | None = None):
translation = translation if translation else {}
return KeywordSpecification(
argument_specification=cls._get_arguments(function),
documentation=cls.get_doc(function, translation),
argument_types=cls._get_types(function),
)
@classmethod
def get_doc(cls, function, translation: dict):
if kw := cls._get_kw_transtation(function, translation): # noqa: SIM102
if "doc" in kw:
return kw["doc"]
return inspect.getdoc(function) or ""
@classmethod
def _get_kw_transtation(cls, function, translation: dict):
return translation.get(function.__name__, {})
@classmethod
def unwrap(cls, function):
return inspect.unwrap(function)
@classmethod
def _get_arguments(cls, function):
unwrap_function = cls.unwrap(function)
arg_spec = cls._get_arg_spec(unwrap_function)
argument_specification = cls._get_args(arg_spec, function)
argument_specification.extend(cls._get_varargs(arg_spec))
argument_specification.extend(cls._get_named_only_args(arg_spec))
argument_specification.extend(cls._get_kwargs(arg_spec))
return argument_specification
@classmethod
def _get_arg_spec(cls, function: Callable) -> inspect.FullArgSpec:
return inspect.getfullargspec(function)
@classmethod
def _get_type_hint(cls, function: Callable):
try:
hints = get_type_hints(function)
except Exception: # noqa: BLE001
hints = function.__annotations__
return hints
@classmethod
def _get_args(cls, arg_spec: inspect.FullArgSpec, function: Callable) -> list:
args = cls._drop_self_from_args(function, arg_spec)
args.reverse()
defaults = list(arg_spec.defaults) if arg_spec.defaults else []
formated_args = []
for arg in args:
if defaults:
formated_args.append((arg, defaults.pop()))
else:
formated_args.append(arg)
formated_args.reverse()
return formated_args
@classmethod
def _drop_self_from_args(
cls,
function: Callable,
arg_spec: inspect.FullArgSpec,
) -> list:
return arg_spec.args[1:] if inspect.ismethod(function) else arg_spec.args
@classmethod
def _get_varargs(cls, arg_spec: inspect.FullArgSpec) -> list:
return [f"*{arg_spec.varargs}"] if arg_spec.varargs else []
@classmethod
def _get_kwargs(cls, arg_spec: inspect.FullArgSpec) -> list:
return [f"**{arg_spec.varkw}"] if arg_spec.varkw else []
@classmethod
def _get_named_only_args(cls, arg_spec: inspect.FullArgSpec) -> list:
rf_spec: list = []
kw_only_args = arg_spec.kwonlyargs if arg_spec.kwonlyargs else []
if not arg_spec.varargs and kw_only_args:
rf_spec.append("*")
kw_only_defaults = arg_spec.kwonlydefaults if arg_spec.kwonlydefaults else {}
for kw_only_arg in kw_only_args:
if kw_only_arg in kw_only_defaults:
rf_spec.append((kw_only_arg, kw_only_defaults[kw_only_arg]))
else:
rf_spec.append(kw_only_arg)
return rf_spec
@classmethod
def _get_types(cls, function):
if function is None:
return function
types = getattr(function, "robot_types", ())
if types is None or types:
return types
return cls._get_typing_hints(function)
@classmethod
def _get_typing_hints(cls, function):
function = cls.unwrap(function)
hints = cls._get_type_hint(function)
arg_spec = cls._get_arg_spec(function)
all_args = cls._args_as_list(function, arg_spec)
for arg_with_hint in list(hints):
# remove self statements
if arg_with_hint not in [*all_args, "return"]:
hints.pop(arg_with_hint)
return hints
@classmethod
def _args_as_list(cls, function, arg_spec) -> list:
function_args = cls._drop_self_from_args(function, arg_spec)
if arg_spec.varargs:
function_args.append(arg_spec.varargs)
function_args.extend(arg_spec.kwonlyargs or [])
if arg_spec.varkw:
function_args.append(arg_spec.varkw)
return function_args
@classmethod
def _get_defaults(cls, arg_spec):
if not arg_spec.defaults:
return {}
names = arg_spec.args[-len(arg_spec.defaults) :]
return zip(names, arg_spec.defaults, strict=False)