-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More documentation and links to GT documentation
- Loading branch information
Showing
10 changed files
with
105 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# SPDX-FileCopyrightText: 2023-present Trenton H <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 | ||
from importlib.util import find_spec | ||
from pathlib import Path | ||
from typing import Dict | ||
from typing import Optional | ||
from typing import Union | ||
|
||
|
||
def optional_to_form(value: Optional[Union[bool, int, float, str]], name: str) -> Dict[str, str]: | ||
""" | ||
Quick helper to convert an optional type into a form data field | ||
with the given name or no changes if the value is None | ||
""" | ||
if value is None: | ||
return {} | ||
else: | ||
return {name: str(value).lower()} | ||
|
||
|
||
def guess_mime_type_stdlib(url: Path) -> Optional[str]: | ||
""" | ||
Uses the standard library to guess a mimetype | ||
""" | ||
import mimetypes | ||
|
||
mime_type, _ = mimetypes.guess_type(url) | ||
return mime_type | ||
|
||
|
||
def guess_mime_type_magic(url: Path) -> Optional[str]: | ||
""" | ||
Uses libmagic to guess the mimetype | ||
""" | ||
import magic # type: ignore | ||
|
||
return magic.from_file(url, mime=True) # type: ignore | ||
|
||
|
||
# Use the best option | ||
guess_mime_type = guess_mime_type_magic if find_spec("magic") is not None else guess_mime_type_stdlib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters