[kupfer] objects: Resolve real path of FileLeaves lazily



commit 91627f2af29714283e8f60d3c67132b4a0e929c1
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Sat Sep 26 01:46:04 2009 +0200

    objects: Resolve real path of FileLeaves lazily
    
    If we call os.path.realpath at construction time for FileLeaf (to
    resolve real paths from symlinks), we incur lots of I/O upon browsing
    directory sources, and effect that slows down even if the directory
    listing is relatively small.
    
    Instead of letting FileLeaf.object be a resolved path, it is the path
    that was used.
    
    We add FileLeaf.canonical_path() to get the real path. We use this for
    the description, and we use os.path.samefile(f1, f2) for FileLeaf's
    __eq__ method.

 kupfer/objects.py |   13 ++++++++++---
 1 files changed, 10 insertions(+), 3 deletions(-)
---
diff --git a/kupfer/objects.py b/kupfer/objects.py
index e0af4ab..b4151c6 100644
--- a/kupfer/objects.py
+++ b/kupfer/objects.py
@@ -204,17 +204,24 @@ class FileLeaf (Leaf):
 		@obj: byte string (file system encoding)
 		@name: unicode name or None for using basename
 		"""
-		# Resolve symlinks
-		obj = path.realpath(obj) or obj
 		# Use glib filename reading to make display name out of filenames
 		# this function returns a `unicode` object
 		if not name:
 			name = gobject.filename_display_basename(obj)
 		super(FileLeaf, self).__init__(obj, name)
 
+	def __eq__(self, other):
+		return (type(self) == type(other) and
+				unicode(self) == unicode(other) and
+				path.samefile(self.object, other.object))
+
 	def repr_key(self):
 		return self.object
 
+	def canonical_path(self):
+		"""Return the true path of the File (without symlinks)"""
+		return path.realpath(self.object)
+
 	def is_valid(self):
 		from os import access, R_OK
 		return access(self.object, R_OK)
@@ -230,7 +237,7 @@ class FileLeaf (Leaf):
 		"""Format the path shorter:
 		replace homedir by ~/
 		"""
-		return utils.get_display_path_for_bytestring(self.object)
+		return utils.get_display_path_for_bytestring(self.canonical_path())
 
 	def get_actions(self):
 		acts = [RevealFile(), ]



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