Badge Store: Update/Remove

andrejusk/bday
Marek Ventur 2018-08-31 15:55:28 +01:00
parent 02d3c1512b
commit 5d777f23da
4 changed files with 58 additions and 23 deletions

View File

@ -6,11 +6,11 @@ To publish apps use https://badge.emfcamp.org"""
___license___ = "MIT" ___license___ = "MIT"
___title___ = "Badge Store" ___title___ = "Badge Store"
___dependencies___ = ["badge_store", "dialogs", "ugfx_helper", "app", "database"] ___dependencies___ = ["badge_store", "dialogs", "ugfx_helper", "app", "database", "ospath"]
___categories___ = ["System"] ___categories___ = ["System"]
___bootstrapped___ = True ___bootstrapped___ = True
import ugfx_helper, os, database, wifi, app import ugfx_helper, os, database, wifi, app, ospath
from dialogs import * from dialogs import *
from lib.badge_store import BadgeStore from lib.badge_store import BadgeStore
@ -25,9 +25,10 @@ store = BadgeStore(url=url, repo=repo, ref=ref)
title = "TiLDA Badge Store" title = "TiLDA Badge Store"
def clear(): def clear():
ugfx.clear(ugfx.html_color(0x7c1143)) ugfx.clear()
def show_categories(): def show_categories():
clear()
with WaitingMessage(): with WaitingMessage():
menu_items = [{"title": c, "category": c} for c in store.get_categories()] menu_items = [{"title": c, "category": c} for c in store.get_categories()]
@ -39,6 +40,7 @@ def show_categories():
return return
def show_apps(c): def show_apps(c):
clear()
menu_items = [{"title": a, "app": a} for a in store.get_apps(c)] menu_items = [{"title": a, "app": a} for a in store.get_apps(c)]
option = prompt_option(menu_items, none_text="Back", title=title) option = prompt_option(menu_items, none_text="Back", title=title)
@ -49,6 +51,7 @@ def show_apps(c):
return return
def show_app(a): def show_app(a):
clear()
with WaitingMessage(): with WaitingMessage():
app_info = store.get_app(a) app_info = store.get_app(a)
@ -61,37 +64,50 @@ def show_app(a):
for i, installer in enumerate(installers): for i, installer in enumerate(installers):
message.text = "%s (%s/%s)" % (installer.path, i + 1, n) message.text = "%s (%s/%s)" % (installer.path, i + 1, n)
installer.download() installer.download()
app.uncache_apps()
notice("App %s has been successfully installed" % a, title=title, close_text="Back") notice("App %s has been successfully installed" % a, title=title, close_text="Back")
def show_update(): def show_update():
None clear()
update = prompt_boolean("Do you want to update all apps on this badge?", title="Update", true_text="OK", false_text="Back")
if update:
clear()
with WaitingMessage(title=title, text="Please wait...") as message:
installers = store.install(_get_current_apps())
n = len(installers)
for i, installer in enumerate(installers):
message.text = "%s (%s/%s)" % (installer.path, i + 1, n)
installer.download()
notice("Your badge has been successfully updated", title=title, close_text="Back")
def show_remove(): def show_remove():
None clear()
app_to_remove = prompt_option(_get_current_apps(), none_text="Back", text="Select App to remove")
if app_to_remove:
ospath.recursive_rmdir(app_to_remove)
app.uncache_apps()
notice("%s has been removed" % app_to_remove, title=title, close_text="Back")
def main_menu(): def main_menu():
while True: while True:
clear() clear()
print()
menu_items = [ menu_items = [
{"title": "Install Apps", "function": show_categories}, {"title": "Install Apps", "function": show_categories},
{"title": "Update", "function": show_update}, {"title": "Update all Apps", "function": show_update},
{"title": "Manage Apps", "function": show_remove} {"title": "Remove App", "function": show_remove}
] ]
option = prompt_option(menu_items, none_text="Exit", text="What do you want to do?", title=title) option = prompt_option(menu_items, none_text="Exit", text="What do you want to do?", title=title)
if option: if option:
option["function"]() option["function"]()
else:
app.restart_to_default()
def _get_current_apps(): def _get_current_apps():
return [a.name for a in app.get_apps()] return [a.name for a in app.get_apps()]
wifi.connect(show_wait_message=True) wifi.connect(show_wait_message=True)
main_menu() main_menu()
#show_app("launcher") app.restart_to_default()

