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

    File type for archive files.

  • AUDIO

    File type for audio files.

  • AUTO

    Special file type for :py:attr:FileType.parse.

  • CHAPTERS

    File type for chapters files.

  • DOCUMENT

    File type for documents.

  • FONT

    File type for font files.

  • IMAGE

    File type for image files.

  • INDEX
  • INDEX_AUDIO
  • INDEX_VIDEO
  • OTHER

    File type for generic files, like applications.

  • VIDEO

    File type for video files.

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 :py:attr: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
25
26
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
324
325
326
327
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.

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

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.

Returns:

  • ParsedFile

    ParsedFile object, holding the file's info.

Source code
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
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
@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.

    :param path:        Path of the file to be parsed.
    :param 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.

    :return:            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

    File type for archive files.

  • AUDIO

    File type for audio files.

  • AUTO

    Special file type for :py:attr:FileType.parse.

  • CHAPTERS

    File type for chapters files.

  • DOCUMENT

    File type for documents.

  • FONT

    File type for font files.

  • IMAGE

    File type for image files.

  • INDEX
  • INDEX_AUDIO
  • INDEX_VIDEO
  • OTHER

    File type for generic files, like applications.

  • VIDEO

    File type for video files.

  • file_type (FileType) –

    Sub-FileType that the index file indexes.

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 :py:attr: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
25
26
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
324
325
326
327
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.

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

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.

Returns:

  • ParsedFile

    ParsedFile object, holding the file's info.

Source code
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
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
@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.

    :param path:        Path of the file to be parsed.
    :param 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.

    :return:            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)