A thin wrapper around a binary file that facilitates reading C-struct values.
| 189 | |
| 190 | |
| 191 | class _Stream(object): |
| 192 | """A thin wrapper around a binary file that facilitates reading C-struct values.""" |
| 193 | |
| 194 | def __init__(self, file): |
| 195 | self._file = file |
| 196 | |
| 197 | @classmethod |
| 198 | def open(cls, path): |
| 199 | """Return |_Stream| providing binary access to contents of file at `path`.""" |
| 200 | return cls(open(path, "rb")) |
| 201 | |
| 202 | def close(self): |
| 203 | """ |
| 204 | Close the wrapped file. Using the stream after closing raises an |
| 205 | exception. |
| 206 | """ |
| 207 | self._file.close() |
| 208 | |
| 209 | def read(self, offset, length): |
| 210 | """ |
| 211 | Return *length* bytes from this stream starting at *offset*. |
| 212 | """ |
| 213 | self._file.seek(offset) |
| 214 | return self._file.read(length) |
| 215 | |
| 216 | def read_fields(self, template, offset=0): |
| 217 | """ |
| 218 | Return a tuple containing the C-struct fields in this stream |
| 219 | specified by *template* and starting at *offset*. |
| 220 | """ |
| 221 | self._file.seek(offset) |
| 222 | bufr = self._file.read(calcsize(template)) |
| 223 | return unpack_from(template, bufr) |
| 224 | |
| 225 | |
| 226 | class _BaseTable(object): |
no outgoing calls
searching dependent graphs…