Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBurchLog committed Oct 19, 2023
1 parent 5faad6c commit a4ceadd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 28 deletions.
42 changes: 30 additions & 12 deletions brewtils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ def echo_json(self, message):
)

if output_type is None:
if str(inspect.signature(_wrapped)._return_annotation) in ["<class 'object'>", "<class 'dict'>"]:
if str(inspect.signature(_wrapped)._return_annotation) in [
"<class 'object'>",
"<class 'dict'>",
]:
output_type = "JSON"
else:
output_type = "STRING"
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -567,13 +577,13 @@ def _parameter_type_hint(method, cmd_parameter):
return "Float"
if str(arg.annotation) in ["<class 'bool'>"]:
return "Boolean"
if str(arg.annotation) in [ "<class 'object'>", "<class 'dict'>"]:
if str(arg.annotation) in ["<class 'object'>", "<class 'dict'>"]:
return "Dictionary"
if str(arg.annotation).lower() in ["<class 'datetime'>"]:
return "DateTime"
if str(arg.annotation) in ["<class 'bytes'>"]:
return "Bytes"

if hasattr(method, "func_doc"):
docstring = method.func_doc
else:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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,
)
)

Expand Down Expand Up @@ -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.
Expand Down
16 changes: 8 additions & 8 deletions brewtils/rest/system_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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'.
Expand Down Expand Up @@ -245,17 +245,15 @@ 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:
self._current_system_namespace = 0
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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions test/decorators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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):
Expand All @@ -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"""

Expand Down

0 comments on commit a4ceadd

Please sign in to comment.