Skip to content

helpers

helpers

StreamReader

StreamReader(stream, byte_order, base_offset=0)

Wraps a file-like object to provide access to structured data from a binary file.

Byte-order is configurable. base_offset is added to any base value provided to calculate actual location for reads.

Source code in src/docx/image/helpers.py
def __init__(self, stream, byte_order, base_offset=0):
    super(StreamReader, self).__init__()
    self._stream = stream
    self._byte_order = LITTLE_ENDIAN if byte_order == LITTLE_ENDIAN else BIG_ENDIAN
    self._base_offset = base_offset

read

read(count)

Allow pass-through read() call.

Source code in src/docx/image/helpers.py
def read(self, count):
    """Allow pass-through read() call."""
    return self._stream.read(count)

read_byte

read_byte(base, offset=0)

Return the int value of the byte at the file position defined by self._base_offset + base + offset.

If base is None, the byte is read from the current position in the stream.

Source code in src/docx/image/helpers.py
def read_byte(self, base, offset=0):
    """Return the int value of the byte at the file position defined by
    self._base_offset + `base` + `offset`.

    If `base` is None, the byte is read from the current position in the stream.
    """
    fmt = "B"
    return self._read_int(fmt, base, offset)

read_long

read_long(base, offset=0)

Return the int value of the four bytes at the file position defined by self._base_offset + base + offset.

If base is None, the long is read from the current position in the stream. The endian setting of this instance is used to interpret the byte layout of the long.

Source code in src/docx/image/helpers.py
def read_long(self, base, offset=0):
    """Return the int value of the four bytes at the file position defined by
    self._base_offset + `base` + `offset`.

    If `base` is None, the long is read from the current position in the stream. The
    endian setting of this instance is used to interpret the byte layout of the
    long.
    """
    fmt = "<L" if self._byte_order is LITTLE_ENDIAN else ">L"
    return self._read_int(fmt, base, offset)

read_short

read_short(base, offset=0)

Return the int value of the two bytes at the file position determined by base and offset, similarly to read_long() above.

Source code in src/docx/image/helpers.py
def read_short(self, base, offset=0):
    """Return the int value of the two bytes at the file position determined by
    `base` and `offset`, similarly to ``read_long()`` above."""
    fmt = b"<H" if self._byte_order is LITTLE_ENDIAN else b">H"
    return self._read_int(fmt, base, offset)

read_str

read_str(char_count, base, offset=0)

Return a string containing the char_count bytes at the file position determined by self._base_offset + base + offset.

Source code in src/docx/image/helpers.py
def read_str(self, char_count, base, offset=0):
    """Return a string containing the `char_count` bytes at the file position
    determined by self._base_offset + `base` + `offset`."""

    def str_struct(char_count):
        format_ = "%ds" % char_count
        return Struct(format_)

    struct = str_struct(char_count)
    chars = self._unpack_item(struct, base, offset)
    unicode_str = chars.decode("UTF-8")
    return unicode_str

tell

tell()

Allow pass-through tell() call.

Source code in src/docx/image/helpers.py
def tell(self):
    """Allow pass-through tell() call."""
    return self._stream.tell()