Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
chunk.Chunk Class Reference

List of all members.

Public Member Functions

def __init__
def getname
def getsize
def close
def isatty
def seek
def tell
def read
def skip

Public Attributes

 closed
 align
 file
 chunkname
 chunksize
 size_read
 offset
 seekable

Detailed Description

Definition at line 51 of file chunk.py.


Constructor & Destructor Documentation

def chunk.Chunk.__init__ (   self,
  file,
  align = True,
  bigendian = True,
  inclheader = False 
)

Definition at line 52 of file chunk.py.

00052 
00053     def __init__(self, file, align=True, bigendian=True, inclheader=False):
00054         import struct
00055         self.closed = False
00056         self.align = align      # whether to align to word (2-byte) boundaries
00057         if bigendian:
00058             strflag = '>'
00059         else:
00060             strflag = '<'
00061         self.file = file
00062         self.chunkname = file.read(4)
00063         if len(self.chunkname) < 4:
00064             raise EOFError
00065         try:
00066             self.chunksize = struct.unpack_from(strflag+'L', file.read(4))[0]
00067         except struct.error:
00068             raise EOFError
00069         if inclheader:
00070             self.chunksize = self.chunksize - 8 # subtract header
00071         self.size_read = 0
00072         try:
00073             self.offset = self.file.tell()
00074         except (AttributeError, IOError):
00075             self.seekable = False
00076         else:
00077             self.seekable = True

Here is the caller graph for this function:


Member Function Documentation

def chunk.Chunk.close (   self)

Definition at line 86 of file chunk.py.

00086 
00087     def close(self):
00088         if not self.closed:
00089             self.skip()
00090             self.closed = True

Here is the call graph for this function:

def chunk.Chunk.getname (   self)
Return the name (ID) of the current chunk.

Definition at line 78 of file chunk.py.

00078 
00079     def getname(self):
00080         """Return the name (ID) of the current chunk."""
00081         return self.chunkname

def chunk.Chunk.getsize (   self)
Return the size of the current chunk.

Definition at line 82 of file chunk.py.

00082 
00083     def getsize(self):
00084         """Return the size of the current chunk."""
00085         return self.chunksize

Here is the caller graph for this function:

def chunk.Chunk.isatty (   self)

Definition at line 91 of file chunk.py.

00091 
00092     def isatty(self):
00093         if self.closed:
00094             raise ValueError("I/O operation on closed file")
00095         return False

def chunk.Chunk.read (   self,
  size = -1 
)
Read at most size bytes from the chunk.
If size is omitted or negative, read until the end
of the chunk.

Definition at line 120 of file chunk.py.

00120 
00121     def read(self, size=-1):
00122         """Read at most size bytes from the chunk.
00123         If size is omitted or negative, read until the end
00124         of the chunk.
00125         """
00126 
00127         if self.closed:
00128             raise ValueError("I/O operation on closed file")
00129         if self.size_read >= self.chunksize:
00130             return ''
00131         if size < 0:
00132             size = self.chunksize - self.size_read
00133         if size > self.chunksize - self.size_read:
00134             size = self.chunksize - self.size_read
00135         data = self.file.read(size)
00136         self.size_read = self.size_read + len(data)
00137         if self.size_read == self.chunksize and \
00138            self.align and \
00139            (self.chunksize & 1):
00140             dummy = self.file.read(1)
00141             self.size_read = self.size_read + len(dummy)
00142         return data

Here is the caller graph for this function:

def chunk.Chunk.seek (   self,
  pos,
  whence = 0 
)
Seek to specified position into the chunk.
Default position is 0 (start of chunk).
If the file is not seekable, this will result in an error.

Definition at line 96 of file chunk.py.

00096 
00097     def seek(self, pos, whence=0):
00098         """Seek to specified position into the chunk.
00099         Default position is 0 (start of chunk).
00100         If the file is not seekable, this will result in an error.
00101         """
00102 
00103         if self.closed:
00104             raise ValueError("I/O operation on closed file")
00105         if not self.seekable:
00106             raise IOError("cannot seek")
00107         if whence == 1:
00108             pos = pos + self.size_read
00109         elif whence == 2:
00110             pos = pos + self.chunksize
00111         if pos < 0 or pos > self.chunksize:
00112             raise RuntimeError
00113         self.file.seek(self.offset + pos, 0)
00114         self.size_read = pos

Here is the caller graph for this function:

def chunk.Chunk.skip (   self)
Skip the rest of the chunk.
If you are not interested in the contents of the chunk,
this method should be called so that the file points to
the start of the next chunk.

Definition at line 143 of file chunk.py.

00143 
00144     def skip(self):
00145         """Skip the rest of the chunk.
00146         If you are not interested in the contents of the chunk,
00147         this method should be called so that the file points to
00148         the start of the next chunk.
00149         """
00150 
00151         if self.closed:
00152             raise ValueError("I/O operation on closed file")
00153         if self.seekable:
00154             try:
00155                 n = self.chunksize - self.size_read
00156                 # maybe fix alignment
00157                 if self.align and (self.chunksize & 1):
00158                     n = n + 1
00159                 self.file.seek(n, 1)
00160                 self.size_read = self.size_read + n
00161                 return
00162             except IOError:
00163                 pass
00164         while self.size_read < self.chunksize:
00165             n = min(8192, self.chunksize - self.size_read)
00166             dummy = self.read(n)
00167             if not dummy:
00168                 raise EOFError

Here is the call graph for this function:

Here is the caller graph for this function:

def chunk.Chunk.tell (   self)

Definition at line 115 of file chunk.py.

00115 
00116     def tell(self):
00117         if self.closed:
00118             raise ValueError("I/O operation on closed file")
00119         return self.size_read


Member Data Documentation

Definition at line 55 of file chunk.py.

Definition at line 61 of file chunk.py.

Definition at line 65 of file chunk.py.

Definition at line 54 of file chunk.py.

Definition at line 60 of file chunk.py.

Definition at line 72 of file chunk.py.

Definition at line 74 of file chunk.py.

Definition at line 70 of file chunk.py.


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