-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoBrewObject.py
89 lines (69 loc) · 3.32 KB
/
AutoBrewObject.py
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
from brewtils.models import Parameter
from brewtils import Plugin
import inspect
class AutoBrewObject:
def updateClientClass(self, client, name = None, version = None):
if name:
client._bg_name = name
else:
client._bg_name = client.__class__.__name__
if version:
client._bg_version = version
else:
client._bg_version = getattr(client, "__version__", None)
client._bg_commands = []
client._current_request = None
self.addFunctions(client)
return client
def addFunctions(self, client):
for func in dir(client):
if callable(getattr(client, func)):
if not func.startswith("_"):
# https://docs.python.org/3/library/inspect.html#inspect.signature
_wrapped = getattr(client, func)
signature = inspect.signature(_wrapped)
for func_parameter in signature.parameters:
func_parameter_value = signature.parameters[func_parameter]
key = func_parameter_value.name
if key == "self":
continue
func_parameter_value.default
typeValue = "String"
default = None
optional = False
is_kwarg = False
if str(func_parameter_value.annotation) in ["<class 'inspect._empty'>", "<class 'str'>"]:
pass
elif str(func_parameter_value.annotation) in ["<class 'int'>", "<class 'float'>"]:
typeValue = "Integer"
elif str(func_parameter_value.annotation) in ["<class 'object'>", "<class 'dict'>"]:
typeValue = "Dictionary"
if str(func_parameter_value.default) != "<class 'inspect._empty'>":
default = func_parameter_value.default
# TODO: Support kwargs
# if "kwargs" == key:
# is_kwarg = True
# optional = True
new_parameter = Parameter(
key=key,
type=typeValue,
multi=False,
display_name=key,
optional=optional,
default=default,
description=None,
choices=None,
parameters=None,
nullable=None,
maximum=None,
minimum=None,
regex=None,
form_input_type=None,
type_info=None,
is_kwarg=is_kwarg,
model=None,
)
_wrapped.parameters = getattr(_wrapped, "parameters", [])
_wrapped.parameters.append(new_parameter)
# TODO: Add description wrapper from Doc String
return client