Skip to content

mime_base

Classes:

FileTypeBase

Bases: FileTypeIndexBase, CustomStrEnum

Attributes:

INDEX instance-attribute

File type representing an indexing file.

FileTypeIndex

Bases: FileType

Methods:

  • __call__

    Instantiate FileType.INDEX with its own sub-FileType.

  • is_index

    Verify whether the FileType is an INDEX that holds its own FileType (e.g. mime: index/video).

  • parse

    Parse infos from a file. If the FileType is different than AUTO, this function will throw if the file

Attributes:

ARCHIVE class-attribute instance-attribute

ARCHIVE = 'archive'

File type for archive files.

AUDIO class-attribute instance-attribute

AUDIO = 'audio'

File type for audio files.

AUTO class-attribute instance-attribute

AUTO = 'auto'

Special file type for FileType.parse.

CHAPTERS class-attribute instance-attribute

CHAPTERS = 'chapters'

File type for chapters files.

DOCUMENT class-attribute instance-attribute

DOCUMENT = 'document'

File type for documents.

FONT class-attribute instance-attribute

FONT = 'font'

File type for font files.

IMAGE class-attribute instance-attribute

IMAGE = 'image'

File type for image files.

INDEX class-attribute instance-attribute

INDEX = 'index'

INDEX_AUDIO class-attribute instance-attribute

INDEX_AUDIO = f'{INDEX}_{AUDIO}'

INDEX_VIDEO class-attribute instance-attribute

INDEX_VIDEO = f'{INDEX}_{VIDEO}'

OTHER class-attribute instance-attribute

OTHER = 'other'

File type for generic files, like applications.

VIDEO class-attribute instance-attribute

VIDEO = 'video'

File type for video files.

__call__

__call__(file_type: str | FileType) -> FileTypeIndexWithType

Instantiate FileType.INDEX with its own sub-FileType.

Source code
24
25
26
27
28
def __call__(self, file_type: str | FileType) -> FileTypeIndexWithType:
    """
    Instantiate FileType.INDEX with its own sub-FileType.
    """
    ...

is_index

Verify whether the FileType is an INDEX that holds its own FileType (e.g. mime: index/video).

Source code
364
365
366
367
368
369
def is_index(self) -> TypeGuard[FileTypeIndexWithType]:  # type: ignore
    """
    Verify whether the FileType is an INDEX that holds its own FileType (e.g. mime: index/video).
    """

    return self in {FileType.INDEX, FileType.INDEX_AUDIO, FileType.INDEX_VIDEO}  # type: ignore

parse

parse(
    path: FilePathType,
    *,
    func: FuncExceptT | None = None,
    force_ffprobe: bool | None = None
) -> ParsedFile

Parse infos from a file. If the FileType is different than AUTO, this function will throw if the file is a different FileType than what this method was called on.

Parameters:

  • path

    (FilePathType) –

    Path of the file to be parsed.

  • func

    (FuncExceptT | None, default: None ) –

    Function returned for custom error handling. This should only be set by VS package developers.

:force_ffprobe: Only rely on ffprobe to parse the file info.

Returns:

  • ParsedFile

    ParsedFile object, holding the file's info.

Source code
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
@inject_self.with_args(AUTO)
def parse(
    self, path: FilePathType, *, func: FuncExceptT | None = None, force_ffprobe: bool | None = None
) -> ParsedFile:
    """
    Parse infos from a file. If the FileType is different than AUTO, this function will throw if the file
    is a different FileType than what this method was called on.

    Args:
        path: Path of the file to be parsed.
        func: Function returned for custom error handling. This should only be set by VS package developers.
    :force_ffprobe:     Only rely on ffprobe to parse the file info.

    Returns:
        ParsedFile object, holding the file's info.
    """

    from .ffprobe import FFProbe, FFProbeStream

    filename = Path(str(path)).absolute()

    file_type: FileType | None = None
    mime: str | None = None
    ext: str | None = None

    header = None if force_ffprobe else FileSignatures.parse(filename)

    if header is not None:
        file_type = FileType(header.file_type)
        mime = header.mime
        ext = f".{header.ext}"
    else:
        stream: FFProbeStream | None = None
        ffprobe = FFProbe(func=func)

        try:
            stream = ffprobe.get_stream(filename, FileType.VIDEO)

            if stream is None:
                stream = ffprobe.get_stream(filename, FileType.AUDIO)

            if not stream:
                raise CustomRuntimeError(f"No usable video/audio stream found in {filename}", func)

            file_type = FileType(stream.codec_type)
            mime = f"{file_type.value}/{stream.codec_name}"
        except Exception as e:
            if force_ffprobe:
                raise e
            elif force_ffprobe is None:
                return self.parse(path, force_ffprobe=False)

        if stream is None:
            mime, encoding = guess_mime_type(filename)

            file_type = FileType(mime)

    if ext is None:
        ext = filename.suffix

    encoding = encodings_map.get(filename.suffix, None)

    if not file_type or not mime:
        return ParsedFile(filename, ext, encoding, FileType.OTHER, "file/unknown")

    if self is not FileType.AUTO and self is not file_type:
        raise CustomValueError(
            "FileType mismatch! self is {good}, file is {bad}!", FileType.parse, good=self, bad=file_type
        )

    return ParsedFile(filename, ext, encoding, file_type, mime)

