Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
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 379 of file tarfile.py.


Constructor & Destructor Documentation

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

Definition at line 390 of file tarfile.py.

00390 
00391     def __init__(self, name, mode, comptype, fileobj, bufsize):
00392         """Construct a _Stream object.
00393         """
00394         self._extfileobj = True
00395         if fileobj is None:
00396             fileobj = _LowLevelFile(name, mode)
00397             self._extfileobj = False
00398 
00399         if comptype == '*':
00400             # Enable transparent compression detection for the
00401             # stream interface
00402             fileobj = _StreamProxy(fileobj)
00403             comptype = fileobj.getcomptype()
00404 
00405         self.name     = name or ""
00406         self.mode     = mode
00407         self.comptype = comptype
00408         self.fileobj  = fileobj
00409         self.bufsize  = bufsize
00410         self.buf      = b""
00411         self.pos      = 0
00412         self.closed   = False
00413 
00414         try:
00415             if comptype == "gz":
00416                 try:
00417                     import zlib
00418                 except ImportError:
00419                     raise CompressionError("zlib module is not available")
00420                 self.zlib = zlib
00421                 self.crc = zlib.crc32(b"")
00422                 if mode == "r":
00423                     self._init_read_gz()
00424                 else:
00425                     self._init_write_gz()
00426 
00427             if comptype == "bz2":
00428                 try:
00429                     import bz2
00430                 except ImportError:
00431                     raise CompressionError("bz2 module is not available")
00432                 if mode == "r":
00433                     self.dbuf = b""
00434                     self.cmp = bz2.BZ2Decompressor()
00435                 else:
00436                     self.cmp = bz2.BZ2Compressor()
00437         except:
00438             if not self._extfileobj:
00439                 self.fileobj.close()
00440             self.closed = True
00441             raise

Here is the caller graph for this function:

def tarfile._Stream.__del__ (   self)

Definition at line 442 of file tarfile.py.

00442 
00443     def __del__(self):
00444         if hasattr(self, "closed") and not self.closed:
00445             self.close()


Member Function Documentation

def 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 595 of file tarfile.py.

00595 
00596     def __read(self, size):
00597         """Return size bytes from stream. If internal buffer is empty,
00598            read another block from the stream.
00599         """
00600         c = len(self.buf)
00601         while c < size:
00602             buf = self.fileobj.read(self.bufsize)
00603             if not buf:
00604                 break
00605             self.buf += buf
00606             c += len(buf)
00607         buf = self.buf[:size]
00608         self.buf = self.buf[size:]
00609         return buf
00610 # class _Stream

Here is the caller graph for this function:

def 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 470 of file tarfile.py.

00470 
00471     def __write(self, s):
00472         """Write string s to the stream if a whole new block
00473            is ready to be written.
00474         """
00475         self.buf += s
00476         while len(self.buf) > self.bufsize:
00477             self.fileobj.write(self.buf[:self.bufsize])
00478             self.buf = self.buf[self.bufsize:]

Here is the caller graph for this function:

def tarfile._Stream._init_read_gz (   self) [private]
Initialize for reading a gzip compressed fileobj.

Definition at line 507 of file tarfile.py.

00507 
00508     def _init_read_gz(self):
00509         """Initialize for reading a gzip compressed fileobj.
00510         """
00511         self.cmp = self.zlib.decompressobj(-self.zlib.MAX_WBITS)
00512         self.dbuf = b""
00513 
00514         # taken from gzip.GzipFile with some alterations
00515         if self.__read(2) != b"\037\213":
00516             raise ReadError("not a gzip file")
00517         if self.__read(1) != b"\010":
00518             raise CompressionError("unsupported compression method")
00519 
00520         flag = ord(self.__read(1))
00521         self.__read(6)
00522 
00523         if flag & 4:
00524             xlen = ord(self.__read(1)) + 256 * ord(self.__read(1))
00525             self.read(xlen)
00526         if flag & 8:
00527             while True:
00528                 s = self.__read(1)
00529                 if not s or s == NUL:
00530                     break
00531         if flag & 16:
00532             while True:
00533                 s = self.__read(1)
00534                 if not s or s == NUL:
00535                     break
00536         if flag & 2:
00537             self.__read(2)

Here is the call graph for this function:

def tarfile._Stream._init_write_gz (   self) [private]
Initialize for writing with gzip compression.

Definition at line 446 of file tarfile.py.

00446 
00447     def _init_write_gz(self):
00448         """Initialize for writing with gzip compression.
00449         """
00450         self.cmp = self.zlib.compressobj(9, self.zlib.DEFLATED,
00451                                             -self.zlib.MAX_WBITS,
00452                                             self.zlib.DEF_MEM_LEVEL,
00453                                             0)
00454         timestamp = struct.pack("<L", int(time.time()))
00455         self.__write(b"\037\213\010\010" + timestamp + b"\002\377")
00456         if self.name.endswith(".gz"):
00457             self.name = self.name[:-3]
00458         # RFC1952 says we must use ISO-8859-1 for the FNAME field.
00459         self.__write(self.name.encode("iso-8859-1", "replace") + NUL)

