A file usually contains many types of chunks. In most occasions, chunks are not arranged in a fixed order. So we need to use matches() to read a few bytes first to detect if the following data match the specific chunk type.
Chunk in the file. A chunk usually contains a signature indicating its type, and a group of data fields.
Parameters: |
|
---|
To define your chunk class, you should follow these steps:
Define its fields. Fields are populated in this order. See Fields for more information.
Define a matches() or safe_matches() class method to judge if the following bytes match this type of chunk.
- Override matches() to handle fp state by yourself.
- Override safe_matches() to get fp state auto saved.
Example:
import os
import struct
from chunker.chunks import Chunk
class DataChunk(Chunk):
Fields = (
UnsignedLongField('sig'), # 0x01020304
UnsignedLongField('type'),
UnsignedLongField('data_length'),
StringField('data', length_field_name='data_length'),
)
@classmethod
def safe_matches(fp):
buf = fp.read(4)
type = struct.unpack('>H', buf)[0]
return type == 0x01020304
if DataChunk.matches(fp):
c = DataChunk(fp, None)
print(c.data) # Access field value by its name
Read next a few bytes to judge if the following data match this type of chunk.
It calls safe_matches() and restores fp state by default.
You can override this to avoid saving fp state.
Parameters: | fp – File object to read from. |
---|---|
Returns: | If the following bytes match this chunk type. |
Populate chunk fields. After that, you can access each field data directly by field name.
The difference between safe_matches() and matches() is that if you override this, you can get your fp state auto saved.
You only need to override one of them:
Parameters: | fp – File object to read from. |
---|---|
Returns: | If the following bytes match this chunk type. |