예제 #1
0
 def write(self, data):
     """Write the data correctly depending on the mode of the file.  While binary mode
     is preferred, we support text mode for streams like stdout."""
     if hasattr(self.out, 'mode') and 'b' in self.out.mode:
         data = bytearray(data)
     else:
         data = decode_bytes(data, "utf8")
     self.out.write(data)
예제 #2
0
    def chunk_to_hash(self, chunk):
        # Our struct template is only 500 bytes, but the last 12 bytes are NUL
        # I elected to ignore them completely instead of including them in the
        # template as '12x'.  The unpack_from method will read the bytes our
        # template defines from chunk and discard the rest.
        unpacked = struct.unpack_from(self.struct_template, chunk)
        unpacked = list(map(lambda x: decode_bytes(x, 'utf8'), unpacked))
        # Zip what we read together with the member names and create a dictionary
        chunk_props = dict(zip(self.struct_members, unpacked))

        return chunk_props