Here is the call graph for this function:

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

Definition at line 574 of file tarfile.py.

00574 
00575     def _read(self, size):
00576         """Return size bytes from the stream.
00577         """
00578         if self.comptype == "tar":
00579             return self.__read(size)
00580 
00581         c = len(self.dbuf)
00582         while c < size:
00583             buf = self.__read(self.bufsize)
00584             if not buf:
00585                 break
00586             try:
00587                 buf = self.cmp.decompress(buf)
00588             except IOError:
00589                 raise ReadError("invalid compressed data")
00590             self.dbuf += buf
00591             c += len(buf)
00592         buf = self.dbuf[:size]
00593         self.dbuf = self.dbuf[size:]
00594         return buf

Here is the call graph for this function:

Here is the caller graph for this function:

def tarfile._Stream.close (   self)
Close the _Stream object. No operation should be
   done on it afterwards.

Definition at line 479 of file tarfile.py.

00479 
00480     def close(self):
00481         """Close the _Stream object. No operation should be
00482            done on it afterwards.
00483         """
00484         if self.closed:
00485             return
00486 
00487         if self.mode == "w" and self.comptype != "tar":
00488             self.buf += self.cmp.flush()
00489 
00490         if self.mode == "w" and self.buf:
00491             self.fileobj.write(self.buf)
00492             self.buf = b""
00493             if self.comptype == "gz":
00494                 # The native zlib crc is an unsigned 32-bit integer, but
00495                 # the Python wrapper implicitly casts that to a signed C
00496                 # long.  So, on a 32-bit box self.crc may "look negative",
00497                 # while the same crc on a 64-bit box may "look positive".
00498                 # To avoid irksome warnings from the `struct` module, force
00499                 # it to look positive on all boxes.
00500                 self.fileobj.write(struct.pack("<L", self.crc & 0xffffffff))
00501                 self.fileobj.write(struct.pack("<L", self.pos & 0xffffFFFF))
00502 
00503         if not self._extfileobj:
00504             self.fileobj.close()
00505 
00506         self.closed = True

Here is the caller graph for this function:

def 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 556 of file tarfile.py.

00556 
00557     def read(self, size=None):
00558         """Return the next size number of bytes from the stream.
00559            If size is not defined, return all bytes of the stream
00560            up to EOF.
00561         """
00562         if size is None:
00563             t = []
00564             while True:
00565                 buf = self._read(self.bufsize)
00566                 if not buf:
00567                     break
00568                 t.append(buf)
00569             buf = "".join(t)
00570         else:
00571             buf = self._read(size)
00572         self.pos += len(buf)
00573         return buf

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 543 of file tarfile.py.

00543 
00544     def seek(self, pos=0):
00545         """Set the stream's file pointer to pos. Negative seeking
00546            is forbidden.
00547         """
00548         if pos - self.pos >= 0:
00549             blocks, remainder = divmod(pos - self.pos, self.bufsize)
00550             for i in range(blocks):
00551                 self.read(self.bufsize)
00552             self.read(remainder)
00553         else:
00554             raise StreamError("seeking backwards is not allowed")
00555         return self.pos

Here is the call graph for this function:

Here is the caller graph for this function:

def tarfile._Stream.tell (   self)
Return the stream's file pointer position.

Definition at line 538 of file tarfile.py.

00538 
00539     def tell(self):
00540         """Return the stream's file pointer position.
00541         """
00542         return self.pos

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

Definition at line 460 of file tarfile.py.

00460 
00461     def write(self, s):
00462         """Write string s to the stream.
00463         """
00464         if self.comptype == "gz":
00465             self.crc = self.zlib.crc32(s, self.crc)
00466         self.pos += len(s)
00467         if self.comptype != "tar":
00468             s = self.cmp.compress(s)
00469         self.__write(s)

Here is the call graph for this function:


Member Data Documentation

Definition at line 393 of file tarfile.py.

Definition at line 409 of file tarfile.py.

Definition at line 408 of file tarfile.py.

Definition at line 411 of file tarfile.py.

Definition at line 433 of file tarfile.py.

Definition at line 406 of file tarfile.py.

Definition at line 420 of file tarfile.py.

Definition at line 432 of file tarfile.py.

Definition at line 407 of file tarfile.py.

Definition at line 405 of file tarfile.py.

Definition at line 404 of file tarfile.py.

Definition at line 410 of file tarfile.py.

Definition at line 419 of file tarfile.py.


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