From 5d777f23da72af26e83879ffadb96123508e1bb4 Mon Sep 17 00:00:00 2001 From: Marek Ventur Date: Fri, 31 Aug 2018 15:55:28 +0100 Subject: [PATCH] Badge Store: Update/Remove --- badge_store/main.py | 42 +++++++++++++++++++++++++++++------------- lib/app.py | 10 ++++------ lib/dialogs.py | 12 ++++++++---- lib/ospath.py | 17 +++++++++++++++++ 4 files changed, 58 insertions(+), 23 deletions(-) diff --git a/badge_store/main.py b/badge_store/main.py index 8fb5f42..31a9941 100644 --- a/badge_store/main.py +++ b/badge_store/main.py @@ -6,11 +6,11 @@ To publish apps use https://badge.emfcamp.org""" ___license___ = "MIT" ___title___ = "Badge Store" -___dependencies___ = ["badge_store", "dialogs", "ugfx_helper", "app", "database"] +___dependencies___ = ["badge_store", "dialogs", "ugfx_helper", "app", "database", "ospath"] ___categories___ = ["System"] ___bootstrapped___ = True -import ugfx_helper, os, database, wifi, app +import ugfx_helper, os, database, wifi, app, ospath from dialogs import * from lib.badge_store import BadgeStore @@ -25,9 +25,10 @@ store = BadgeStore(url=url, repo=repo, ref=ref) title = "TiLDA Badge Store" def clear(): - ugfx.clear(ugfx.html_color(0x7c1143)) + ugfx.clear() def show_categories(): + clear() with WaitingMessage(): menu_items = [{"title": c, "category": c} for c in store.get_categories()] @@ -39,6 +40,7 @@ def show_categories(): return def show_apps(c): + clear() menu_items = [{"title": a, "app": a} for a in store.get_apps(c)] option = prompt_option(menu_items, none_text="Back", title=title) @@ -49,6 +51,7 @@ def show_apps(c): return def show_app(a): + clear() with WaitingMessage(): app_info = store.get_app(a) @@ -61,37 +64,50 @@ def show_app(a): for i, installer in enumerate(installers): message.text = "%s (%s/%s)" % (installer.path, i + 1, n) 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(): - 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(): - 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(): while True: clear() - print() - menu_items = [ {"title": "Install Apps", "function": show_categories}, - {"title": "Update", "function": show_update}, - {"title": "Manage Apps", "function": show_remove} + {"title": "Update all Apps", "function": show_update}, + {"title": "Remove App", "function": show_remove} ] option = prompt_option(menu_items, none_text="Exit", text="What do you want to do?", title=title) if option: option["function"]() - else: - app.restart_to_default() + def _get_current_apps(): return [a.name for a in app.get_apps()] wifi.connect(show_wait_message=True) main_menu() -#show_app("launcher") +app.restart_to_default() diff --git a/lib/app.py b/lib/app.py index 35a82b2..b3b9531 100644 --- a/lib/app.py +++ b/lib/app.py @@ -58,12 +58,6 @@ class App: return self.attributes[attribute] return default - def uninstall(self): - try: - os.remove(self.name) - except Exception as e: - pass - def boot(self): write_launch_file(self.name) machine.reset() @@ -99,6 +93,10 @@ def get_apps(category=None): return [app for app in _apps if app.matches_category(category)] return _apps +def uncache_apps(): + global _apps + _apps = None + _categories = None def get_categories(): global _categories diff --git a/lib/dialogs.py b/lib/dialogs.py index ec01c61..7fea947 100644 --- a/lib/dialogs.py +++ b/lib/dialogs.py @@ -19,10 +19,10 @@ TILDA_COLOR = ugfx.html_color(0x7c1143); FONT_SMALL = 0 #todo: find correct values FONT_MEDIUM_BOLD = 0 -def notice(text, title="TiLDA", close_text="Close", width = 260, height = 180, 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) +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, 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 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: style = default_style_dialog 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() ugfx.set_default_font(font) window.text(5, 10, title, TILDA_COLOR) diff --git a/lib/ospath.py b/lib/ospath.py index 962b6ee..e2976af 100644 --- a/lib/ospath.py +++ b/lib/ospath.py @@ -17,6 +17,10 @@ F_OK = 0 def join(*args): # TODO: this is non-compliant + if not args: + return "" + if len(args) == 1: + return args[0] if type(args[0]) is bytes: return b"/".join(args) else: @@ -70,3 +74,16 @@ def makedirs(path): makedirs(sub_path) if not exists(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) +