Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
MoinMoin.support.tarfile._Stream Class Reference

List of all members.

Public Member Functions

def __init__
def __del__
def write
def close
def tell
def seek
def read

Public Attributes

 name
 mode
 comptype
 fileobj
 bufsize
 buf
 pos
 closed
 zlib
 crc
 dbuf
 cmp

Private Member Functions

def _init_write_gz
def __write
def _init_read_gz
def _read
def __read

Private Attributes

 _extfileobj

Detailed Description

Class that serves as an adapter between TarFile and
   a stream-like object.  The stream-like object only
   needs to have a read() or write() method and is accessed
   blockwise.  Use of gzip or bzip2 compression is possible.
   A stream-like object could be for example: sys.stdin,
   sys.stdout, a socket, a tape device etc.

   _Stream is intended to be used only internally.

Definition at line 330 of file tarfile.py.


Constructor & Destructor Documentation

def MoinMoin.support.tarfile._Stream.__init__ (   self,
  name,
  mode,
  comptype,
  fileobj,
  bufsize 
)
Construct a _Stream object.

Definition at line 341 of file tarfile.py.

00341 
00342     def __init__(self, name, mode, comptype, fileobj, bufsize):
00343         """Construct a _Stream object.
00344         """
00345         self._extfileobj = True
00346         if fileobj is None:
00347             fileobj = _LowLevelFile(name, mode)
00348             self._extfileobj = False
00349 
00350         if comptype == '*':
00351             # Enable transparent compression detection for the
00352             # stream interface
00353             fileobj = _StreamProxy(fileobj)
00354             comptype = fileobj.getcomptype()
00355 
00356         self.name     = name or ""
00357         self.mode     = mode
00358         self.comptype = comptype
00359         self.fileobj  = fileobj
00360         self.bufsize  = bufsize
00361         self.buf      = ""
00362         self.pos      = 0L
00363         self.closed   = False
00364 
00365         if comptype == "gz":
00366             try:
00367                 import zlib
00368             except ImportError:
00369                 raise CompressionError("zlib module is not available")
00370             self.zlib = zlib
00371             self.crc = zlib.crc32("")
00372             if mode == "r":
00373                 self._init_read_gz()
00374             else:
00375                 self._init_write_gz()
00376 
00377         if comptype == "bz2":
00378             try:
00379                 import bz2
00380             except ImportError:
00381                 raise CompressionError("bz2 module is not available")
00382             if mode == "r":
00383                 self.dbuf = ""
00384                 self.cmp = bz2.BZ2Decompressor()
00385             else:
00386                 self.cmp = bz2.BZ2Compressor()

Definition at line 387 of file tarfile.py.

00387 
00388     def __del__(self):
00389         if hasattr(self, "closed") and not self.closed:
00390             self.close()

Here is the call graph for this function:


Member Function Documentation

def MoinMoin.support.tarfile._Stream.__read (   self,
  size 
) [private]
Return size bytes from stream. If internal buffer is empty,
   read another block from the stream.

Definition at line 537 of file tarfile.py.

00537 
00538     def __read(self, size):
00539         """Return size bytes from stream. If internal buffer is empty,
00540            read another block from the stream.
00541         """
00542         c = len(self.buf)
00543         t = [self.buf]
00544         while c < size:
00545             buf = self.fileobj.read(self.bufsize)
00546             if not buf:
00547                 break
00548             t.append(buf)
00549             c += len(buf)
00550         t = "".join(t)
00551         self.buf = t[size:]
00552         return t[:size]
00553 # class _Stream

Here is the caller graph for this function:

def MoinMoin.support.tarfile._Stream.__write (   self,
  s 
) [private]
Write string s to the stream if a whole new block
   is ready to be written.

Definition at line 414 of file tarfile.py.

00414 
00415     def __write(self, s):
00416         """Write string s to the stream if a whole new block
00417            is ready to be written.
00418         """
00419         self.buf += s
00420         while len(self.buf) > self.bufsize:
00421             self.fileobj.write(self.buf[:self.bufsize])
00422             self.buf = self.buf[self.bufsize:]

Here is the caller graph for this function:

Initialize for reading a gzip compressed fileobj.

Definition at line 451 of file tarfile.py.

00451 
00452     def _init_read_gz(self):
00453         """Initialize for reading a gzip compressed fileobj.
00454         """
00455         self.cmp = self.zlib.decompressobj(-self.zlib.MAX_WBITS)
00456         self.dbuf = ""
00457 
00458         # taken from gzip.GzipFile with some alterations
00459         if self.__read(2) != "\037\213":
00460             raise ReadError("not a gzip file")
00461         if self.__read(1) != "\010":
00462             raise CompressionError("unsupported compression method")
00463 
00464         flag = ord(self.__read(1))
00465         self.__read(6)
00466 
00467         if flag & 4:
00468             xlen = ord(self.__read(1)) + 256 * ord(self.__read(1))
00469             self.read(xlen)
00470         if flag & 8:
00471             while True:
00472                 s = self.__read(1)
00473                 if not s or s == NUL:
00474                     break
00475         if flag & 16:
00476             while True:
00477                 s = self.__read(1)
00478                 if not s or s == NUL:
00479                     break
00480         if flag & 2:
00481             self.__read(2)

Initialize for writing with gzip compression.

Definition at line 391 of file tarfile.py.

