TransFile: introduce "_force_fake_seek" attribute
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Mon, 3 Jun 2013 11:02:16 +0000 (14:02 +0300)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Mon, 3 Jun 2013 11:02:16 +0000 (14:02 +0300)
TransFile object provide read interface to compressed and/or remote files.
TransFile objects also allow seeking files forward. When the file happens to be
a local uncompressed file, seeking is done using the native 'seek()' method.
Otherwise, we emulate this by just reading the required amount of bytes from the
file and discarding the data.

The way we detect whether we can seek using the native method or not is that we
call 'hasattr(file_obj, "seek")', and if the file object has the "seek()"
method, we use it.

However, there are situations when a files have the "seek()" method, but it is
not really usable. For example, stdout.

This patch introduces an internal attribute named "_force_fake_seek", which
will force fake seek implementation for such file objects.

We do not need this change right now, but will need it soon. So this is just a
preparation for the coming changes.

Change-Id: I0128499e7c3fba0b6aa665ece405ecb33085d4be
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
bmaptools/TransRead.py

index 4f0b5296f162508f697de5f2b9b686608630d1c3..2308d6adda7ba9a201cbefeb982d9406b18ae4ca 100644 (file)
@@ -285,6 +285,7 @@ class TransRead:
         self.is_url = False
         self._file_obj = None
         self._transfile_obj = None
+        self._force_fake_seek = False
         self._pos = 0
 
         try:
@@ -324,19 +325,19 @@ class TransRead:
     def seek(self, offset, whence = os.SEEK_SET):
         """ The 'seek()' method, similar to the one file objects have. """
 
-        if hasattr(self._transfile_obj, "seek"):
-            self._transfile_obj.seek(offset, whence)
-        else:
+        if self._force_fake_seek or not hasattr(self._transfile_obj, "seek"):
             self._pos = _fake_seek_forward(self._transfile_obj, self._pos, \
                                            offset, whence)
+        else:
+            self._transfile_obj.seek(offset, whence)
 
     def tell(self):
         """ The 'tell()' method, similar to the one file objects have. """
 
-        if hasattr(self._transfile_obj, "tell"):
-            return self._transfile_obj.tell()
-        else:
+        if self._force_fake_seek or not hasattr(self._transfile_obj, "tell"):
             return self._pos
+        else:
+            return self._transfile_obj.tell()
 
     def close(self):
         """ Close the file-like object. """