View File

@ -58,12 +58,6 @@ class App:
return self.attributes[attribute] return self.attributes[attribute]
return default return default
def uninstall(self):
try:
os.remove(self.name)
except Exception as e:
pass
def boot(self): def boot(self):
write_launch_file(self.name) write_launch_file(self.name)
machine.reset() machine.reset()
@ -99,6 +93,10 @@ def get_apps(category=None):
return [app for app in _apps if app.matches_category(category)] return [app for app in _apps if app.matches_category(category)]
return _apps return _apps
def uncache_apps():
global _apps
_apps = None
_categories = None _categories = None
def get_categories(): def get_categories():
global _categories global _categories

View File

@ -19,10 +19,10 @@ TILDA_COLOR = ugfx.html_color(0x7c1143);
FONT_SMALL = 0 #todo: find correct values FONT_SMALL = 0 #todo: find correct values
FONT_MEDIUM_BOLD = 0 FONT_MEDIUM_BOLD = 0
def notice(text, title="TiLDA", close_text="Close", width = 260, height = 180, font=FONT_SMALL, style=None): def notice(text, title="TiLDA", close_text="Close", font=FONT_SMALL, style=None):
prompt_boolean(text, title = title, true_text = close_text, false_text = None, width = width, height = height, font=font, style=style) prompt_boolean(text, title = title, true_text = close_text, false_text = None, font=font, style=style)
def prompt_boolean(text, title="TiLDA", true_text="Yes", false_text="No", width = 260, height = 180, font=FONT_SMALL, style=None): def prompt_boolean(text, title="TiLDA", true_text="Yes", false_text="No", font=FONT_SMALL, style=None):
"""A simple one and two-options dialog """A simple one and two-options dialog
if 'false_text' is set to None only one button is displayed. if 'false_text' is set to None only one button is displayed.
@ -32,7 +32,11 @@ def prompt_boolean(text, title="TiLDA", true_text="Yes", false_text="No", width
if style == None: if style == None:
style = default_style_dialog style = default_style_dialog
ugfx.set_default_font(FONT_MEDIUM_BOLD) ugfx.set_default_font(FONT_MEDIUM_BOLD)
window = ugfx.Container((ugfx.width() - width) // 2, (ugfx.height() - height) // 2, width, height)
width = ugfx.width() - 10
height = ugfx.height() - 10
window = ugfx.Container(5, 5, width, height)
window.show() window.show()
ugfx.set_default_font(font) ugfx.set_default_font(font)
window.text(5, 10, title, TILDA_COLOR) window.text(5, 10, title, TILDA_COLOR)

View File

@ -17,6 +17,10 @@ F_OK = 0
def join(*args): def join(*args):
# TODO: this is non-compliant # TODO: this is non-compliant
if not args:
return ""
if len(args) == 1:
return args[0]
if type(args[0]) is bytes: if type(args[0]) is bytes:
return b"/".join(args) return b"/".join(args)
else: else:
@ -70,3 +74,16 @@ def makedirs(path):
makedirs(sub_path) makedirs(sub_path)
if not exists(path): if not exists(path):
os.mkdir(path) os.mkdir(path)
def recursive_rmdir(path=""):
for s in os.listdir(path):
full = join(path, s)
if isdir(full):
try:
recursive_rmdir(full)
except:
pass
os.rmdir(full)
else:
os.remove(full)