22import string
33import zlib
44import StringIO
5+ import __builtin__
56
67# implements a python function that reads and writes a gzipped file
78# the user of the file doesn't have to worry about the compression,
@@ -38,23 +39,31 @@ def read32(input):
3839 v = v + (ord (buf [3 ]) << 24 )
3940 return v
4041
41- written = []
42-
43- _py_open = open
44-
45- def open (filename , mode , compresslevel = 9 ):
42+ def open (filename , mode = "r" , compresslevel = 9 ):
4643 return GzipFile (filename , mode , compresslevel )
4744
4845class GzipFile :
4946
50- def __init__ (self , filename , mode = 'r' , compresslevel = 9 ):
51- if mode == 'r' or mode == 'rb' :
47+ myfileobj = None
48+
49+ def __init__ (self , filename = None , mode = None ,
50+ compresslevel = 9 , fileobj = None ):
51+ if fileobj is None :
52+ fileobj = self .myfileobj = __builtin__ .open (filename , mode or 'r' )
53+ if filename is None :
54+ if hasattr (fileobj , 'name' ): filename = fileobj .name
55+ else : filename = 'GzippedFile'
56+ if mode is None :
57+ if hasattr (fileobj , 'mode' ): mode = fileobj .mode
58+ else : mode = 'r'
59+
60+ if mode [0 :1 ] == 'r' :
5261 self .mode = READ
5362 self ._init_read ()
5463 self .filename = filename
5564 self .decompress = zlib .decompressobj (- zlib .MAX_WBITS )
5665
57- elif mode == 'w' or mode == 'wb ' :
66+ elif mode [ 0 : 1 ] == 'w' :
5867 self .mode = WRITE
5968 self ._init_write (filename )
6069 self .compress = zlib .compressobj (compresslevel ,
@@ -65,7 +74,7 @@ def __init__(self, filename, mode='r', compresslevel=9):
6574 else :
6675 raise ValueError , "Mode " + mode + " not supported"
6776
68- self .fileobj = _py_open ( self . filename , mode )
77+ self .fileobj = fileobj
6978
7079 if self .mode == WRITE :
7180 self ._write_gzip_header ()
@@ -134,6 +143,8 @@ def _read_gzip_header(self):
134143
135144
136145 def write (self ,data ):
146+ if self .fileobj is None :
147+ raise ValueError , "write() on closed GzipFile object"
137148 if len (data ) > 0 :
138149 self .size = self .size + len (data )
139150 self .crc = zlib .crc32 (data , self .crc )
@@ -143,7 +154,7 @@ def writelines(self,lines):
143154 self .write (string .join (lines ))
144155
145156 def read (self ,size = None ):
146- if self .extrasize <= 0 and self .fileobj . closed :
157+ if self .extrasize <= 0 and self .fileobj is None :
147158 return ''
148159
149160 if not size :
@@ -173,7 +184,7 @@ def _read(self):
173184 uncompress = self .decompress .flush ()
174185 if uncompress == "" :
175186 self ._read_eof ()
176- self .fileobj . close ()
187+ self .fileobj = None
177188 raise EOFError , 'Reached EOF'
178189 else :
179190 uncompress = self .decompress .decompress (buf )
@@ -201,9 +212,12 @@ def close(self):
201212 self .fileobj .write (self .compress .flush ())
202213 write32 (self .fileobj , self .crc )
203214 write32 (self .fileobj , self .size )
204- self .fileobj . close ()
215+ self .fileobj = None
205216 elif self .mode == READ :
206- self .fileobj .close ()
217+ self .fileobj = None
218+ if self .myfileobj :
219+ self .myfileobj .close ()
220+ self .myfileobj = None
207221
208222 def flush (self ):
209223 self .fileobj .flush ()
@@ -218,47 +232,22 @@ def isatty(self):
218232 return 0
219233
220234 def readline (self ):
221- # should I bother with this
222- raise RuntimeError , "not implemented"
235+ # XXX This function isn't implemented in a very efficient way
236+ line = ""
237+ while 1 :
238+ c = self .read (1 )
239+ line = line + c
240+ if c == '\n ' or c == "" : break
241+ return line
223242
224243 def readlines (self ):
225- # should I bother with this
226- raise RuntimeError , "not implemented"
227-
228-
229- class StringIOgz (GzipFile ):
230-
231- """A StringIO substitute that reads/writes gzipped buffers."""
232-
233- def __init__ (self , buf = None , filename = "StringIOgz" ):
234- """Read/write mode depends on first argument.
235-
236- If __init__ is passed a buffer, it will treat that as the
237- gzipped data and set up the StringIO for reading. Without the
238- initial argument, it will assume a new file for writing.
239-
240- The filename argument is written in the header of buffers
241- opened for writing. Not sure that this is useful, but the
242- GzipFile code expects *some* filename."""
243-
244- if buf :
245- self .mode = READ
246- self ._init_read ()
247- self .filename = filename
248- self .decompress = zlib .decompressobj (- zlib .MAX_WBITS )
249- self .fileobj = StringIO .StringIO (buf )
250- else :
251- self .mode = WRITE
252- self ._init_write (filename )
253- self .compress = zlib .compressobj (compresslevel ,
254- zlib .DEFLATED ,
255- - zlib .MAX_WBITS ,
256- zlib .DEF_MEM_LEVEL ,
257- 0 )
258- self .fileobj = StringIO .StringIO ()
259-
260- if self .mode == WRITE :
261- self ._write_gzip_header ()
262- elif self .mode == READ :
263- self ._read_gzip_header ()
264-
244+ L = []
245+ line = self .readline ()
246+ while line != "" :
247+ L .append (line )
248+ line = self .readline ()
249+ return L
250+
251+ def writelines (self , L ):
252+ for line in L :
253+ self .write (line )
0 commit comments