Skip to content

Commit

Permalink
fix: replacing Yaspin with Simplified Spinners for Windows Systems (#369
Browse files Browse the repository at this point in the history
)

* feat: add a spinner to utils
  • Loading branch information
negar-abbasi authored Dec 14, 2023
1 parent 90c876b commit e12311e
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 23 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ auth0-python = "^4.4.0"
algokit-utils = "v2.1.0"
multiformats = "^0.2.1"
aiohttp = "3.9.1"
yaspin = "^3.0.1"

[tool.poetry.group.dev.dependencies]
pytest = "^7.2.0"
Expand Down
16 changes: 10 additions & 6 deletions src/algokit/cli/tasks/ipfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from pathlib import Path

import click
from yaspin import yaspin # type: ignore # noqa: PGH003

from algokit.core.tasks.ipfs import (
MAX_FILE_SIZE,
Expand All @@ -15,6 +14,7 @@
set_pinata_jwt,
upload_to_pinata,
)
from algokit.core.utils import run_with_animation

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -72,17 +72,21 @@ def logout_command() -> None:
def upload(file_path: Path, name: str | None) -> None:
pinata_jwt = get_pinata_jwt()
if not pinata_jwt:
raise click.ClickException("You are not logged in! Please login using `algokit ipfs login`.")
raise click.ClickException("You are not logged in! Please login using `algokit task ipfs login`.")

try:
total = file_path.stat().st_size
if total > MAX_FILE_SIZE:
raise click.ClickException("File size exceeds 100MB limit!")

with yaspin(text="Uploading", color="yellow") as spinner:
cid = upload_to_pinata(file_path, pinata_jwt, name)
spinner.ok("✅ ")
logger.info(f"File uploaded successfully!\nCID: {cid}")
def upload() -> str:
return upload_to_pinata(file_path, pinata_jwt, name)

cid = run_with_animation(
target_function=upload,
animation_text="Uploading",
)
logger.info(f"File uploaded successfully!\n CID: {cid}")

