From 2af230f089579b777058505c574c07a7a810f4ac Mon Sep 17 00:00:00 2001 From: TheBurchLog <5104941+TheBurchLog@users.noreply.github.com> Date: Fri, 13 Oct 2023 13:25:40 -0400 Subject: [PATCH] Add Auto Decorator (#408) Add Auto Decorator --- CHANGELOG.rst | 5 ++ brewtils/__version__.py | 2 +- brewtils/auto_decorator.py | 93 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 brewtils/auto_decorator.py diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 268450bc..08e83c2b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,11 @@ Brewtils Changelog ================== +3.18.0 +------ +10/13/2023 +- Add Auto Decorator for class objects + 3.17.0 ------ 10/11/2023 diff --git a/brewtils/__version__.py b/brewtils/__version__.py index eb20209b..d8d58178 100644 --- a/brewtils/__version__.py +++ b/brewtils/__version__.py @@ -1,3 +1,3 @@ # -*- coding: utf-8 -*- -__version__ = "3.17.0" +__version__ = "3.18.0" diff --git a/brewtils/auto_decorator.py b/brewtils/auto_decorator.py new file mode 100644 index 00000000..1ab5d7ed --- /dev/null +++ b/brewtils/auto_decorator.py @@ -0,0 +1,93 @@ +import inspect + +from brewtils.models import Parameter + + +class AutoDecorator: + def updateClientClass(self, client, name=None, version=None): + if name: + client._bg_name = name + else: + client._bg_name = getattr(client, "__name__", client.__class__.__name__) + + if version: + client._bg_version = version + else: + client._bg_version = getattr(client, "__version__", "0.0.0") + 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 [ + "", + "", + ]: + pass + elif str(func_parameter_value.annotation) in [""]: + typeValue = "Integer" + elif str(func_parameter_value.annotation) in [ + "" + ]: + typeValue = "Float" + elif str(func_parameter_value.annotation) in [""]: + typeValue = "Boolean" + elif str(func_parameter_value.annotation) in [ + "", + "", + ]: + typeValue = "Dictionary" + + if ( + str(func_parameter_value.default) + != "" + ): + default = func_parameter_value.default + + 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) + + return client