From 678095180a9d95ea6e0e553c423bafba6c9f575e Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Wed, 14 Aug 2013 13:58:34 +0300 Subject: [PATCH] TransRead: do not cache local uncompressed files When the file is already local and uncompressed, do not create a temporary copy of it when 'local' is True. On top of this, re-use own __init__ function to re-open the local copy of the remote/compressed file. Change-Id: I2093419ed2cfc6d40f73a47657682436b8932402 Signed-off-by: Artem Bityutskiy --- bmaptools/TransRead.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/bmaptools/TransRead.py b/bmaptools/TransRead.py index 9d064e6..a16d0c8 100644 --- a/bmaptools/TransRead.py +++ b/bmaptools/TransRead.py @@ -222,10 +222,10 @@ class TransRead: """ Class constructor. The 'filepath' argument is the full path to the file to read transparently. If 'local' is True, then the file-like object is - guaranteed to be backed by a local file. This means that if the source - file is compressed or an URL, then it will first be copied to a - temporary local file, and then all the subsequent operations will be - done with the local copy. + guaranteed to be backed by an uncompressed local file. This means that + if the source file is compressed and/or an URL, then it will first be + copied to an temporary local file, and then all the subsequent + operations will be done with the uncompresed local copy. """ self.name = filepath @@ -266,7 +266,7 @@ class TransRead: self._open_compressed_file() - if local: + if local and (self.is_url or self.is_compressed): self._create_local_copy() def __del__(self): @@ -491,17 +491,10 @@ class TransRead: tmp_file_obj.write(chunk) tmp_file_obj.flush() - self.close() - self.is_compressed = False - self.is_url = False - self._f_objs.append(tmp_file_obj) - try: - f_obj = open(tmp_file_obj.name, "rb") - self._f_objs.appen(f_obj) - except IOError as err: - raise Error("cannot open own temporary file '%s': %s" - % (tmp_file_obj.name, err)) + self.close() + self.__init__(tmp_file_obj.name, local = False) + tmp_file_obj.close() def read(self, size=-1): """ -- 2.7.4