except click.ClickException as ex:
raise ex
Expand Down
2 changes: 1 addition & 1 deletion src/algokit/cli/tasks/mint.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def mint( # noqa: PLR0913

pinata_jwt = get_pinata_jwt()
if not pinata_jwt:
raise click.ClickException("You are not logged in! Please login using `algokit ipfs login`.")
raise click.ClickException("You are not logged in! Please login using `algokit task ipfs login`.")

client = load_algod_client(network)
validate_balance(
Expand Down
4 changes: 2 additions & 2 deletions src/algokit/core/tasks/ipfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def get_pinata_jwt() -> str | None:
old_api_key = keyring.get_password("algokit_web3_storage", "algokit_web3_storage_access_token")
if old_api_key:
logger.warning(
"You are using the old Web3 Storage API key. Please login again using `algokit ipfs login` with Pinata "
"ipfs provider. Follow the instructions on https://docs.pinata.cloud/docs/getting-started "
"You are using the old Web3 Storage API key. Please login again using `algokit task ipfs login` with "
"Pinata ipfs provider. Follow the instructions on https://docs.pinata.cloud/docs/getting-started"
"to create an account and obtain a JWT."
)
keyring.delete_password("algokit_web3_storage", "algokit_web3_storage_access_token")
Expand Down
50 changes: 50 additions & 0 deletions src/algokit/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import codecs
import re
import socket
import sys
import threading
import time
from collections.abc import Callable
from concurrent.futures import ThreadPoolExecutor
from typing import Any

CLEAR_LINE = "\033[K"


def extract_version_triple(version_str: str) -> str:
Expand All @@ -26,3 +35,44 @@ def is_network_available(host: str = "8.8.8.8", port: int = 53, timeout: float =
return True
except OSError:
return False


def animate(name: str, stop_event: threading.Event) -> None:
spinner = {
"interval": 100,
"frames": ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
}

while not stop_event.is_set():
for frame in spinner["frames"]: # type: ignore # noqa: PGH003
if stop_event.is_set():
break
try:
text = codecs.decode(frame, "utf-8")
except Exception:
text = frame
output = f"\r{text} {name}"
sys.stdout.write(output)
sys.stdout.write(CLEAR_LINE)
sys.stdout.flush()
time.sleep(0.001 * spinner["interval"]) # type: ignore # noqa: PGH003

sys.stdout.write("\r ")


def run_with_animation(target_function: Callable[[], Any], animation_text: str = "Loading") -> Any: # noqa: ANN401
with ThreadPoolExecutor(max_workers=2) as executor:
stop_event = threading.Event()
animation_future = executor.submit(animate, animation_text, stop_event)
function_future = executor.submit(target_function)

try:
result = function_future.result()
except Exception as e:
stop_event.set()
animation_future.result()
raise e
else:
stop_event.set()
animation_future.result()
return result
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@

⠋ UploadingHTTP Request: POST https://api.pinata.cloud/pinning/pinFileToIPFS "HTTP/1.1 500 Internal Server Error"


⠋ UploadingDEBUG: HTTP Request: POST https://api.pinata.cloud/pinning/pinFileToIPFS "HTTP/1.1 500 Internal Server Error"


DEBUG: Pinata error: 500. {"ok": false, "cid": "test"}
DEBUG: Pinata error: 500. {"ok": false, "cid": "test"}
Error: PinataInternalServerError('Pinata error: 500')
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@

⠋ UploadingHTTP Request: POST https://api.pinata.cloud/pinning/pinFileToIPFS "HTTP/1.1 200 OK"


⠋ UploadingDEBUG: HTTP Request: POST https://api.pinata.cloud/pinning/pinFileToIPFS "HTTP/1.1 200 OK"


✅ Uploading
File uploaded successfully!
CID: test
File uploaded successfully!
CID: test
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Enter the mnemonic phrase (25 words separated by whitespace):
Error: You are not logged in! Please login using `algokit ipfs login`.
Error: You are not logged in! Please login using `algokit task ipfs login`.

1 comment on commit e12311e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
src/algokit
   __init__.py15753%6–13, 17–24, 32–34
   __main__.py220%1–3
src/algokit/cli
   completions.py108298%83, 98
   deploy.py72790%44, 46, 92–94, 158, 182
   dispenser.py118199%75
   doctor.py48394%142–144
   explore.py501276%34–39, 41–46
   generate.py67396%74–75, 140
   goal.py39197%57
   init.py1901692%273–274, 324, 327–329, 340, 384, 410, 450, 459–461, 464–469, 482
   localnet.py93397%162, 183–184
src/algokit/cli/common
   utils.py26292%120, 123
src/algokit/cli/tasks
   assets.py821384%65–66, 72, 74–75, 105, 119, 125–126, 132, 134, 136–137
   ipfs.py51884%52, 80, 92, 94–95, 105–107
   mint.py66494%48, 70, 91, 250
   send_transaction.py651085%52–53, 57, 89, 158, 170–174
   sign_transaction.py59886%21, 28–30, 71–72, 109, 123
   transfer.py37392%25, 89, 116
   utils.py994555%26–34, 40–43, 75–76, 100–101, 125–133, 152–162, 209, 258–259, 279–290, 297–299
   vanity_address.py561082%41, 45–48, 112, 114, 121–123
   wallet.py79495%21, 66, 136, 162
src/algokit/core
   bootstrap.py1612485%103–104, 126, 149, 214, 217, 223–237, 246–251
   conf.py54885%10, 24, 28, 36, 38, 71–73
   deploy.py691184%61–64, 73–75, 79, 84, 91–93
   dispenser.py2022687%91, 123–124, 141–149, 191–192, 198–200, 218–219, 259–260, 318, 332–334, 345–346, 356, 369, 384
   doctor.py65789%67–69, 92–94, 134
   generate.py41295%69, 87
   goal.py60395%30–31, 41
   log_handlers.py68790%50–51, 63, 112–116, 125
   proc.py45198%98
   sandbox.py1991493%70, 116–123, 134, 429, 445, 470, 478
   typed_client_generation.py80594%55–57, 70, 75
   utils.py56296%36–37
   version_prompt.py72889%26–27, 39, 58–61, 79, 108
src/algokit/core/tasks
   ipfs.py63789%58–64, 140, 144, 146, 152
   nfd.py491373%25, 31, 34–41, 70–72, 99–101
   vanity_address.py903462%49–50, 54, 59–75, 92–108, 128–131
   wallet.py71593%37, 129, 155–157
src/algokit/core/tasks/mint
   mint.py781087%123–133, 187
   models.py901188%50, 52, 57, 71–74, 85–88
TOTAL322936289% 

Tests Skipped Failures Errors Time
382 0 💤 0 ❌ 0 🔥 14.355s ⏱️

Please sign in to comment.