From: Artem Bityutskiy Date: Mon, 3 Jun 2013 11:02:16 +0000 (+0300) Subject: TransFile: introduce "_force_fake_seek" attribute X-Git-Tag: v2.5~34 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=fc7ebf238bb9f7af1c91feb843548ed3ed9c48eb;p=tools%2Fbmap-tools.git TransFile: introduce "_force_fake_seek" attribute 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 --- diff --git a/bmaptools/TransRead.py b/bmaptools/TransRead.py index 4f0b529..2308d6a 100644 --- a/bmaptools/TransRead.py +++ b/bmaptools/TransRead.py @@ -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. """