[kupfer: 9/27] Code cleanup in execfile.py



commit 29610ca68b415e06b4eecace78adbf8429970eae
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Tue Feb 16 16:38:40 2010 +0100

    Code cleanup in execfile.py
    
    We avoid doing existance check manually by opening the file first of
    all. The race between reading the file and checking the permission is
    judged not to be solvable.
    
    Other cleanup in the icon code.

 kupfer/execfile.py |   15 +++++++--------
 1 files changed, 7 insertions(+), 8 deletions(-)
---
diff --git a/kupfer/execfile.py b/kupfer/execfile.py
index c549d94..1200e00 100644
--- a/kupfer/execfile.py
+++ b/kupfer/execfile.py
@@ -23,14 +23,13 @@ def execute_file(filepath):
 	    ...
 	ExecutionError: ... (not executable)
 	"""
-	if not os.path.exists(filepath):
-		raise IOError('"%s" does not exist' % (filepath, ))
+	fobj = open(filepath, "rb")
 	if not os.access(filepath, os.X_OK):
 		raise ExecutionError(_('No permission to run "%s" (not executable)') %
 				glib.filename_display_basename(filepath))
 
 	# strip shebang away
-	data = open(filepath).read()
+	data = fobj.read()
 	if data.startswith("#!") and "\n" in data:
 		shebang, data = data.split("\n", 1)
 
@@ -71,17 +70,17 @@ def _write_thumbnail(gfile, pixbuf):
 def update_icon(kobj, filepath):
 	"Give @filepath a custom icon taken from @kobj"
 	icon_key = "metadata::custom-icon"
+	icon_namespace = icon_key.split(":")[0]
 
 	gfile = gio.File(filepath)
 	finfo = gfile.query_info(icon_key)
 	custom_icon_uri = finfo.get_attribute_string(icon_key)
 	if custom_icon_uri and gio.File(custom_icon_uri).query_exists():
 		return
-	pretty.print_debug(__name__, "Updating icon for", filepath)
-	thumb_filename = _write_thumbnail(gfile, kobj.get_pixbuf(128))
-	if any(N.name == "metadata" for N in gfile.query_writable_namespaces()):
-		gfile.set_attribute_string("metadata::custom-icon",
-				gio.File(thumb_filename).get_uri())
+	if icon_namespace in (N.name for N in gfile.query_writable_namespaces()):
+		pretty.print_debug(__name__, "Updating icon for", filepath)
+		thumb_filename = _write_thumbnail(gfile, kobj.get_pixbuf(128))
+		gfile.set_attribute_string(icon_key, gio.File(thumb_filename).get_uri())
 
 
 if __name__ == '__main__':



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