Rewritten progresses in fetch to session, now they uses 1.0 dimensions instead of 100.0, updated tests

This commit is contained in:
MaXFeeD
2025-04-22 20:55:30 +03:00
parent aaf23791a2
commit 2f344e5bc9
3 changed files with 26 additions and 25 deletions
+3 -3
View File
@@ -235,8 +235,8 @@ def run_test():
async def update_progress():
texts = ["Downloading your BIOS...", "Comparing BIOS hashes...", "Removing previous BIOS...", "Flashing BIOS..."]
while True:
progress.update(progress.percentage + random(), texts[int(progress.percentage / 25)])
if progress.percentage >= 99:
progress.update(progress.percentage + random() / 100.0, texts[int(progress.percentage * 4)])
if progress.percentage >= 0.99:
progress.update(progress.percentage, "Something went terribly wrong!")
progress.style = "class:interrupted"
await asyncio.sleep(5)
@@ -244,7 +244,7 @@ def run_test():
progress.percentage = 0
else:
await asyncio.sleep(0.1)
intermediate_progress.text = texts[int(progress.percentage / 25)]
intermediate_progress.text = texts[int(progress.percentage * 4)]
async def main():
app = Application(
+11 -10
View File
@@ -8,7 +8,7 @@ from urllib.error import URLError
from urllib.response import addinfourl
from . import GLOBALS
from .shell import warn
from .shell import pretty_print_failure, warn
from .utils import ensure_file, name_to_identifier
@@ -125,12 +125,13 @@ def create_download_github_repository_request(repository: str, branch: str = "ma
def queue_download_request(url: str, data: Optional[bytes] = None, output_path: Optional[str] = None, /, placeholder: Optional[str] = None, timeout: float = 10, seconds_between_requests: float = 0.5, attempts: int = 2):
if not placeholder:
placeholder = url.rsplit("/", 1)[-1]
# progress = Progress(text=placeholder)
try:
_, fetch = create_download_request(url, data, placeholder=placeholder, timeout=timeout, seconds_between_requests=seconds_between_requests, attempts=attempts)
fetch(output_path, lambda received, size: None) # progress.notify(shell, progress, received / size, f"{placeholder} ({received / size / 1048576:.1f}%)")
# progress.notify(shell, progress, 1, placeholder)
except URLError as exc:
# Shell.notify(shell, f"#{exc.errno}: {exc.strerror}")
# Progress.notify(shell, progress, 1, "Check your network connection!")
pass
from .shell import InteractiveSession, Progress
with InteractiveSession(progress=Progress(text=placeholder)) as session:
try:
_, fetch = create_download_request(url, data, placeholder=placeholder, timeout=timeout, seconds_between_requests=seconds_between_requests, attempts=attempts)
fetch(output_path, lambda received, size: session["progress"].update(received / size, f"{placeholder} ({received / size / 1048576:.1f}%)"))
session["progress"].update(1.0, placeholder)
except URLError as exc:
pretty_print_failure(f"#{exc.errno}: {exc.strerror}")
session["progress"].update(1.0, "Check your network connection!")
session["progress"].style = "class:interrupted"
+12 -12
View File
@@ -634,11 +634,11 @@ class Progress(UIControl):
available_width = width
if self.display_states:
time_left = self.time_left()
percentage_text = f"{self.percentage:.1f}% "
percentage_text = f"{self.percentage * 100:.1f}% "
time_left_text = f" {format_timedelta(time_left) if time_left else 'N/A'}"
available_width = available_width - len(percentage_text) - len(time_left_text)
filled_progress_width = int(self.percentage / 100 * available_width)
filled_progress_width = int(self.percentage * available_width)
bar_text = self.text.center(available_width) if self.text else " " * available_width
if self.display_states:
@@ -681,7 +681,7 @@ class Progress(UIControl):
)
def update(self, percentage: float, text: Optional[str] = None) -> None:
self.percentage = max(0, min(100, percentage))
self.percentage = max(0, min(1.0, percentage))
if text is not None:
self.text = text
@@ -697,7 +697,7 @@ class Progress(UIControl):
elif self.done or self.stopped:
return timedelta(0)
else:
return self.time_elapsed() * (100 - self.percentage) / self.percentage
return self.time_elapsed() * (1.0 - self.percentage) / self.percentage
def __pt_container__(self) -> Container:
return self.window
@@ -877,14 +877,14 @@ def abort(*values: object, sep: Optional[str] = " ", code: int = 255, cause: Opt
if __name__ == "__main__":
preparing = Progress(intermediate=True)
progress = Progress(intermediate=True)
progress2 = Progress("I'm abobus", percentage=100.0)
progress2 = Progress("I'm abobus", percentage=1.0)
application = request_application(preparing, progress, progress2)
import asyncio
async def in_coroutine():
async def update_progress():
while progress.percentage < 100:
progress.update(progress.percentage + 1.5)
progress2.update(progress.percentage - 1.5)
while progress.percentage < 1.0:
progress.update(progress.percentage + 0.015)
progress2.update(progress.percentage - 0.015)
await asyncio.sleep(0.1)
application.exit()
application.create_background_task(update_progress())
@@ -893,8 +893,8 @@ if __name__ == "__main__":
clear_application(preparing, progress, progress2)
import time
with InteractiveSession(preparing=Progress(intermediate=True), progress=Progress("I'm aboba"), progress2=Progress("I'm abobus", percentage=100.0)) as session:
while session["progress"].percentage < 100:
session["progress"].update(session["progress"].percentage + 1.5)
session["progress2"].update(session["progress2"].percentage - 1.5)
with InteractiveSession(preparing=Progress(intermediate=True), progress=Progress("I'm aboba"), progress2=Progress("I'm abobus", percentage=1.0)) as session:
while session["progress"].percentage < 1.0:
session["progress"].update(session["progress"].percentage + 0.015)
session["progress2"].update(session["progress2"].percentage - 0.015)
time.sleep(0.1)