Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
solarw committed Jan 22, 2024
1 parent e33c257 commit a38eae7
Show file tree
Hide file tree
Showing 7 changed files with 2,027 additions and 125 deletions.
1,965 changes: 1,965 additions & 0 deletions poetry.lock

Large diffs are not rendered by default.

73 changes: 23 additions & 50 deletions propel_client/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@
# ------------------------------------------------------------------------------
"""CLI implementation."""
import concurrent.futures
from contextlib import contextmanager
import json
import os
import time
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import Future, ThreadPoolExecutor
from dataclasses import dataclass
from functools import wraps
from pathlib import Path
from sys import stdin
from traceback import print_exc, print_tb
from typing import Any, Callable, Dict, Optional
from typing import Any, Callable, Dict, List, Optional

import click # type: ignore

Expand Down Expand Up @@ -125,8 +123,11 @@ def cli(
:param ctx: click context
:param url: base propel api url
:param http_retries: int num of http request retries
:param backoff_factor: flost factor for delays in retries
:param http_timeout: int request timeout in seconds
"""
ctx.url = url
ctx.url = url # type: ignore
storage = CredentialStorage()
propel_client = PropelClient(
url,
Expand Down Expand Up @@ -245,7 +246,7 @@ def keys_group(obj: ClickAPPObject) -> None:

@click.command(name="list")
@click.pass_obj
def keys_list(obj: ClickAPPObject) -> None:
def keys_list_command(obj: ClickAPPObject) -> None:
"""
List keys command.
Expand All @@ -267,7 +268,7 @@ def keys_create(obj: ClickAPPObject) -> None:
print_json(keys)


keys_group.add_command(keys_list)
keys_group.add_command(keys_list_command)
keys_group.add_command(keys_create)
cli.add_command(keys_group)

Expand Down Expand Up @@ -411,6 +412,8 @@ def agents_deploy( # pylint: disable=too-many-arguments
:param variables: optional str
:param tendermint_ingress_enabled: optional bool
:param timeout: int
:param do_restart: bool, restart after deployment
:param do_delete: bool, delete agents first
"""
ctx.invoke(seats_ensure)
if do_delete:
Expand All @@ -435,7 +438,7 @@ def agents_deploy( # pylint: disable=too-many-arguments
_restart_and_wait(ctx, name_or_id=name, timeout=timeout)


def _restart_and_wait(ctx, name_or_id: str, timeout: int):
def _restart_and_wait(ctx: click.Context, name_or_id: str, timeout: int) -> None:
name = name_or_id
click.echo(f"[Agent: {name}] agent restarting")
ctx.invoke(agents_restart, name_or_id=name)
Expand Down Expand Up @@ -614,42 +617,6 @@ def agents_variables_remove(
print_json(agent)


@click.command(name="variables-add")
@click.pass_obj
@click.argument("name_or_id", type=str, required=True)
@click.argument("variables", type=str, required=False)
def agents_variables_add(obj: ClickAPPObject, name_or_id: str, variables: str) -> None:
"""
Add variables to agent.
:param name_or_id: str
:param variables: str
:param obj: ClickAPPObject
"""
variables_list = variables.split(",") or [] if variables else []
agent = obj.propel_client.agents_variables_add(name_or_id, variables_list)
print_json(agent)


@click.command(name="variables-remove")
@click.pass_obj
@click.argument("name_or_id", type=str, required=True)
@click.argument("variables", type=str, required=False)
def agents_variables_remove(
obj: ClickAPPObject, name_or_id: str, variables: str
) -> None:
"""
Remove variables from agent.
:param name_or_id: str
:param variables: str
:param obj: ClickAPPObject
"""
variables_list = variables.split(",") or [] if variables else []
agent = obj.propel_client.agents_variables_remove(name_or_id, variables_list)
print_json(agent)


@click.command(name="delete")
@click.pass_obj
@click.argument("name_or_id", type=str, required=True)
Expand Down Expand Up @@ -768,7 +735,7 @@ def service_group(obj: ClickAPPObject) -> None:
@click.option("--tendermint-ingress-enabled", type=bool, required=False, default=False)
@click.option("--timeout", type=int, required=False, default=120)
@click.option("--show-variable-value", type=bool, required=False, default=False)
def service_deploy( # pylint: disable=too-many-arguments
def service_deploy( # pylint: disable=too-many-arguments,too-many-locals
ctx: click.Context,
keys: str,
name: str,
Expand Down Expand Up @@ -799,7 +766,9 @@ def service_deploy( # pylint: disable=too-many-arguments
f"Deploy {len(keys_list)} agents for service with variables {','.join(variable_names)}"
)

def _deploy(idx, key_id, executor):
def _deploy(
idx: int, key_id: int, executor: ThreadPoolExecutor
) -> tuple[Future[Any], str]:
agent_name = f"{name}_agent_{idx}"
click.echo(f"[Agent: {agent_name}] Deploying with keey id {key_id}")
f = executor.submit(
Expand All @@ -819,7 +788,9 @@ def _deploy(idx, key_id, executor):
)
return f, agent_name

def _delete(idx, _, executor):
def _delete(
idx: int, _: int, executor: ThreadPoolExecutor
) -> tuple[Future[Any], str]:
agent_name = f"{name}_agent_{idx}"
click.echo(f"[Agent: {agent_name}] deleting")
f = executor.submit(
Expand All @@ -830,7 +801,9 @@ def _delete(idx, _, executor):
)
return f, agent_name

def _restart(idx, key_id, executor):
def _restart(
idx: int, _: int, executor: ThreadPoolExecutor
) -> tuple[Future[None], str]:
agent_name = f"{name}_agent_{idx}"
click.echo(f"[Agent: {agent_name}] restarting")
f = executor.submit(
Expand All @@ -849,7 +822,7 @@ def _restart(idx, key_id, executor):
click.echo("All agents restarted")


def _run_agents_command(keys_list, fn):
def _run_agents_command(keys_list: List[int], fn: Callable) -> None:
with ThreadPoolExecutor(max_workers=len(keys_list)) as executor:
futures = {}
for idx, key_id in enumerate(keys_list):
Expand All @@ -860,7 +833,7 @@ def _run_agents_command(keys_list, fn):
agent_name = futures[future]
try:
future.result()
except Exception as exc:
except Exception as exc: # pylint: disable=broad-except
click.echo(f"ERROR: [Agent {agent_name}]: {repr(exc)}")
exceptions[agent_name] = exc
executor.shutdown(wait=False)
Expand Down
89 changes: 21 additions & 68 deletions propel_client/propel.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import requests
from requests import Response
from requests.adapters import HTTPAdapter, Retry

from propel_client import constants
from propel_client.cred_storage import CredentialStorage
Expand Down Expand Up @@ -56,16 +55,6 @@ def __init__(
super().__init__(message, *args)


class LogRetry(Retry):
"""
Adding extra logs before making a retry request
"""

def __init__(self, *args, **kwargs):
print("RETRY", args, kwargs)
super().__init__(*args, **kwargs)


class PropelClient:
"""Propel client."""

Expand All @@ -78,19 +67,22 @@ class PropelClient:
API_AGENTS_LIST = constants.AGENTS_LIST
API_VARIABLES_LIST = constants.VARIABLES_LIST

def __init__(
def __init__( # pylint: disable=too-many-arguments
self,
base_url: str,
credentials_storage: CredentialStorage,
retries: int = 10,
backoff_factor: float = 1.0,
timeout: float = 20,
backoff_factor: float = 0.1,
timeout: float = 60,
) -> None:
"""
Init client.
:param base_url: base propel http server url
:param credentials_storage: credential storage instance.
:param retries: int num of http request retries
:param backoff_factor: flost factor for delays in retries
:param timeout: int request timeout in seconds
"""
self.base_url = base_url
self.credentials_storage = credentials_storage
Expand Down Expand Up @@ -255,28 +247,34 @@ def agents_get(self, agent_name_or_id: Union[int, str]) -> Dict:
self._check_response(response)
return response.json()

def _http_get(self, *args, **kwargs) -> Response:
def _http_get(self, *args, **kwargs) -> Response: # type: ignore # pylint: disable=inconsistent-return-statements
"""Perform http get request."""
for i in range(self._retries):
try:
return self._http_session.get(*args, **kwargs, timeout=self._timeout)
except Exception as e:
print(f"Failed to perform get request: {args}: attempt {i+1} exception {e}")
time.sleep(self._backoff_factor * (2**i))
except Exception as e: # pylint: disable=broad-except
print(
f"Failed to perform get request: {args}: attempt {i+1} exception {e}"
)
if i < self._retries - 1:
time.sleep(self._backoff_factor * (2**i))
else:
raise

def _http_post(self, *args, **kwargs) -> Response:
def _http_post(self, *args, **kwargs) -> Response: # type: ignore # pylint: disable=inconsistent-return-statements
"""Perform http post request."""
for i in range(self._retries):
try:
return self._http_session.post(*args, **kwargs, timeout=self._timeout)
except Exception as e:
print(f"Failed to perform post request: {args}: attempt {i+1} exception {e}")
if i < self._retries -1:
except Exception as e: # pylint: disable=broad-except
print(
f"Failed to perform post request: {args}: attempt {i+1} exception {e}"
)
if i < self._retries - 1:
time.sleep(self._backoff_factor * (2**i))
else:
raise


def agents_restart(self, agent_name_or_id: Union[int, str]) -> Dict:
"""
Restart agent by name or id.
Expand Down Expand Up @@ -348,51 +346,6 @@ def agents_variables_remove(
self._check_response(response)
return response.json()

def agents_variables_add(
self, agent_name_or_id: Union[int, str], variables: List[str]
) -> Dict:
"""
Add variables to agent.
:param agent_name_or_id: str or int
:param variables: list of str
:return: dict
"""
url = (
self._get_url(self.API_AGENTS_LIST) + f"/{agent_name_or_id}/variables_add/"
)
response = requests.post(
url,
json={"variables": variables},
**self._get_credentials_params(),
allow_redirects=False,
)
self._check_response(response)
return response.json()

def agents_variables_remove(
self, agent_name_or_id: Union[int, str], variables: List[str]
) -> Dict:
"""
Remove variables from agent.
:param agent_name_or_id: str or int
:param variables: list of str
:return: dict
"""
url = (
self._get_url(self.API_AGENTS_LIST)
+ f"/{agent_name_or_id}/variables_remove/"
)

response = requests.post(
url, **self._get_credentials_params(), json={"variables": variables}
)
self._check_response(response)
return response.json()

def agents_delete(self, agent_name_or_id: Union[int, str]) -> Dict:
"""
Delete agent by name or id.
Expand Down
11 changes: 7 additions & 4 deletions propel_client/utils.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
"""Various utils."""
from itertools import chain
from pathlib import Path
from typing import Any, Generator

from aea.helpers.env_vars import ENV_VARIABLE_RE, is_env_variable
from autonomy.configurations.loader import load_service_config


def get_all_env_vars(d):
def get_all_env_vars(d: Any) -> Generator[tuple[str | Any, str | Any] | Any, Any, None]:
"""Get env vars from dict."""
for value in d.values():
if is_env_variable(value):
result = ENV_VARIABLE_RE.match(value)
_, var_name, _, _, default = result.groups()
result = ENV_VARIABLE_RE.match(value) # type: ignore
_, var_name, _, _, default = result.groups() # type: ignore
yield var_name, default
if isinstance(value, dict):
yield from get_all_env_vars(value)


def get_env_vars_for_service(service_path: Path):
def get_env_vars_for_service(
service_path: Path,
) -> set[tuple[str | Any, str | Any] | Any]:
"""Get env vars for service path."""
service = load_service_config(service_path)
return set(chain(*(get_all_env_vars(i) for i in service.overrides)))
2 changes: 1 addition & 1 deletion scripts/check_copyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2021-2023 Valory AG
# Copyright 2021-2024 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
7 changes: 6 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ application-import-names = propel_client,tests
# D202: blank lines
# B014: redundant exception
[pylint.BASIC]
good-names = e
good-names = e,f,i,d,fn

[pylint.FORMAT]
max-line-length = 120

[pycodestyle]
max-line-length = 120

[isort]
# for black compatibility
Expand Down
5 changes: 4 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ isolated_build = True
[deps-framework]
deps =
click==8.0.2
requests<3.0.0,>=2.28.2
requests<3.0.0,>=2.28.1
pytest
poetry
pytest-coverage
types-requests
open-autonomy==0.13.9.post1



[deps-tests]
Expand Down Expand Up @@ -147,6 +149,7 @@ skipsdist = True
skip_install = True
deps =
tomte[mypy]==0.2.3
open-autonomy==0.13.9.post1
commands =
pip install types-requests
mypy propel_client --disallow-untyped-defs
Expand Down

0 comments on commit a38eae7

Please sign in to comment.