FileTypeIndexBase

Attributes:

INDEX instance-attribute

File type representing an indexing file.

FileTypeIndexWithType

Bases: FileTypeIndex

Methods:

  • __call__

    Instantiate FileType.INDEX with its own sub-FileType.

  • is_index

    Verify whether the FileType is an INDEX that holds its own FileType (e.g. mime: index/video).

  • parse

    Parse infos from a file. If the FileType is different than AUTO, this function will throw if the file

Attributes:

ARCHIVE class-attribute instance-attribute

ARCHIVE = 'archive'

File type for archive files.

AUDIO class-attribute instance-attribute

AUDIO = 'audio'

File type for audio files.

AUTO class-attribute instance-attribute

AUTO = 'auto'

Special file type for FileType.parse.

CHAPTERS class-attribute instance-attribute

CHAPTERS = 'chapters'

File type for chapters files.

DOCUMENT class-attribute instance-attribute

DOCUMENT = 'document'

File type for documents.

FONT class-attribute instance-attribute

FONT = 'font'

File type for font files.

IMAGE class-attribute instance-attribute

IMAGE = 'image'

File type for image files.

INDEX class-attribute instance-attribute

INDEX = 'index'

INDEX_AUDIO class-attribute instance-attribute

INDEX_AUDIO = f'{INDEX}_{AUDIO}'

INDEX_VIDEO class-attribute instance-attribute

INDEX_VIDEO = f'{INDEX}_{VIDEO}'

OTHER class-attribute instance-attribute

OTHER = 'other'

File type for generic files, like applications.

VIDEO class-attribute instance-attribute

VIDEO = 'video'

File type for video files.

file_type instance-attribute

file_type: FileType

Sub-FileType that the index file indexes.

__call__

__call__(file_type: str | FileType) -> FileTypeIndexWithType

Instantiate FileType.INDEX with its own sub-FileType.

Source code
24
25
26
27
28
def __call__(self, file_type: str | FileType) -> FileTypeIndexWithType:
    """
    Instantiate FileType.INDEX with its own sub-FileType.
    """
    ...

is_index

Verify whether the FileType is an INDEX that holds its own FileType (e.g. mime: index/video).

Source code
364
365
366
367
368
369
def is_index(self) -> TypeGuard[FileTypeIndexWithType]:  # type: ignore
    """
    Verify whether the FileType is an INDEX that holds its own FileType (e.g. mime: index/video).
    """

    return self in {FileType.INDEX, FileType.INDEX_AUDIO, FileType.INDEX_VIDEO}  # type: ignore

parse

parse(
    path: FilePathType,
    *,
    func: FuncExceptT | None = None,
    force_ffprobe: bool | None = None
) -> ParsedFile

Parse infos from a file. If the FileType is different than AUTO, this function will throw if the file is a different FileType than what this method was called on.

Parameters:

  • path

    (FilePathType) –

    Path of the file to be parsed.

  • func

    (FuncExceptT | None, default: None ) –

    Function returned for custom error handling. This should only be set by VS package developers.

:force_ffprobe: Only rely on ffprobe to parse the file info.

Returns:

  • ParsedFile

    ParsedFile object, holding the file's info.

Source code
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
@inject_self.with_args(AUTO)
def parse(
    self, path: FilePathType, *, func: FuncExceptT | None = None, force_ffprobe: bool | None = None
) -> ParsedFile:
    """
    Parse infos from a file. If the FileType is different than AUTO, this function will throw if the file
    is a different FileType than what this method was called on.

    Args:
        path: Path of the file to be parsed.
        func: Function returned for custom error handling. This should only be set by VS package developers.
    :force_ffprobe:     Only rely on ffprobe to parse the file info.

    Returns:
        ParsedFile object, holding the file's info.
    """

    from .ffprobe import FFProbe, FFProbeStream

    filename = Path(str(path)).absolute()

    file_type: FileType | None = None
    mime: str | None = None
    ext: str | None = None

    header = None if force_ffprobe else FileSignatures.parse(filename)

    if header is not None:
        file_type = FileType(header.file_type)
        mime = header.mime
        ext = f".{header.ext}"
    else:
        stream: FFProbeStream | None = None
        ffprobe = FFProbe(func=func)

        try:
            stream = ffprobe.get_stream(filename, FileType.VIDEO)

            if stream is None:
                stream = ffprobe.get_stream(filename, FileType.AUDIO)

            if not stream:
                raise CustomRuntimeError(f"No usable video/audio stream found in {filename}", func)

            file_type = FileType(stream.codec_type)
            mime = f"{file_type.value}/{stream.codec_name}"
        except Exception as e:
            if force_ffprobe:
                raise e
            elif force_ffprobe is None:
                return self.parse(path, force_ffprobe=False)

        if stream is None:
            mime, encoding = guess_mime_type(filename)

            file_type = FileType(mime)

    if ext is None:
        ext = filename.suffix

    encoding = encodings_map.get(filename.suffix, None)

    if not file_type or not mime:
        return ParsedFile(filename, ext, encoding, FileType.OTHER, "file/unknown")

    if self is not FileType.AUTO and self is not file_type:
        raise CustomValueError(
            "FileType mismatch! self is {good}, file is {bad}!", FileType.parse, good=self, bad=file_type
        )

    return ParsedFile(filename, ext, encoding, file_type, mime)