exiv2._basicio

Class interface to access files, memory and remote data.

Classes

BasicIo

An interface for simple binary IO.

class BasicIo

Bases: SwigPyObject

An interface for simple binary IO.

Designed to have semantics and names similar to those of C style FILE* operations. Subclasses should all behave the same so that they can be interchanged.

class Position

Bases: IntEnum

Seek starting positions.

beg = 0
cur = 1
end = 2
close()

Close the IO source. After closing a BasicIo instance can not be read or written. Closing flushes any unwritten data. It is safe to call close on a closed instance.

Return type:

int

Returns:

0 if successful;

Nonzero if failure.

eof()

Returns true if the IO position has reached the end, otherwise false.

error()

Returns 0 if the IO source is in a valid state, otherwise nonzero.

getb()

Read one byte from the IO source. Current IO position is advanced by one byte.

Return type:

int

Returns:

The byte read from the IO source if successful;

EOF if failure;

ioType()

Return the derived class type.

You shouldn’t usually need to know the type of IO as they all have the same interface.

Return type:

str

Returns:

A class name such as “FileIo”.

isopen()

Returns true if the IO source is open, otherwise false.

mmap()

Direct access to the IO data. For files, this is done by mapping the file into the process’s address space; for memory blocks, this allows direct access to the memory block.

Parameters:

isWriteable (bool, optional) – Set to true if the mapped area should be writeable (default is false).

Return type:

memoryview

Returns:

A pointer to the mapped area.

Raises:

Exiv2Error In case of failure.

munmap()

Remove a mapping established with mmap(). If the mapped area is writeable, this ensures that changes are written back.

Return type:

int

Returns:

0 if successful;

Nonzero if failure;

open()

Open the IO source using the default access mode. The default mode should allow for reading and writing.

This method can also be used to “reopen” an IO source which will flush any unwritten data and reset the IO position to the start. Subclasses may provide custom methods to allow for opening IO sources differently.

Return type:

int

Returns:

0 if successful;

Nonzero if failure.

path()

Return the path to the IO resource. Often used to form comprehensive error messages where only a BasicIo instance is available.

putb()

Write one byte to the IO source. Current IO position is advanced by one byte.

Parameters:

data (int) – The single byte to be written.

Return type:

int

Returns:

The value of the byte written if successful;

EOF if failure;

read()

Overload 1:

Read data from the IO source. Reading starts at the current IO position and the position is advanced by the number of bytes read.

Parameters:

rcount (int) – Maximum number of bytes to read. Fewer bytes may be read if rcount bytes are not available.

Return type:

DataBuf

Returns:

DataBuf instance containing the bytes read. Use the DataBuf.size_ member to find the number of bytes read. DataBuf.size_ will be 0 on failure.

Overload 2:

Read data from the IO source. Reading starts at the current IO position and the position is advanced by the number of bytes read.

Parameters:
  • buf (writeable bytes-like object) – Pointer to a block of memory into which the read data is stored. The memory block must be at least rcount bytes long.

  • rcount (int) – Maximum number of bytes to read. Fewer bytes may be read if rcount bytes are not available.

Return type:

int

Returns:

Number of bytes read from IO source successfully;

0 if failure;

readOrThrow()

Safe version of read() that checks for errors and throws an exception if the read was unsuccessful.

Parameters:
  • buf (writeable bytes-like object) – Pointer to a block of memory into which the read data is stored. The memory block must be at least rcount bytes long.

  • rcount (int) – Maximum number of bytes to read. Fewer bytes may be read if rcount bytes are not available.

  • err (ErrorCode, optional) – Error code to use if an exception is thrown.

seek()

Move the current IO position.

Parameters:
  • offset (int) – Number of bytes to move the position relative to the starting position specified by pos

  • pos (BasicIo.Position) – Position from which the seek should start

Return type:

int

Returns:

0 if successful;

Nonzero if failure;

seekOrThrow()

Safe version of seek() that checks for errors and throws an exception if the seek was unsuccessful.

Parameters:
  • offset (int) – Number of bytes to move the position relative to the starting position specified by pos

  • pos (BasicIo.Position) – Position from which the seek should start

  • err (ErrorCode) – Error code to use if an exception is thrown.

size()

Get the current size of the IO source in bytes.

Return type:

int

Returns:

Size of the IO source in bytes;

-1 if failure;

tell()

Get the current IO position.

Return type:

int

Returns:

Offset from the start of IO

transfer()

Remove all data from this object’s IO source and then transfer data from the src BasicIo object into this object.

The source object is invalidated by this operation and should not be used after this method returns. This method exists primarily to be used with the BasicIo::temporary() method.

Parameters:

src (BasicIo) – Reference to another BasicIo instance. The entire contents of src are transferred to this object. The src object is invalidated by the method.

Raises:

Exiv2Error In case of failure

write()

Overload 1:

Write data to the IO source. Current IO position is advanced by the number of bytes written.

Parameters:
  • data (bytes-like object) – Pointer to data. Data must be at least wcount bytes long

  • wcount (int) – Number of bytes to be written.

Return type:

int

Returns:

Number of bytes written to IO source successfully;

0 if failure;

Overload 2:

Write data that is read from another BasicIo instance to the IO source. Current IO position is advanced by the number of bytes written.

Parameters:

src (BasicIo) – Reference to another BasicIo instance. Reading start at the source’s current IO position

Return type:

int

Returns:

Number of bytes written to IO source successfully;

0 if failure;