From 6bff62b48207eb1d532f21e9158e3573850f3f6c Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Fri, 29 Jul 2005 20:43:10 +0000 Subject: [PATCH] Restructuring. Added --show-help. git-svn-id: https://zero-install.svn.sourceforge.net/svnroot/zero-install/addapp@398 9f8c893c-44ee-0310-b757-c8ca8341c71e --- AddApp/AppRun | 62 ++++++++++++++++++------------------------------------ AddApp/help.py | 17 +++++++++++++++ AddApp/launcher.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 94 insertions(+), 46 deletions(-) create mode 100644 AddApp/help.py diff --git a/AddApp/AppRun b/AddApp/AppRun index 8ee5311..f4d248d 100755 --- a/AddApp/AppRun +++ b/AddApp/AppRun @@ -1,9 +1,7 @@ #!/usr/bin/env python -import os, sys, traceback +import os, sys import findrox; findrox.version(1, 9, 6) import rox -import urllib2 -import tempfile, shutil from rox import g, processes, saving from cStringIO import StringIO @@ -45,47 +43,27 @@ elif len(sys.argv) == 2: uri = sys.argv[1] if not try_interface(uri): sys.exit(1) +elif len(sys.argv) == 3 and sys.argv[1] == '--show-help': + import help + help.show_help(sys.argv[2]) + sys.exit(0) else: rox.alert("Multiple arguments to AddApp (%s)!" % `sys.argv[1:]`) sys.exit(1) -def get_icon(uri): - """Returns a GdkPixbuf icon for the app (downloading it first), or None.""" - from zeroinstall.injector import reader, model, basedir, namespaces - cached = basedir.load_first_cache(namespaces.config_site, 'interfaces', model.escape(uri)) - if not cached: - print >>sys.stderr, "Internal error: interface '%s' still not cached!" % uri - return None - from xml.dom import minidom - doc = minidom.parse(cached) - names = doc.documentElement.getElementsByTagNameNS(namespaces.XMLNS_IFACE, 'icon') - for x in names: - type = x.getAttribute('type') - if str(type) != 'image/png': - print "Skipping unknown icon type '%s'" % type - continue - icon_uri = x.getAttribute('href') - print "Downloading icon from", icon_uri - try: - icon_data = urllib2.urlopen(icon_uri) - except: - traceback.print_exc(file = sys.stderr) - return None - tmp = tempfile.NamedTemporaryFile() - shutil.copyfileobj(icon_data, tmp) - icon_data.close() - tmp.flush() - return tmp - return None - from launcher import AppLauncher -icon = get_icon(uri) -launcher = AppLauncher(uri, icon) -box = saving.SaveBox(launcher, os.path.basename(uri), 'inode/directory') -if icon: - image = g.Image() - icon_pixbuf = g.gdk.pixbuf_new_from_file(icon.name) - image.set_from_pixbuf(icon_pixbuf) - box.set_type('inode/directory', image) -box.show() -rox.mainloop() +try: + launcher = AppLauncher(uri) + box = saving.SaveBox(launcher, os.path.basename(uri), 'inode/directory') + + box.show() + g.gdk.flush() + launcher.get_icon() + if launcher.icon: + image = g.Image() + icon_pixbuf = g.gdk.pixbuf_new_from_file(launcher.icon.name) + image.set_from_pixbuf(icon_pixbuf) + box.set_type('inode/directory', image) + rox.mainloop() +except: + rox.report_exception() diff --git a/AddApp/help.py b/AddApp/help.py new file mode 100644 index 0000000..f54aaa4 --- /dev/null +++ b/AddApp/help.py @@ -0,0 +1,17 @@ +from zeroinstall.injector import policy +import rox, os +from rox import filer + +# Work-around for injector __slots__ bug +class Policy(policy.Policy): pass + +def show_help(uri): + p = Policy(uri) + p.recalculate() + iface = p.get_interface(uri) + assert iface.main + help = os.path.join(os.path.dirname(iface.main), 'Help') + if not os.path.isdir(help): + rox.alert('Sorry, this program has no documentation\n\n(%s directory missing)' % help) + else: + filer.open_dir(help) diff --git a/AddApp/launcher.py b/AddApp/launcher.py index 96032b7..2119881 100644 --- a/AddApp/launcher.py +++ b/AddApp/launcher.py @@ -1,11 +1,28 @@ +from zeroinstall.injector import policy import os, shutil +import rox from rox import saving from xml.dom import minidom +import urllib2 +import tempfile, shutil + +addapp_uri = "http://rox.sourceforge.net/2005/interfaces/AddApp" + +class Policy(policy.Policy): + pass class AppLauncher(saving.Saveable): - def __init__(self, uri, icon): + def __init__(self, uri): self.uri = uri - self.icon = icon + self.icon = None + + self.policy = Policy(uri) + self.policy.recalculate() + iface = self.policy.get_interface(uri) + if not iface.main: + rox.alert("Interface '%s' cannot be executed directly; it is just a library " + "to be used by other programs (or missing 'main' attribute on the " + "root element)." % iface.name) def save_to_file(self, path): os.mkdir(path) @@ -18,16 +35,20 @@ if [ "$*" = "--versions" ]; then exec 0launch -gd '%s' "$@" elif [ "$*" = "" ]; then exec 0launch '%s' --prompt +elif [ "$*" = "--help" ]; then + exec 0launch '%s' --show-help '%s' else exec 0launch '%s' "$@" -fi""" % (self.uri, self.uri, self.uri)) +fi""" % (self.uri, self.uri, addapp_uri, self.uri, self.uri)) else: apprun.write("""#!/bin/sh if [ "$*" = "--versions" ]; then exec 0launch -gd '%s' "$@" +elif [ "$*" = "--help" ]; then + exec 0launch '%s' --show-help '%s' else exec 0launch '%s' "$@" -fi""" % (self.uri, self.uri)) +fi""" % (self.uri, addapp_uri, self.uri, self.uri)) apprun.close() if self.icon: @@ -36,6 +57,9 @@ fi""" % (self.uri, self.uri)) doc = minidom.parseString(""" + + + @@ -45,3 +69,32 @@ fi""" % (self.uri, self.uri)) info_file = file(os.path.join(path, 'AppInfo.xml'), 'w') doc.writexml(info_file) info_file.close() + + def get_icon(self): + """Returns a GdkPixbuf icon for the app (downloading it first), or None.""" + from zeroinstall.injector import reader, model, basedir, namespaces + cached = basedir.load_first_cache(namespaces.config_site, 'interfaces', model.escape(self.uri)) + if not cached: + print >>sys.stderr, "Internal error: interface '%s' still not cached!" % self.uri + return None + from xml.dom import minidom + doc = minidom.parse(cached) + names = doc.documentElement.getElementsByTagNameNS(namespaces.XMLNS_IFACE, 'icon') + for x in names: + type = x.getAttribute('type') + if str(type) != 'image/png': + print "Skipping unknown icon type '%s'" % type + continue + icon_uri = x.getAttribute('href') + print "Downloading icon from", icon_uri + try: + icon_data = urllib2.urlopen(icon_uri) + except: + rox.report_exception() + return + tmp = tempfile.NamedTemporaryFile() + shutil.copyfileobj(icon_data, tmp) + icon_data.close() + tmp.flush() + + self.icon = tmp -- 2.11.4.GIT