diff options
author | S. Solomon Darnell | 2025-03-28 21:52:21 -0500 |
---|---|---|
committer | S. Solomon Darnell | 2025-03-28 21:52:21 -0500 |
commit | 4a52a71956a8d46fcb7294ac71734504bb09bcc2 (patch) | |
tree | ee3dc5af3b6313e921cd920906356f5d4febc4ed /.venv/lib/python3.12/site-packages/filetype/filetype.py | |
parent | cc961e04ba734dd72309fb548a2f97d67d578813 (diff) | |
download | gn-ai-master.tar.gz |
Diffstat (limited to '.venv/lib/python3.12/site-packages/filetype/filetype.py')
-rw-r--r-- | .venv/lib/python3.12/site-packages/filetype/filetype.py | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/.venv/lib/python3.12/site-packages/filetype/filetype.py b/.venv/lib/python3.12/site-packages/filetype/filetype.py new file mode 100644 index 00000000..457152ef --- /dev/null +++ b/.venv/lib/python3.12/site-packages/filetype/filetype.py @@ -0,0 +1,98 @@ +# -*- coding: utf-8 -*- + +from __future__ import absolute_import + +from .match import match +from .types import TYPES, Type + +# Expose supported matchers types +types = TYPES + + +def guess(obj): + """ + Infers the type of the given input. + + Function is overloaded to accept multiple types in input + and peform the needed type inference based on it. + + Args: + obj: path to file, bytes or bytearray. + + Returns: + The matched type instance. Otherwise None. + + Raises: + TypeError: if obj is not a supported type. + """ + return match(obj) if obj else None + + +def guess_mime(obj): + """ + Infers the file type of the given input + and returns its MIME type. + + Args: + obj: path to file, bytes or bytearray. + + Returns: + The matched MIME type as string. Otherwise None. + + Raises: + TypeError: if obj is not a supported type. + """ + kind = guess(obj) + return kind.mime if kind else kind + + +def guess_extension(obj): + """ + Infers the file type of the given input + and returns its RFC file extension. + + Args: + obj: path to file, bytes or bytearray. + + Returns: + The matched file extension as string. Otherwise None. + + Raises: + TypeError: if obj is not a supported type. + """ + kind = guess(obj) + return kind.extension if kind else kind + + +def get_type(mime=None, ext=None): + """ + Returns the file type instance searching by + MIME type or file extension. + + Args: + ext: file extension string. E.g: jpg, png, mp4, mp3 + mime: MIME string. E.g: image/jpeg, video/mpeg + + Returns: + The matched file type instance. Otherwise None. + """ + for kind in types: + if kind.extension == ext or kind.mime == mime: + return kind + return None + + +def add_type(instance): + """ + Adds a new type matcher instance to the supported types. + + Args: + instance: Type inherited instance. + + Returns: + None + """ + if not isinstance(instance, Type): + raise TypeError('instance must inherit from filetype.types.Type') + + types.insert(0, instance) |