labapi.entry.attachment.Attachment#

class labapi.entry.attachment.Attachment(
backing: BinaryIO,
mime_type: str,
filename: str,
caption: str,
)[source]#

Bases: object

Represents an attachment file with associated metadata.

This class wraps a file-like object (such as BytesIO or TemporaryFile) along with metadata like MIME type, filename, and caption. It provides a convenient interface for working with file attachments in LabArchives.

Note

Write operations to the backing buffer need explicit syncing with the server.

__init__(
backing: BinaryIO,
mime_type: str,
filename: str,
caption: str,
)[source]#

Initialize an attachment wrapper.

Parameters:
  • backing – The file-like object that contains the attachment data. Can be a BufferedRandom, BufferedReader, BytesIO, or TemporaryFile.

  • mime_type – The MIME type of the attachment (e.g., “image/png”, “application/pdf”).

  • filename – The filename of the attachment.

  • caption – A descriptive caption for the attachment.

Methods

__init__(backing, mime_type, filename, caption)

Initialize an attachment wrapper.

close()

Close the backing attachment stream.

from_file(file)

Create an attachment by cloning a seekable file object.

read([size])

Read bytes from the backing attachment stream.

seek(offset[, whence])

Move the backing stream cursor.

seekable()

Return whether the backing stream supports random access.

tell()

Return the current backing stream cursor position.

write(data, /)

Write bytes to the backing attachment stream.

Attributes

caption

Return the attachment caption.

filename

Return the attachment filename.

mime_type

Return the attachment MIME type.

static from_file(
file: NamedBinaryIO,
) Attachment[source]#

Create an attachment by cloning a seekable file object.

The content of the provided file is copied into a temporary buffer, making the Attachment independent of the original file’s state. The MIME type is automatically guessed from the local file name. If the MIME type cannot be determined, it defaults to “application/octet-stream”.

Parameters:

file – The file object to create an attachment from. Must have a name attribute.

Returns:

A new Attachment object wrapping a clone of the file.

read(size: int = -1, /) bytes[source]#

Read bytes from the backing attachment stream.

write(data: Buffer, /) int[source]#

Write bytes to the backing attachment stream.

seek(offset: int, whence: int = 0, /) int[source]#

Move the backing stream cursor.

tell() int[source]#

Return the current backing stream cursor position.

close() None[source]#

Close the backing attachment stream.

seekable() bool[source]#

Return whether the backing stream supports random access.

property filename: str#

Return the attachment filename.

Returns:

The filename.

property mime_type: str#

Return the attachment MIME type.

Returns:

The MIME type (e.g., “image/png”, “application/pdf”).

property caption: str#

Return the attachment caption.

Returns:

The caption text.