From 1d778a679ec1285969e8066b44aa2fab9ded42cd Mon Sep 17 00:00:00 2001 From: Hugo Herter Date: Thu, 12 Jan 2023 16:35:22 +0100 Subject: [PATCH] Fix: Use of black was not enforced (#1) --- .github/workflows/test-pytest.yml | 9 +- setup.cfg | 1 + src/aleph_client/__main__.py | 5 +- src/aleph_client/chains/common.py | 1 - src/aleph_client/chains/sol.py | 2 - src/aleph_client/commands/account.py | 8 +- src/aleph_client/commands/message.py | 88 +++++++++---------- src/aleph_client/commands/program.py | 124 +++++++++++++-------------- src/aleph_client/commands/utils.py | 3 +- src/aleph_client/conf.py | 6 +- src/aleph_client/synchronous.py | 57 +++++++----- src/aleph_client/vm/app.py | 11 ++- src/aleph_client/vm/cache.py | 8 +- 13 files changed, 175 insertions(+), 148 deletions(-) diff --git a/.github/workflows/test-pytest.yml b/.github/workflows/test-pytest.yml index 227798ec..eaa2bf3f 100644 --- a/.github/workflows/test-pytest.yml +++ b/.github/workflows/test-pytest.yml @@ -30,10 +30,15 @@ jobs: - name: Cache the image on GitHub's repository run: docker tag aleph-client:${GITHUB_REF##*/} docker.pkg.github.com/$GITHUB_REPOSITORY/aleph-client-build-cache && docker push docker.pkg.github.com/$GITHUB_REPOSITORY/aleph-client-build-cache || true - - name: Pytest in the Docker image + - name: Black in the Docker image run: | - docker run --entrypoint /opt/venv/bin/pytest aleph-client:${GITHUB_REF##*/} /opt/aleph-client/ + docker run --entrypoint /opt/venv/bin/black aleph-client:${GITHUB_REF##*/} --check /opt/aleph-client/src - name: MyPy in the Docker image run: | docker run --entrypoint /opt/venv/bin/mypy aleph-client:${GITHUB_REF##*/} --config-file /opt/aleph-client/mypy.ini /opt/aleph-client/src/ /opt/aleph-client/tests/ /opt/aleph-client/examples/ + + - name: Pytest in the Docker image + run: | + docker run --entrypoint /opt/venv/bin/pytest aleph-client:${GITHUB_REF##*/} /opt/aleph-client/ + diff --git a/setup.cfg b/setup.cfg index 3f44c3ed..51c7bed4 100644 --- a/setup.cfg +++ b/setup.cfg @@ -70,6 +70,7 @@ testing = requests aleph-pytezos==0.1.0 types-setuptools + black mqtt = aiomqtt certifi diff --git a/src/aleph_client/__main__.py b/src/aleph_client/__main__.py index 26c6e89e..1d9cf9c3 100644 --- a/src/aleph_client/__main__.py +++ b/src/aleph_client/__main__.py @@ -31,9 +31,8 @@ aggregate.app, name="aggregate", help="Manage aggregate messages on Aleph.im" ) -app.add_typer( - account.app, name="account", help="Manage account" -) +app.add_typer(account.app, name="account", help="Manage account") + @app.command() def whoami( diff --git a/src/aleph_client/chains/common.py b/src/aleph_client/chains/common.py index 6462d7f7..f82b2b5e 100644 --- a/src/aleph_client/chains/common.py +++ b/src/aleph_client/chains/common.py @@ -86,7 +86,6 @@ def get_fallback_private_key(path: Optional[Path] = None) -> bytes: with open(path, "rb") as prvfile: print(prvfile.read()) - default_key_path = path.parent / "default.key" if not default_key_path.is_symlink(): # Create a symlink to use this key by default diff --git a/src/aleph_client/chains/sol.py b/src/aleph_client/chains/sol.py index c7988cc0..98a8c1e3 100644 --- a/src/aleph_client/chains/sol.py +++ b/src/aleph_client/chains/sol.py @@ -79,10 +79,8 @@ def get_fallback_private_key(path: Optional[Path] = None) -> bytes: with open(path, "rb") as prvfile: print(prvfile.read()) - default_key_path = path.parent / "default.key" if not default_key_path.is_symlink(): # Create a symlink to use this key by default os.symlink(path, default_key_path) return private_key - diff --git a/src/aleph_client/commands/account.py b/src/aleph_client/commands/account.py index 79816f62..b67d54c9 100644 --- a/src/aleph_client/commands/account.py +++ b/src/aleph_client/commands/account.py @@ -17,9 +17,7 @@ @app.command() def create( - from_private_key: Optional[str] = typer.Option( - None, help=help_strings.PRIVATE_KEY - ), + from_private_key: Optional[str] = typer.Option(None, help=help_strings.PRIVATE_KEY), debug: bool = False, ): """Create or import a private key.""" @@ -27,7 +25,9 @@ def create( setup_logging(debug) typer.echo("Generating private key file.") - private_key_file = typer.prompt("Enter file in which to save the key", settings.PRIVATE_KEY_FILE) + private_key_file = typer.prompt( + "Enter file in which to save the key", settings.PRIVATE_KEY_FILE + ) if os.path.exists(private_key_file): typer.echo(f"Error: key already exists: '{private_key_file}'") diff --git a/src/aleph_client/commands/message.py b/src/aleph_client/commands/message.py index c805737c..6fb5675c 100644 --- a/src/aleph_client/commands/message.py +++ b/src/aleph_client/commands/message.py @@ -29,20 +29,20 @@ @app.command() def post( - path: Optional[Path] = typer.Option( - None, - help="Path to the content you want to post. If omitted, you can input your content directly", - ), - type: str = typer.Option("test", help="Text representing the message object type"), - ref: Optional[str] = typer.Option(None, help=help_strings.REF), - channel: str = typer.Option(settings.DEFAULT_CHANNEL, help=help_strings.CHANNEL), - private_key: Optional[str] = typer.Option( - settings.PRIVATE_KEY_STRING, help=help_strings.PRIVATE_KEY - ), - private_key_file: Optional[Path] = typer.Option( - settings.PRIVATE_KEY_FILE, help=help_strings.PRIVATE_KEY_FILE - ), - debug: bool = False, + path: Optional[Path] = typer.Option( + None, + help="Path to the content you want to post. If omitted, you can input your content directly", + ), + type: str = typer.Option("test", help="Text representing the message object type"), + ref: Optional[str] = typer.Option(None, help=help_strings.REF), + channel: str = typer.Option(settings.DEFAULT_CHANNEL, help=help_strings.CHANNEL), + private_key: Optional[str] = typer.Option( + settings.PRIVATE_KEY_STRING, help=help_strings.PRIVATE_KEY + ), + private_key_file: Optional[Path] = typer.Option( + settings.PRIVATE_KEY_FILE, help=help_strings.PRIVATE_KEY_FILE + ), + debug: bool = False, ): """Post a message on Aleph.im.""" @@ -96,14 +96,14 @@ def post( @app.command() def amend( - hash: str = typer.Argument(..., help="Hash reference of the message to amend"), - private_key: Optional[str] = typer.Option( - settings.PRIVATE_KEY_STRING, help=help_strings.PRIVATE_KEY - ), - private_key_file: Optional[Path] = typer.Option( - settings.PRIVATE_KEY_FILE, help=help_strings.PRIVATE_KEY_FILE - ), - debug: bool = False, + hash: str = typer.Argument(..., help="Hash reference of the message to amend"), + private_key: Optional[str] = typer.Option( + settings.PRIVATE_KEY_STRING, help=help_strings.PRIVATE_KEY + ), + private_key_file: Optional[Path] = typer.Option( + settings.PRIVATE_KEY_FILE, help=help_strings.PRIVATE_KEY_FILE + ), + debug: bool = False, ): """Amend an existing Aleph message.""" @@ -141,10 +141,10 @@ def amend( def forget_messages( - account: AccountFromPrivateKey, - hashes: List[str], - reason: Optional[str], - channel: str, + account: AccountFromPrivateKey, + hashes: List[str], + reason: Optional[str], + channel: str, ): try: result: ForgetMessage = synchronous.forget( @@ -161,20 +161,20 @@ def forget_messages( @app.command() def forget( - hashes: str = typer.Argument( - ..., help="Comma separated list of hash references of messages to forget" - ), - reason: Optional[str] = typer.Option( - None, help="A description of why the messages are being forgotten." - ), - channel: str = typer.Option(settings.DEFAULT_CHANNEL, help=help_strings.CHANNEL), - private_key: Optional[str] = typer.Option( - settings.PRIVATE_KEY_STRING, help=help_strings.PRIVATE_KEY - ), - private_key_file: Optional[Path] = typer.Option( - settings.PRIVATE_KEY_FILE, help=help_strings.PRIVATE_KEY_FILE - ), - debug: bool = False, + hashes: str = typer.Argument( + ..., help="Comma separated list of hash references of messages to forget" + ), + reason: Optional[str] = typer.Option( + None, help="A description of why the messages are being forgotten." + ), + channel: str = typer.Option(settings.DEFAULT_CHANNEL, help=help_strings.CHANNEL), + private_key: Optional[str] = typer.Option( + settings.PRIVATE_KEY_STRING, help=help_strings.PRIVATE_KEY + ), + private_key_file: Optional[Path] = typer.Option( + settings.PRIVATE_KEY_FILE, help=help_strings.PRIVATE_KEY_FILE + ), + debug: bool = False, ): """Forget an existing Aleph message.""" @@ -188,9 +188,9 @@ def forget( @app.command() def watch( - ref: str = typer.Argument(..., help="Hash reference of the message to watch"), - indent: Optional[int] = typer.Option(None, help="Number of indents to use"), - debug: bool = False, + ref: str = typer.Argument(..., help="Hash reference of the message to watch"), + indent: Optional[int] = typer.Option(None, help="Number of indents to use"), + debug: bool = False, ): """Watch a hash for amends and print amend hashes""" @@ -199,6 +199,6 @@ def watch( original: AlephMessage = synchronous.get_message(item_hash=ref) for message in synchronous.watch_messages( - refs=[ref], addresses=[original.content.address] + refs=[ref], addresses=[original.content.address] ): typer.echo(f"{message.json(indent=indent)}") diff --git a/src/aleph_client/commands/program.py b/src/aleph_client/commands/program.py index d48cb500..2e5712d0 100644 --- a/src/aleph_client/commands/program.py +++ b/src/aleph_client/commands/program.py @@ -36,62 +36,58 @@ @app.command() def upload( - path: Path = typer.Argument(..., help="Path to your source code"), - entrypoint: str = typer.Argument(..., help="Your program entrypoint"), - channel: str = typer.Option(settings.DEFAULT_CHANNEL, help=help_strings.CHANNEL), - memory: int = typer.Option( - settings.DEFAULT_VM_MEMORY, help="Maximum memory allocation on vm in MiB" - ), - vcpus: int = typer.Option( - settings.DEFAULT_VM_VCPUS, help="Number of virtual cpus to allocate." - ), - timeout_seconds: float = typer.Option( - settings.DEFAULT_VM_TIMEOUT, - help="If vm is not called after [timeout_seconds] it will shutdown", - ), - private_key: Optional[str] = typer.Option( - settings.PRIVATE_KEY_STRING, help=help_strings.PRIVATE_KEY - ), - private_key_file: Optional[Path] = typer.Option( - settings.PRIVATE_KEY_FILE, help=help_strings.PRIVATE_KEY_FILE - ), - print_messages: bool = typer.Option(False), - print_code_message: bool = typer.Option(False), - print_program_message: bool = typer.Option(False), - runtime: str = typer.Option( - None, - help="Hash of the runtime to use for your program. Defaults to aleph debian with Python3.8 and node. You can also create your own runtime and pin it", - ), - beta: bool = typer.Option(False), - - debug: bool = False, - persistent: bool = False, - persistent_volume: Optional[List[str]] = typer.Option( - None, - help='''Takes 3 parameters + path: Path = typer.Argument(..., help="Path to your source code"), + entrypoint: str = typer.Argument(..., help="Your program entrypoint"), + channel: str = typer.Option(settings.DEFAULT_CHANNEL, help=help_strings.CHANNEL), + memory: int = typer.Option( + settings.DEFAULT_VM_MEMORY, help="Maximum memory allocation on vm in MiB" + ), + vcpus: int = typer.Option( + settings.DEFAULT_VM_VCPUS, help="Number of virtual cpus to allocate." + ), + timeout_seconds: float = typer.Option( + settings.DEFAULT_VM_TIMEOUT, + help="If vm is not called after [timeout_seconds] it will shutdown", + ), + private_key: Optional[str] = typer.Option( + settings.PRIVATE_KEY_STRING, help=help_strings.PRIVATE_KEY + ), + private_key_file: Optional[Path] = typer.Option( + settings.PRIVATE_KEY_FILE, help=help_strings.PRIVATE_KEY_FILE + ), + print_messages: bool = typer.Option(False), + print_code_message: bool = typer.Option(False), + print_program_message: bool = typer.Option(False), + runtime: str = typer.Option( + None, + help="Hash of the runtime to use for your program. Defaults to aleph debian with Python3.8 and node. You can also create your own runtime and pin it", + ), + beta: bool = typer.Option(False), + debug: bool = False, + persistent: bool = False, + persistent_volume: Optional[List[str]] = typer.Option( + None, + help="""Takes 3 parameters A persistent volume is allocated on the host machine at any time eg: Use , to seperate the parameters and no spaces --persistent_volume persistence=host,name=my-volume,size=100 ./my-program main:app - '''), - - ephemeral_volume: Optional[List[str]] = typer.Option( - None, - help= - '''Takes 1 parameter Only + """, + ), + ephemeral_volume: Optional[List[str]] = typer.Option( + None, + help="""Takes 1 parameter Only Ephemeral volumes can move and be removed by the host,Garbage collected basically, when the VM isn't running eg: Use , to seperate the parameters and no spaces - --ephemeral-volume size_mib=100 ./my-program main:app '''), - - immutable_volume: Optional[List[str]] = typer.Option( - None, - help= - '''Takes 3 parameters + --ephemeral-volume size_mib=100 ./my-program main:app """, + ), + immutable_volume: Optional[List[str]] = typer.Option( + None, + help="""Takes 3 parameters Immutable volume is one whose contents do not change eg: Use , to seperate the parameters and no spaces --immutable-volume ref=25a393222692c2f73489dc6710ae87605a96742ceef7b91de4d7ec34bb688d94,use_latest=true,mount=/mnt/volume ./my-program main:app - ''' - ) - + """, + ), ): """Register a program to run on Aleph.im virtual machines from a zip archive.""" @@ -111,15 +107,19 @@ def upload( account: AccountFromPrivateKey = _load_account(private_key, private_key_file) runtime = ( - runtime - or input(f"Ref of runtime ? [{settings.DEFAULT_RUNTIME_ID}] ") - or settings.DEFAULT_RUNTIME_ID + runtime + or input(f"Ref of runtime ? [{settings.DEFAULT_RUNTIME_ID}] ") + or settings.DEFAULT_RUNTIME_ID ) volumes = [] # Check if the volumes are empty - if persistent_volume is None or ephemeral_volume is None or immutable_volume is None: + if ( + persistent_volume is None + or ephemeral_volume is None + or immutable_volume is None + ): for volume in prompt_for_volumes(): volumes.append(volume) typer.echo("\n") @@ -211,12 +211,12 @@ def upload( @app.command() def update( - hash: str, - path: Path, - private_key: Optional[str] = settings.PRIVATE_KEY_STRING, - private_key_file: Optional[Path] = settings.PRIVATE_KEY_FILE, - print_message: bool = True, - debug: bool = False, + hash: str, + path: Path, + private_key: Optional[str] = settings.PRIVATE_KEY_STRING, + private_key_file: Optional[Path] = settings.PRIVATE_KEY_FILE, + print_message: bool = True, + debug: bool = False, ): """Update the code of an existing program""" @@ -274,10 +274,10 @@ def update( @app.command() def unpersist( - hash: str, - private_key: Optional[str] = settings.PRIVATE_KEY_STRING, - private_key_file: Optional[Path] = settings.PRIVATE_KEY_FILE, - debug: bool = False, + hash: str, + private_key: Optional[str] = settings.PRIVATE_KEY_STRING, + private_key_file: Optional[Path] = settings.PRIVATE_KEY_FILE, + debug: bool = False, ): """Stop a persistent virtual machine by making it non-persistent""" diff --git a/src/aleph_client/commands/utils.py b/src/aleph_client/commands/utils.py index 97e32be7..91e80f32 100644 --- a/src/aleph_client/commands/utils.py +++ b/src/aleph_client/commands/utils.py @@ -80,10 +80,9 @@ def volume_to_dict(volume: List[str]) -> Optional[Dict[str, Union[str, int]]]: p = param.split("=") if p[1].isdigit(): dict_store[p[0]] = int(p[1]) - elif p[1] in ['True', 'true', 'False', 'false']: + elif p[1] in ["True", "true", "False", "false"]: dict_store[p[0]] = bool(p[1].capitalize()) else: dict_store[p[0]] = p[1] return dict_store - diff --git a/src/aleph_client/conf.py b/src/aleph_client/conf.py index d7390c60..0971f071 100644 --- a/src/aleph_client/conf.py +++ b/src/aleph_client/conf.py @@ -17,7 +17,7 @@ class Settings(BaseSettings): default=Path("ethereum.key"), description="Path to the private key used to sign messages", ) - + PRIVATE_KEY_STRING: Optional[str] = None API_HOST: str = "https://api2.aleph.im" MAX_INLINE_SIZE: int = 50000 @@ -60,4 +60,6 @@ class Config: assert settings.CONFIG_HOME if str(settings.PRIVATE_KEY_FILE) == "ethereum.key": - settings.PRIVATE_KEY_FILE = Path(settings.CONFIG_HOME, "private-keys", "ethereum.key") + settings.PRIVATE_KEY_FILE = Path( + settings.CONFIG_HOME, "private-keys", "ethereum.key" + ) diff --git a/src/aleph_client/synchronous.py b/src/aleph_client/synchronous.py index 359fafe8..f0fa0c69 100644 --- a/src/aleph_client/synchronous.py +++ b/src/aleph_client/synchronous.py @@ -1,7 +1,18 @@ import asyncio import queue import threading -from typing import Any, Callable, List, Optional, Dict, Iterable, Type, Protocol, TypeVar, Awaitable +from typing import ( + Any, + Callable, + List, + Optional, + Dict, + Iterable, + Type, + Protocol, + TypeVar, + Awaitable, +) from aiohttp import ClientSession from aleph_message.models import AlephMessage @@ -48,11 +59,11 @@ def func_caller(*args, **kwargs): def get_message( - item_hash: str, - message_type: Optional[Type[GenericMessage]] = None, - channel: Optional[str] = None, - session: Optional[ClientSession] = None, - api_server: str = settings.API_HOST, + item_hash: str, + message_type: Optional[Type[GenericMessage]] = None, + channel: Optional[str] = None, + session: Optional[ClientSession] = None, + api_server: str = settings.API_HOST, ) -> GenericMessage: return wrap_async(asynchronous.get_message)( item_hash=item_hash, @@ -64,23 +75,23 @@ def get_message( def create_program( - account: Account, - program_ref: str, - entrypoint: str, - runtime: str, - environment_variables: Optional[Dict[str, str]] = None, - storage_engine: StorageEnum = StorageEnum.storage, - channel: Optional[str] = None, - address: Optional[str] = None, - session: Optional[ClientSession] = None, - api_server: Optional[str] = None, - memory: Optional[int] = None, - vcpus: Optional[int] = None, - timeout_seconds: Optional[float] = None, - persistent: bool = False, - encoding: Encoding = Encoding.zip, - volumes: Optional[List[Dict]] = None, - subscriptions: Optional[List[Dict]] = None, + account: Account, + program_ref: str, + entrypoint: str, + runtime: str, + environment_variables: Optional[Dict[str, str]] = None, + storage_engine: StorageEnum = StorageEnum.storage, + channel: Optional[str] = None, + address: Optional[str] = None, + session: Optional[ClientSession] = None, + api_server: Optional[str] = None, + memory: Optional[int] = None, + vcpus: Optional[int] = None, + timeout_seconds: Optional[float] = None, + persistent: bool = False, + encoding: Encoding = Encoding.zip, + volumes: Optional[List[Dict]] = None, + subscriptions: Optional[List[Dict]] = None, ): """ Post a (create) PROGRAM message. diff --git a/src/aleph_client/vm/app.py b/src/aleph_client/vm/app.py index 9d264507..7a3bb14e 100644 --- a/src/aleph_client/vm/app.py +++ b/src/aleph_client/vm/app.py @@ -1,5 +1,14 @@ from dataclasses import dataclass -from typing import List, Dict, Callable, Awaitable, Optional, Any, MutableMapping, Mapping +from typing import ( + List, + Dict, + Callable, + Awaitable, + Optional, + Any, + MutableMapping, + Mapping, +) AsgiApplication = Callable diff --git a/src/aleph_client/vm/cache.py b/src/aleph_client/vm/cache.py index 64f021d3..08429ea7 100644 --- a/src/aleph_client/vm/cache.py +++ b/src/aleph_client/vm/cache.py @@ -49,7 +49,9 @@ class VmCache(BaseVmCache): cache: Dict[str, bytes] api_host: str - def __init__(self, session: Optional[ClientSession] = None, api_host: Optional[str] = None): + def __init__( + self, session: Optional[ClientSession] = None, api_host: Optional[str] = None + ): self.session = session or get_fallback_session() self.cache = {} self.api_host = api_host if api_host else settings.API_HOST @@ -74,7 +76,9 @@ async def set(self, key: str, value: Union[str, bytes]) -> Any: async def delete(self, key: str) -> Any: sanitized_key = sanitize_cache_key(key) - async with self.session.delete(f"{self.api_host}/cache/{sanitized_key}") as resp: + async with self.session.delete( + f"{self.api_host}/cache/{sanitized_key}" + ) as resp: resp.raise_for_status() return await resp.json()