From e56d52f854b64d5c6023f9b160742b7fd1a8794d Mon Sep 17 00:00:00 2001 From: Victor Date: Fri, 5 Sep 2014 13:18:33 +0200 Subject: [PATCH] Update appmanager.py --- appmanager.py | 312 +++++++++++++++++++++++++++----------------------- 1 file changed, 171 insertions(+), 141 deletions(-) diff --git a/appmanager.py b/appmanager.py index ee8f7b2..eae0d23 100644 --- a/appmanager.py +++ b/appmanager.py @@ -1,9 +1,8 @@ import ui import os -import sys import json -import apps import console +import apps def reload_all(mod, name): reload(mod) @@ -15,156 +14,187 @@ def reload_all(mod, name): reload_all(apps, "apps") -appsfln = "apps.json" -appsdir = "apps" +def save(fn, data): + with open(fn, "w") as fp: + json.dump(data, fp) +def load(fn): + with open(fn, "r") as fp: + data = json.load(fp) + final = {} + for appname, exts in data.items(): + final[appname] = [str(i) for i in exts] # ui doesn't like unicode -class Delegate (object): - def __init__(self, app): - self.app = app + return final - def tableview_did_select(self, tableview, section, row): - name = tableview.data_source.items[row] - self.app.showInfo(name) +appsfn = "apps.json" +appsdir = "apps" - def tableview_title_for_delete_button(self, tableview, section, row): - # Return the title for the 'swipe-to-***' button. - return 'Delete' +if not os.path.exists(appsfn): + save(appsfn, {}) +apps = load(appsfn) -class AppInfo (object): - def __init__(self, info): - self.setup(info) - - def savefunc(self, appname, app, view): - def wrapper(sender): - app.apps[appname] = view["extview"].data_source.items - - return wrapper +# Functions +@ui.in_background +def error(msg): + console.alert("Error", msg) - def makeLabel(self, name, frame): - label = ui.Label() - label.text = name - label.frame = frame - label.font = ("Courier", 18) +def makeView(name): + view = ui.View() + view.name = name + view.bg_color = 0.89 - return label + return view - def makeTable(self, exts, y): - lst = ui.ListDataSource([str(i) for i in exts]) - - table = ui.TableView() - table.name = "extview" - table.frame = (10, y, table.frame[2] - 5, 200) - table.flex = "W" - table.corner_radius = 10 - table.data_source = lst - - return table - - def setup(self, info): - view = ui.View() - view.name = info["appname"] - view.bg_color = 0.89 - - verlabel = self.makeLabel("Version: {}".format(info["version"]), - (10, 10, 500, 32)) - autlabel = self.makeLabel("Author: {}".format(info["author"]), - (10, 52, 500, 32)) - extlabel = self.makeLabel("Supported file formats: ", - (10, 94, 500, 32)) - table = self.makeTable(info["exts"], 136) - - app = info["appmng"] - button = app.makeButton("Save", (10, 368, 80, 32)) - button.action = self.savefunc(info["appname"], app, view) - - view.add_subview(verlabel) - view.add_subview(autlabel) - view.add_subview(extlabel) - view.add_subview(table) - view.add_subview(button) - self.view = view - self.view.present("sheet") - self.view.wait_modal() - - -class AppManager (object): - def __init__(self): - self.setup() - - def makeButton(self, title, frame): - button = ui.Button() - button.frame = frame - button.title = title - button.border_width = 1 - button.corner_radius = 5 - button.border_color = 0.9 - button.bg_color = 1 - button.tint_color = 0 - button.font = ("Courier", 15) - - return button - - def makeTable(self): - lst = ui.ListDataSource(self.apps.keys()) - delegate = Delegate(self) - - table = ui.TableView() - table.y = 55 - table.flex = "WH" - table.data_source = lst - table.delegate = delegate - - return table - - def getInfo(self, mod, appname): - info = {} - info["version"] = getattr(mod, "__version__", "Unknown") - info["author"] = getattr(mod, "__author__", "Unknown") - info["appname"] = appname - info["exts"] = self.apps[appname] - info["appmng"] = self - - return info - - def showInfo(self, appname): - appname = str(appname) - mod = getattr(__import__("apps", fromlist=[appname]), appname) - info = self.getInfo(mod, appname) +def makeButton(title, frame): + button = ui.Button() + button.frame = frame + button.title = title + button.border_width = 1 + button.corner_radius = 5 + button.border_color = 0.9 + button.bg_color = 1 + button.tint_color = 0 + button.font = ("Courier", 15) - app = AppInfo(info) + return button - def setup(self): - self.apps = self.load(appsfln) +def makeLabel(name, frame): + label = ui.Label() + label.text = name + label.frame = frame + label.font = ("Courier", 18) - view = ui.View() - view.name = "App Manager" - view.bg_color = 0.89 + return label - nbutton = self.makeButton("New", (10, 10, 80, 32)) - dbutton = self.makeButton("Download", (100, 10, 111, 32)) - - table = self.makeTable() - - view.add_subview(nbutton) - view.add_subview(dbutton) - view.add_subview(table) - self.view = view - self.view.present("sheet") - - def load(self, fn): - if not os.path.exists(fn): - self.save(fn, {}) - with open(fn) as fp: - data = json.load(fp) - return data +def makeExtTable(appname, exts, y): + lst = ExtList(appname, [str(i) for i in exts]) + + table = ui.TableView() + table.name = "extview" + table.frame = (10, y, table.frame[2] - 5, 200) + table.flex = "W" + table.corner_radius = 10 + table.data_source = lst + + return table + +def makeTable(): + lst = ListDataSource(apps.keys()) + delegate = Delegate() + + table = ui.TableView() + table.y = 55 + table.flex = "WH" + table.data_source = lst + table.delegate = delegate + + return table + +def getInfo(mod, appname): + version = getattr(mod, "__version__", "Unknown") + author = getattr(mod, "__author__", "Unknown") + appname = appname + exts = apps[appname] + + return version, author, appname, exts + +def showInfo(appname): + appname = str(appname) + try: + mod = getattr(__import__("apps", fromlist=[appname]), appname) + except AttributeError: + return error("App {} not found".format(appname)) + version, author, appname, exts = getInfo(mod, appname) + + view = makeView(appname) + + verlabel = makeLabel("Version: {}".format(version), + (10, 10, 500, 32)) + autlabel = makeLabel("Author: {}".format(author), + (10, 52, 500, 32)) + extlabel = makeLabel("Supported file formats: ", + (10, 94, 500, 32)) + table = makeExtTable(appname, exts, 136) - def save(self, fn, data): - with open(fn, "w") as fp: - json.dump(data, fp) - - -appmanager = AppManager() -appmanager.view.wait_modal() -appmanager.save(appsfln, appmanager.apps) + nbutton = makeButton("New", (10, 368, 80, 32)) + nbutton.action = newExt(table, appname) + + view.add_subview(verlabel) + view.add_subview(autlabel) + view.add_subview(extlabel) + view.add_subview(table) + view.add_subview(nbutton) + view.present("sheet") + +def newApp(table): + @ui.in_background + def wrapper(sender): + appname = console.input_alert("Enter application's name") + if appname: + apps[appname] = [] + table.data_source.items.append(appname) + + return wrapper + +def newExt(table, appname): + @ui.in_background + def wrapper(sender): + ext = console.input_alert("Enter extension") + if ext: + if not ext.startswith("."): + ext = "." + ext + apps[appname].append(ext) + table.data_source.items.append(ext) + + return wrapper + +@ui.in_background +def saveData(sender): + save(appsfn, apps) + +# Classes +class Delegate (object): + def tableview_did_select(self, tableview, section, row): + name = tableview.data_source.items[row] + showInfo(name) + +class ListDataSource (ui.ListDataSource): + def tableview_delete(self, tv, section, row): + self.reload_disabled = True + name = self.items[row] + del self.items[row] + self.reload_disabled = False + tv.delete_rows([row]) + del apps[name] + +class ExtList (ui.ListDataSource): + def __init__(self, appname, items=None): + ui.ListDataSource.__init__(self, items) + self.appname = appname + + def tableview_delete(self, tv, section, row): + self.reload_disabled = True + name = self.items[row] + del self.items[row] + self.reload_disabled = False + tv.delete_rows([row]) + apps[self.appname].remove(name) + +# Main frame +view = makeView("App Manager") + +nbutton = makeButton("New", (10, 10, 80, 32)) +dbutton = makeButton("Download", (100, 10, 111, 32)) +sbutton = makeButton("Save", (221, 10, 80, 32)) +table = makeTable() + +nbutton.action = newApp(table) +sbutton.action = saveData + +view.add_subview(nbutton) +view.add_subview(dbutton) +view.add_subview(sbutton) +view.add_subview(table) +view.present("sheet")