00391 
00392     def _init_write_gz(self):
00393         """Initialize for writing with gzip compression.
00394         """
00395         self.cmp = self.zlib.compressobj(9, self.zlib.DEFLATED,
00396                                             -self.zlib.MAX_WBITS,
00397                                             self.zlib.DEF_MEM_LEVEL,
00398                                             0)
00399         timestamp = struct.pack("<L", long(time.time()))
00400         self.__write("\037\213\010\010%s\002\377" % timestamp)
00401         if self.name.endswith(".gz"):
00402             self.name = self.name[:-3]
00403         self.__write(self.name + NUL)

Here is the call graph for this function:

def MoinMoin.support.tarfile._Stream._read (   self,
  size 
) [private]
Return size bytes from the stream.

Definition at line 518 of file tarfile.py.

00518 
00519     def _read(self, size):
00520         """Return size bytes from the stream.
00521         """
00522         if self.comptype == "tar":
00523             return self.__read(size)
00524 
00525         c = len(self.dbuf)
00526         t = [self.dbuf]
00527         while c < size:
00528             buf = self.__read(self.bufsize)
00529             if not buf:
00530                 break
00531             buf = self.cmp.decompress(buf)
00532             t.append(buf)
00533             c += len(buf)
00534         t = "".join(t)
00535         self.dbuf = t[size:]
00536         return t[:size]

Here is the call graph for this function:

Here is the caller graph for this function:

Close the _Stream object. No operation should be
   done on it afterwards.

Definition at line 423 of file tarfile.py.

00423 
00424     def close(self):
00425         """Close the _Stream object. No operation should be
00426            done on it afterwards.
00427         """
00428         if self.closed:
00429             return
00430 
00431         if self.mode == "w" and self.comptype != "tar":
00432             self.buf += self.cmp.flush()
00433 
00434         if self.mode == "w" and self.buf:
00435             self.fileobj.write(self.buf)
00436             self.buf = ""
00437             if self.comptype == "gz":
00438                 # The native zlib crc is an unsigned 32-bit integer, but
00439                 # the Python wrapper implicitly casts that to a signed C
00440                 # long.  So, on a 32-bit box self.crc may "look negative",
00441                 # while the same crc on a 64-bit box may "look positive".
00442                 # To avoid irksome warnings from the `struct` module, force
00443                 # it to look positive on all boxes.
00444                 self.fileobj.write(struct.pack("<L", self.crc & 0xffffffffL))
00445                 self.fileobj.write(struct.pack("<L", self.pos & 0xffffFFFFL))
00446 
00447         if not self._extfileobj:
00448             self.fileobj.close()
00449 
00450         self.closed = True

Here is the caller graph for this function:

def MoinMoin.support.tarfile._Stream.read (   self,
  size = None 
)
Return the next size number of bytes from the stream.
   If size is not defined, return all bytes of the stream
   up to EOF.

Definition at line 500 of file tarfile.py.

00500 
00501     def read(self, size=None):
00502         """Return the next size number of bytes from the stream.
00503            If size is not defined, return all bytes of the stream
00504            up to EOF.
00505         """
00506         if size is None:
00507             t = []
00508             while True:
00509                 buf = self._read(self.bufsize)
00510                 if not buf:
00511                     break
00512                 t.append(buf)
00513             buf = "".join(t)
00514         else:
00515             buf = self._read(size)
00516         self.pos += len(buf)
00517         return buf

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.support.tarfile._Stream.seek (   self,
  pos = 0 
)
Set the stream's file pointer to pos. Negative seeking
   is forbidden.

Definition at line 487 of file tarfile.py.

00487 
00488     def seek(self, pos=0):
00489         """Set the stream's file pointer to pos. Negative seeking
00490            is forbidden.
00491         """
00492         if pos - self.pos >= 0:
00493             blocks, remainder = divmod(pos - self.pos, self.bufsize)
00494             for i in xrange(blocks):
00495                 self.read(self.bufsize)
00496             self.read(remainder)
00497         else:
00498             raise StreamError("seeking backwards is not allowed")
00499         return self.pos

Return the stream's file pointer position.

Definition at line 482 of file tarfile.py.

00482 
00483     def tell(self):
00484         """Return the stream's file pointer position.
00485         """
00486         return self.pos

def MoinMoin.support.tarfile._Stream.write (   self,
  s 
)
Write string s to the stream.

Definition at line 404 of file tarfile.py.

00404 
00405     def write(self, s):
00406         """Write string s to the stream.
00407         """
00408         if self.comptype == "gz":
00409             self.crc = self.zlib.crc32(s, self.crc)
00410         self.pos += len(s)
00411         if self.comptype != "tar":
00412             s = self.cmp.compress(s)
00413         self.__write(s)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 344 of file tarfile.py.

Definition at line 360 of file tarfile.py.

Definition at line 359 of file tarfile.py.

Definition at line 362 of file tarfile.py.

Definition at line 383 of file tarfile.py.

Definition at line 357 of file tarfile.py.

Definition at line 370 of file tarfile.py.

Definition at line 382 of file tarfile.py.

Definition at line 358 of file tarfile.py.

Definition at line 356 of file tarfile.py.

Definition at line 355 of file tarfile.py.

Definition at line 361 of file tarfile.py.

Definition at line 369 of file tarfile.py.


The documentation for this class was generated from the following file: