patch the uncompress module to be able to deal with remote file

This commit is contained in:
2016-03-29 20:57:39 +02:00
parent 2e17dbce55
commit 94417e1330
2 changed files with 20 additions and 16 deletions

View File

@ -1,10 +1,11 @@
#cython: language_level=3 #cython: language_level=3
cdef class MagicKeyFile: cdef class MagicKeyFile:
cdef object stream
cdef str stream_mode cdef str stream_mode
cdef object binary cdef object binary
cdef bytes key cdef bytes key
cdef int keylength cdef int keylength
cdef int pos cdef int pos
cpdef bytes read(self,int size=?) cpdef bytes read(self,int size=?)

View File

@ -15,17 +15,20 @@ import io
cdef class MagicKeyFile: cdef class MagicKeyFile:
def __init__(self,stream,length=2): def __init__(self,stream,length=2):
self.stream_mode = None binary=stream
self.stream = stream
self.stream_mode = None
if hasattr(stream, "mode"): if hasattr(stream, "mode"):
self.stream_mode = stream.mode self.stream_mode = stream.mode
if 'b' in stream.mode: if (not 'b' in stream.mode and
binary=stream hasattr(stream, "buffer") and
elif hasattr(stream, "buffer") and 'b' in stream.buffer.mode: 'b' in stream.buffer.mode):
binary=stream.buffer binary=stream.buffer
else:
self.stream_mode = None if (self.stream_mode is None and
not (hasattr(stream, 'headers') and
if self.stream_mode is None: hasattr(stream.headers, "keys") and
'Content-type' in stream.headers)):
raise TypeError("stream does not present the good interface") raise TypeError("stream does not present the good interface")
self.binary=binary self.binary=binary
@ -94,9 +97,12 @@ cdef class CompressedFile:
if self.accessor is None: if self.accessor is None:
self.accessor = magic self.accessor = magic
if ((hasattr(stream, 'headers') and
if 'b' not in magic.stream_mode: hasattr(stream.headers, "keys") and
'Content-type' in stream.headers and
stream.headers['Content-type'].startswith('text/')) or
'b' not in magic.stream_mode):
self.accessor = io.TextIOWrapper(self.accessor) self.accessor = io.TextIOWrapper(self.accessor)
@ -105,7 +111,4 @@ cdef class CompressedFile:
def __iter__(self): def __iter__(self):
for x in self.accessor: for x in self.accessor:
yield x yield x