From a4ceadd16be4f46846cefdc4a500305a52cfe3fb Mon Sep 17 00:00:00 2001 From: TheBurchLog <5104941+TheBurchLog@users.noreply.github.com> Date: Thu, 19 Oct 2023 18:43:34 +0000 Subject: [PATCH] formatting --- brewtils/decorators.py | 42 ++++++++++++++++++++++++---------- brewtils/rest/system_client.py | 16 ++++++------- test/decorators_test.py | 18 ++++++++------- 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/brewtils/decorators.py b/brewtils/decorators.py index 6335dd56..86799372 100644 --- a/brewtils/decorators.py +++ b/brewtils/decorators.py @@ -152,7 +152,10 @@ def echo_json(self, message): ) if output_type is None: - if str(inspect.signature(_wrapped)._return_annotation) in ["", ""]: + if str(inspect.signature(_wrapped)._return_annotation) in [ + "", + "", + ]: output_type = "JSON" else: output_type = "STRING" @@ -420,7 +423,8 @@ def _parse_method(method): # Need to initialize existing parameters before attempting to add parameters # pulled from the method signature. method_command.parameters = _initialize_parameters( - method_command.parameters + getattr(method, "parameters", []), method=method + method_command.parameters + getattr(method, "parameters", []), + method=method, ) # Add and update parameters based on the method signature @@ -429,7 +433,9 @@ def _parse_method(method): # Checks Docstring and Type Hints for undefined properties for cmd_parameter in method_command.parameters: if cmd_parameter.description is None: - cmd_parameter.description = _parameter_docstring(method, cmd_parameter.key) + cmd_parameter.description = _parameter_docstring( + method, cmd_parameter.key + ) # Verify that all parameters conform to the method signature _signature_validate(method_command, method) @@ -527,6 +533,7 @@ def _method_docstring(method): return docstring.split("\n")[0] if docstring else None + def _parameter_docstring(method, parameter): # type: (...) -> str """Parse out the description associated with parameter of the docstring from a method @@ -551,11 +558,14 @@ def _parameter_docstring(method, parameter): line = line.strip() for delimiter in delimiters: if delimiter in line: - if line.startswith(parameter + " ") or line.startswith(parameter + delimiter): + if line.startswith(parameter + " ") or line.startswith( + parameter + delimiter + ): return line.split(delimiter)[1].strip() return None + def _parameter_type_hint(method, cmd_parameter): for _, arg in enumerate(signature(method).parameters.values()): if arg.name == cmd_parameter: @@ -567,13 +577,13 @@ def _parameter_type_hint(method, cmd_parameter): return "Float" if str(arg.annotation) in [""]: return "Boolean" - if str(arg.annotation) in [ "", ""]: + if str(arg.annotation) in ["", ""]: return "Dictionary" if str(arg.annotation).lower() in [""]: return "DateTime" if str(arg.annotation) in [""]: return "Bytes" - + if hasattr(method, "func_doc"): docstring = method.func_doc else: @@ -601,6 +611,7 @@ def _parameter_type_hint(method, cmd_parameter): return "Bytes" return None + def _sig_info(arg): # type: (InspectParameter) -> Tuple[Any, bool] """Get the default and optionality of a method argument @@ -811,7 +822,9 @@ def _initialize_parameters(parameter_list, method=None): "Constructing a nested Parameters list using model class objects " "is deprecated. Please pass the model's parameter list directly." ) - initialized_params += _initialize_parameters(param.parameters, method=method) + initialized_params += _initialize_parameters( + param.parameters, method=method + ) # This is a dict of Parameter kwargs elif isinstance(param, dict): @@ -879,7 +892,10 @@ def _signature_parameters(cmd, method): if arg.name not in cmd.parameter_keys(): cmd.parameters.append( _initialize_parameter( - key=arg.name, default=sig_default, optional=sig_optional, method=method + key=arg.name, + default=sig_default, + optional=sig_optional, + method=method, ) ) @@ -931,11 +947,13 @@ def _signature_validate(cmd, method): if p.kind == InspectParameter.VAR_KEYWORD: has_kwargs = True - if _parameter_type_hint(method, param.key) and param.type != _parameter_type_hint(method, param.key): + if _parameter_type_hint( + method, param.key + ) and param.type != _parameter_type_hint(method, param.key): raise PluginParamError( - "Parameter Type assigned in the @parameter(type=?) does not match either the function Type Hint " - "or the Doc String definition. Please evaluate your type matching." - ) + "Parameter Type assigned in the @parameter(type=?) does not match either the function Type Hint " + "or the Doc String definition. Please evaluate your type matching." + ) # Couldn't find the parameter. That's OK if this parameter is meant to be part # of the **kwargs AND the function has a **kwargs parameter. diff --git a/brewtils/rest/system_client.py b/brewtils/rest/system_client.py index 56650a4f..3e18fd33 100644 --- a/brewtils/rest/system_client.py +++ b/brewtils/rest/system_client.py @@ -63,7 +63,7 @@ class SystemClient(object): system_namespaces: If the targeted system is stateless and if a collection of systems could handle the Request. This will allow the plugin to round robin the requests - to each namespace to help load balance the requests. It will rotate per + to each namespace to help load balance the requests. It will rotate per Request to the target system. Loading the System: @@ -161,7 +161,7 @@ class SystemClient(object): Args: system_name (str): Name of the System to make Requests on system_namespace (str): Namespace of the System to make Requests on - system_namespaces (list): Namespaces of the System to round robin Requests to. + system_namespaces (list): Namespaces of the System to round robin Requests to. The target System should be stateless. version_constraint (str): System version to make Requests on. Can be specific ('1.0.0') or 'latest'. @@ -245,9 +245,7 @@ def __init__(self, *args, **kwargs): self._system_namespace = kwargs.get( "system_namespace", brewtils.plugin.CONFIG.namespace or "" ) - self._system_namespaces = kwargs.get( - "system_namespaces", [] - ) + self._system_namespaces = kwargs.get("system_namespaces", []) # if both system namespaces are defined, combine the inputs if len(self._system_namespaces) > 0: @@ -255,7 +253,7 @@ def __init__(self, *args, **kwargs): if kwargs.get("system_namespace", None): if self._system_namespace not in self._system_namespaces: self._system_namespaces.append(self._system_namespace) - + elif len(self._system_namespaces) == 1: self._system_namespace = self._system_namespaces[0] self._current_system_namespace = -1 @@ -294,10 +292,12 @@ def bg_system(self): @property def bg_default_instance(self): return self._default_instance - + def _rotate_namespace(self): if self._current_system_namespace > -1: - self._system_namespace = self._system_namespaces[self._current_system_namespace] + self._system_namespace = self._system_namespaces[ + self._current_system_namespace + ] self._current_system_namespace += 1 if self._current_system_namespace == len(self._system_namespaces): self._current_system_namespace = 0 diff --git a/test/decorators_test.py b/test/decorators_test.py index 1ce7cb8e..583cb086 100644 --- a/test/decorators_test.py +++ b/test/decorators_test.py @@ -188,8 +188,9 @@ class TestTypeHint(object): def test_type_hints_parameter(self): @command - def cmd(foo:int): + def cmd(foo: int): return foo + bg_cmd = _parse_method(cmd) assert len(bg_cmd.parameters) == 1 @@ -200,20 +201,20 @@ def cmd(foo:int): def test_type_hints_output(self): @command - def cmd(foo:int) -> dict: + def cmd(foo: int) -> dict: return foo + bg_cmd = _parse_method(cmd) assert bg_cmd.output_type == "JSON" - - class TestDocString(object): + class TestDocString(object): def test_cmd_description(self): @command def cmd(foo): - """Default Command Description - """ + """Default Command Description""" return foo + bg_cmd = _parse_method(cmd) assert bg_cmd.description == "Default Command Description" @@ -227,10 +228,11 @@ def cmd(foo): foo : Parameter Description """ return foo + bg_cmd = _parse_method(cmd) assert len(bg_cmd.parameters) == 1 - assert bg_cmd.parameters[0].key == "foo" + assert bg_cmd.parameters[0].key == "foo" assert bg_cmd.parameters[0].description == "Parameter Description" def test_param_type(self): @@ -242,13 +244,13 @@ def cmd(foo): foo (int): Parameter Description """ return foo + bg_cmd = _parse_method(cmd) assert len(bg_cmd.parameters) == 1 assert bg_cmd.parameters[0].key == "foo" assert bg_cmd.parameters[0].type == "Integer" - class TestParameterReconciliation(object): """Test that the parameters line up correctly"""