file.utils

Module Contents

Functions

content_type_from_fileobj(→ str)

Gets the content type from a file obj. Depot has this as well, but it

as_fileintent(→ depot.io.utils.FileIntent)

get_supported_image_mime_types(→ set[str])

Queries PIL for all locally supported mime types.

get_svg_size(→ tuple[str | None, str | None])

extension_for_content_type(→ str)

Gets the extension for the given content type. Note that this is

get_image_size(→ tuple[str, str])

digest(→ str)

word_count(→ int)

The word-count of the given text. Goes through the string exactly

name_without_extension(→ str)

current_dir(→ Iterator[None])

Attributes

EXCLUDED_IMAGE_TYPES

IMAGE_MIME_TYPES

IMAGE_MIME_TYPES_AND_SVG

file.utils.content_type_from_fileobj(fileobj: SupportsRead[bytes]) str[source]

Gets the content type from a file obj. Depot has this as well, but it doesn’t use python-magic. We use python-magic to be slower, but more accurate.

file.utils.as_fileintent(content: bytes | IO[bytes], filename: str | None) depot.io.utils.FileIntent[source]
file.utils.get_supported_image_mime_types() set[str][source]

Queries PIL for all locally supported mime types.

Adapted from: https://github.com/python-pillow/Pillow/issues/1182#issuecomment-90572583

file.utils.get_svg_size(svg: IO[bytes]) tuple[str | None, str | None][source]
file.utils.extension_for_content_type(content_type: str, filename: str | None = None) str[source]

Gets the extension for the given content type. Note that this is meant for display only. A file claiming to be a PDF might not be one, but this function would not let you know that.

file.utils.get_image_size(image: PIL.Image.Image) tuple[str, str][source]
file.utils.digest(fileobj: SupportsRead[bytes], type: str = 'sha256', chunksize: int = 4096) str[source]
file.utils.word_count(text: str) int[source]

The word-count of the given text. Goes through the string exactly once and has constant memory usage. Not super sophisticated though.

file.utils.name_without_extension(name: str) str[source]
file.utils.current_dir(dir: _typeshed.StrOrBytesPath) Iterator[None][source]
file.utils.EXCLUDED_IMAGE_TYPES[source]
file.utils.IMAGE_MIME_TYPES[source]
file.utils.IMAGE_MIME_TYPES_AND_SVG[source]