Use BytesIO when needed
authorThierry Florac <thierry.florac@onf.fr>
Wed, 22 Jul 2015 16:54:13 +0200
changeset 14 26134ad2f5b9
parent 13 c5e656ef5fe6
child 15 17a9b2d027ec
Use BytesIO when needed
src/pyams_file/archive/gz.py
src/pyams_file/archive/tar.py
src/pyams_file/archive/zip.py
--- a/src/pyams_file/archive/gz.py	Wed Jun 17 09:56:17 2015 +0200
+++ b/src/pyams_file/archive/gz.py	Wed Jul 22 16:54:13 2015 +0200
@@ -33,8 +33,10 @@
     def initialize(self, data):
         if isinstance(data, tuple):
             data = data[0]
+        if not hasattr(data, 'read'):
+            data = BytesIO(data)
         self.data = data
-        self.gzip_file = gzip.GzipFile(fileobj=BytesIO(data), mode='r')
+        self.gzip_file = gzip.GzipFile(fileobj=data, mode='r')
 
     def get_contents(self):
         gzip_data = self.gzip_file.read(4096)
--- a/src/pyams_file/archive/tar.py	Wed Jun 17 09:56:17 2015 +0200
+++ b/src/pyams_file/archive/tar.py	Wed Jul 22 16:54:13 2015 +0200
@@ -32,7 +32,9 @@
     def initialize(self, data, mode='r'):
         if isinstance(data, tuple):
             data = data[0]
-        self.tar = tarfile.open(fileobj=BytesIO(data), mode=mode)
+        if not hasattr(data, 'read'):
+            data = BytesIO(data)
+        self.tar = tarfile.open(fileobj=data, mode=mode)
 
     def get_contents(self):
         members = self.tar.getmembers()
--- a/src/pyams_file/archive/zip.py	Wed Jun 17 09:56:17 2015 +0200
+++ b/src/pyams_file/archive/zip.py	Wed Jul 22 16:54:13 2015 +0200
@@ -32,7 +32,9 @@
     def initialize(self, data, mode='r'):
         if isinstance(data, tuple):
             data = data[0]
-        self.zip_data = zipfile.ZipFile(BytesIO(data), mode=mode)
+        if not hasattr(data, 'read'):
+            data = BytesIO(data)
+        self.zip_data = zipfile.ZipFile(data, mode=mode)
 
     def get_contents(self):
         members = self.zip_data.infolist()