[kupfer] Use localized exceptions in execfile to notify users of errors



commit f88726a5beb1d97cc5ecc5c04890169451d66d39
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Sat Feb 13 15:53:09 2010 +0100

    Use localized exceptions in execfile to notify users of errors

 kupfer/execfile.py   |   23 +++++++++++++++++------
 kupfer/ui/browser.py |    7 ++++++-
 2 files changed, 23 insertions(+), 7 deletions(-)
---
diff --git a/kupfer/execfile.py b/kupfer/execfile.py
index 2daacf8..c549d94 100644
--- a/kupfer/execfile.py
+++ b/kupfer/execfile.py
@@ -11,6 +11,9 @@ from kupfer import conspickle
 
 KUPFER_COMMAND_SHEBANG="#!/usr/bin/env kupfer-exec\n"
 
+class ExecutionError (Exception):
+	pass
+
 def execute_file(filepath):
 	"""Execute serialized command inside @filepath
 
@@ -18,14 +21,16 @@ def execute_file(filepath):
 	>>> execute_file(__file__)  # doctest: +ELLIPSIS
 	Traceback (most recent call last):
 	    ...
-	OSError: ... is not executable
+	ExecutionError: ... (not executable)
 	"""
 	if not os.path.exists(filepath):
-		raise IOError("%s does not exist" % (filepath, ))
+		raise IOError('"%s" does not exist' % (filepath, ))
 	if not os.access(filepath, os.X_OK):
-		raise OSError("%s is not executable" % (filepath, ))
-	data = open(filepath).read()
+		raise ExecutionError(_('No permission to run "%s" (not executable)') %
+				glib.filename_display_basename(filepath))
+
 	# strip shebang away
+	data = open(filepath).read()
 	if data.startswith("#!") and "\n" in data:
 		shebang, data = data.split("\n", 1)
 
@@ -33,8 +38,14 @@ def execute_file(filepath):
 		id_ = conspickle.BasicUnpickler.loads(data)
 		command_object = puid.resolve_unique_id(id_)
 	except pickle.UnpicklingError, err:
-		pretty.print_error(__name__, "Could not read", filepath, err)
-		return
+		raise ExecutionError("Could not parse: %s" % unicode(err))
+	except Exception:
+		raise ExecutionError('"%s" is not a saved command' %
+				os.path.basename(filepath))
+	if command_object is None:
+		raise ExecutionError(_('Command in "%s" is not available') %
+				glib.filename_display_basename(filepath))
+
 	command_object.run()
 	glib.idle_add(update_icon, command_object, filepath)
 
diff --git a/kupfer/ui/browser.py b/kupfer/ui/browser.py
index 408829e..736bf60 100644
--- a/kupfer/ui/browser.py
+++ b/kupfer/ui/browser.py
@@ -1524,7 +1524,12 @@ class WindowController (pretty.OutputMixin):
 
 	def _execute_file_received(self, sender, filepath):
 		from kupfer import execfile
-		execfile.execute_file(filepath)
+		from kupfer import uiutils
+		try:
+			execfile.execute_file(filepath)
+		except execfile.ExecutionError, exc:
+			if not uiutils.show_notification(unicode(exc)):
+				raise
 
 	def _close_window(self, window, event):
 		self.put_away()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]