hfutils.utils.type_

This module provides functionality for determining file types and managing list item types.

It includes an enumeration class for different types of list items, and a function to determine the type of a given file. The module also adds support for the WebP image format.

The module uses the mimetypes library for MIME type detection and imports custom functions for identifying archive, model, and data files.

FileItemType

enum hfutils.utils.type_.FileItemType(value)[source]

Enum class representing different types of list items.

This enumeration defines various file and folder types that can be encountered in a file system or list of items. Each type is associated with a unique integer value.

Usage:
>>> item_type = FileItemType.FILE
>>> print(item_type)
FileItemType.FILE
>>> print(item_type.value)
1

Valid values are as follows:

FILE = <FileItemType.FILE: 1>
FOLDER = <FileItemType.FOLDER: 2>
IMAGE = <FileItemType.IMAGE: 3>
ARCHIVE = <FileItemType.ARCHIVE: 4>
MODEL = <FileItemType.MODEL: 5>
DATA = <FileItemType.DATA: 6>

The Enum and its members also have the following methods:

property render_color

Get the render color based on the item type.

This property returns a color string associated with each item type, which can be used for rendering or display purposes.

Returns:

The render color for the item type.

Return type:

str or None

Raises:

ValueError – If an unknown item type is encountered.

Usage:
>>> item_type = FileItemType.FOLDER
>>> print(item_type.render_color)
blue

get_file_type

hfutils.utils.type_.get_file_type(filename: str | PathLike) FileItemType[source]

Determine the type of a given file.

This function analyzes the provided filename and returns the corresponding FileItemType. It uses various methods to determine the file type, including checking for archives, model files, data files, and image files based on MIME types.

Parameters:

filename (Union[str, os.PathLike]) – The name or path of the file to analyze.

Returns:

The determined FileItemType for the given file.

Return type:

FileItemType

Raises:

TypeError – If the provided filename is not a string or PathLike object.

Usage:
>>> file_type = get_file_type('image.jpg')
>>> print(file_type)
FileItemType.IMAGE
>>> file_type = get_file_type('data.csv')
>>> print(file_type)
FileItemType.DATA