[Checkins] SVN: z3c.extfile/trunk/src/z3c/extfile/ Readfile is now lazy in opening files

Bernd Dorn bernd.dorn at fhv.at
Wed Sep 13 08:27:42 EDT 2006


Log message for revision 70146:
  Readfile is now lazy in opening files

Changed:
  U   z3c.extfile/trunk/src/z3c/extfile/file/file.txt
  U   z3c.extfile/trunk/src/z3c/extfile/hashdir.py
  U   z3c.extfile/trunk/src/z3c/extfile/hashdir.txt

-=-
Modified: z3c.extfile/trunk/src/z3c/extfile/file/file.txt
===================================================================
--- z3c.extfile/trunk/src/z3c/extfile/file/file.txt	2006-09-13 12:15:00 UTC (rev 70145)
+++ z3c.extfile/trunk/src/z3c/extfile/file/file.txt	2006-09-13 12:27:41 UTC (rev 70146)
@@ -38,7 +38,7 @@
   >>> f.data.write("hello")
   Traceback (most recent call last):
   ...
-  IOError: (0, 'Error')
+  AttributeError: 'ReadFile' object has no attribute 'write'
 
 But we can of course set a new value on data
 

Modified: z3c.extfile/trunk/src/z3c/extfile/hashdir.py
===================================================================
--- z3c.extfile/trunk/src/z3c/extfile/hashdir.py	2006-09-13 12:15:00 UTC (rev 70145)
+++ z3c.extfile/trunk/src/z3c/extfile/hashdir.py	2006-09-13 12:27:41 UTC (rev 70146)
@@ -72,24 +72,56 @@
         return ReadFile(self.getPath(digest))
         
 
-class ReadFile(file):
+class ReadFile(object):
 
     interface.implements(interfaces.IReadFile)
 
-    def __init__(self, filename, bufsize=-1):
+    def __init__(self, name, bufsize=-1):
         """we always use read binary as mode"""
-        super(ReadFile, self).__init__(filename, 'rb', bufsize)
+        self.name = name
+        self.digest = os.path.split(self.name)[1]
+        self.bufsize=bufsize
+        self._v_file = None
+
+    @property
+    def _file(self):
+        if self._v_file is not None:
+            if not self._v_file.closed:
+                return self._v_file
+        self._v_file = file(self.name, 'rb', self.bufsize)
+        return self._v_file
     
     def __len__(self):
-        return int(os.fstat(self.fileno())[stat.ST_SIZE])
+        return int(os.stat(self.name)[stat.ST_SIZE])
 
     def __repr__(self):
         return "<ReadFile named %s>" % repr(self.digest)
 
-    @property
-    def digest(self):
-        return os.path.split(self.name)[1]
+    def seek(self, offset, whence=0):
+        """see file.seek"""
+        return self._file.seek(offset, whence)
 
+    def tell(self):
+        """see file.tell"""
+        return self._file.tell()
+
+    def read(self, size=-1):
+        """see file.read"""
+        return self._file.read(size)
+
+    def close(self):
+        """see file.close"""
+        if self._v_file is not None:
+            if not self._v_file.closed:
+                return self._v_file.close()
+        self._v_file = None
+        
+    def fileno(self):
+        return self._file.fileno()
+
+    def __iter__(self):
+        return self._file.__iter__()
+
 class WriteFile(object):
 
     interface.implements(interfaces.IWriteFile)

Modified: z3c.extfile/trunk/src/z3c/extfile/hashdir.txt
===================================================================
--- z3c.extfile/trunk/src/z3c/extfile/hashdir.txt	2006-09-13 12:15:00 UTC (rev 70145)
+++ z3c.extfile/trunk/src/z3c/extfile/hashdir.txt	2006-09-13 12:27:41 UTC (rev 70146)
@@ -76,13 +76,39 @@
 
   >>> f.tell()
   9L
-  
+
+  >>> f.seek(2)
+  >>> f.read(10)
+  'ntent 2'
+
+Readfiles are sized
+
+  >>> len(f)
+  9
+
+You also can get the filedescriptor for the Readfile.
+
+  >>> type(f.fileno())==type(1)
+  True
+
+If an IReadFile is closed it can still be read but it is then a new file.
+
+  >>> f.close()
+  >>> f.read()
+  'Content 2'
+
+Readfiles can be iterated over.
+
+  >>> f.seek(0)
+  >>> [line for line in f]
+  ['Content 2']
+
 We can also get the path of the file.
 
   >>> hd.getPath('0db0e5fa1ecf3e7659504f2e4048434cd9f20d2d')
   '...0db0e5fa1ecf3e7659504f2e4048434cd9f20d2d'
 
-We can get the size of the file.
+We can get the size of the file via the hashdir.
 
   >>> f = hd.new()
   >>> for s in ['abc']*1024*500:
@@ -91,7 +117,6 @@
   >>> hd.getSize(d)
   1536000L
 
-
 Empty files are also allowed.
 
   >>> f = hd.new()



More information about the Checkins mailing list