Skip to content

Commit ffc0cd9

Browse files
AppStore app: show progress in debug log
1 parent baf00fe commit ffc0cd9

File tree

1 file changed

+14
-6
lines changed
  • internal_filesystem/builtin/apps/com.micropythonos.appstore/assets

1 file changed

+14
-6
lines changed

internal_filesystem/builtin/apps/com.micropythonos.appstore/assets/appstore.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def show_app_detail(self, app):
177177
intent.putExtra("appstore", self)
178178
self.startActivity(intent)
179179

180-
async def download_url(self, url, outfile=None):
180+
async def download_url(self, url, outfile=None, total_size=None):
181181
print(f"Downloading {url}")
182182
#await TaskManager.sleep(4) # test slowness
183183
try:
@@ -188,11 +188,13 @@ async def download_url(self, url, outfile=None):
188188
# Always use chunked downloading
189189
chunk_size = 1024
190190
print("headers:") ; print(response.headers)
191-
total_size = response.headers.get('Content-Length') # some servers don't send this
191+
if total_size is None:
192+
total_size = response.headers.get('Content-Length') # some servers don't send this in the headers
192193
print(f"download_url {'writing to ' + outfile if outfile else 'reading'} {total_size} bytes in chunks of size {chunk_size}")
193194

194195
fd = open(outfile, 'wb') if outfile else None
195196
chunks = [] if not outfile else None
197+
partial_size = 0
196198

197199
if fd:
198200
print("opened file...")
@@ -215,6 +217,8 @@ async def download_url(self, url, outfile=None):
215217
return False if outfile else None
216218

217219
if chunk:
220+
partial_size += len(chunk)
221+
print(f"progress: {partial_size} / {total_size} bytes")
218222
if fd:
219223
fd.write(chunk)
220224
else:
@@ -283,6 +287,7 @@ async def fetch_badgehub_app_details(self, app_obj):
283287
print(f"file has extension: {ext}")
284288
if ext == ".mpk":
285289
app_obj.download_url = file.get("url")
290+
app_obj.download_url_size = file.get("size_of_content")
286291
break # only one .mpk per app is supported
287292
except Exception as e:
288293
print(f"Could not get files from version: {e}")
@@ -476,7 +481,7 @@ def toggle_install(self, app_obj):
476481
label_text = self.install_label.get_text()
477482
if label_text == self.action_label_install:
478483
print("Starting install task...")
479-
TaskManager.create_task(self.download_and_install(download_url, f"apps/{fullname}", fullname))
484+
TaskManager.create_task(self.download_and_install(app_obj, f"apps/{fullname}"))
480485
elif label_text == self.action_label_uninstall or label_text == self.action_label_restore:
481486
print("Starting uninstall task...")
482487
TaskManager.create_task(self.uninstall_app(fullname))
@@ -487,7 +492,7 @@ def update_button_click(self, app_obj):
487492
print(f"Update button clicked for {download_url} and fullname {fullname}")
488493
self.update_button.add_flag(lv.obj.FLAG.HIDDEN)
489494
self.install_button.set_size(lv.pct(100), 40)
490-
TaskManager.create_task(self.download_and_install(download_url, f"apps/{fullname}", fullname))
495+
TaskManager.create_task(self.download_and_install(app_obj, f"apps/{fullname}"))
491496

492497
async def uninstall_app(self, app_fullname):
493498
self.install_button.add_state(lv.STATE.DISABLED)
@@ -508,7 +513,10 @@ async def uninstall_app(self, app_fullname):
508513
self.update_button.remove_flag(lv.obj.FLAG.HIDDEN)
509514
self.install_button.set_size(lv.pct(47), 40) # if a builtin app was removed, then it was overridden, and a new version is available, so make space for update button
510515

511-
async def download_and_install(self, zip_url, dest_folder, app_fullname):
516+
async def download_and_install(self, app_obj, dest_folder):
517+
zip_url = app_obj.download_url
518+
app_fullname = app_obj.fullname
519+
download_url_size = app_obj.download_url_size
512520
self.install_button.add_state(lv.STATE.DISABLED)
513521
self.install_label.set_text("Please wait...")
514522
self.progress_bar.remove_flag(lv.obj.FLAG.HIDDEN)
@@ -527,7 +535,7 @@ async def download_and_install(self, zip_url, dest_folder, app_fullname):
527535
self.progress_bar.set_value(40, True)
528536
temp_zip_path = "tmp/temp.mpk"
529537
print(f"Downloading .mpk file from: {zip_url} to {temp_zip_path}")
530-
result = await self.appstore.download_url(zip_url, outfile=temp_zip_path)
538+
result = await self.appstore.download_url(zip_url, outfile=temp_zip_path, total_size=download_url_size)
531539
if result is not True:
532540
print("Download failed...") # Would be good to show an error to the user if this failed...
533541
else:

0 commit comments

Comments
 (0)