GitHub

API reference for File.

A file part of a form: its content, file name and content type.

Attributes

AttributeTypeDescription
contentFileContent
namestr = 'blob'
content_typestr = 'application/octet-stream'

Functions

__init__

__init__(content, name='blob', content_type='application/octet-stream') -> None
ParameterTypeDescription
contentFileContent
namestr = 'blob'
content_typestr = 'application/octet-stream'

stream

stream() -> Stream_[bytes, FileSourceError]

The content; a source is opened when the stream runs, and its raise or failure is a FileSourceError.

Returns Stream_[bytes, FileSourceError]

def stream(self) -> Stream_[bytes, FileSourceError]:
    """The content; a source is opened when the stream runs, and its
    raise or failure is a ``FileSourceError``."""
    from pyfect import stream as _stream

    content = self.content
    if isinstance(content, bytes):
        return _stream.succeed(content)
    source = content

    def open_source() -> Stream_[bytes, FileSourceError]:
        try:
            opened = source()
        except Exception as error:
            return _stream.fail(FileSourceError(error))
        return opened.map_error(FileSourceError)

    return _stream.suspend(open_source)