rbtools.utils.mimetypes¶
Utilities for working with MIME types.
New in version 5.0.
Functions
|
Guess the MIME type of the given file content. |
|
Return a score for how well the pattern matches the mimetype. |
|
Parse a MIME type into its component parts. |
Classes
A MIME type, parsed into its component parts. |
- class rbtools.utils.mimetypes.MIMEType[source]¶
Bases:
TypedDict
A MIME type, parsed into its component parts.
New in version 5.0.
- sub_type: str¶
Sub-type (for example, “plain” for “text/plain”).
This will include vendor information, if present.
- Type:
- vendor: str¶
The vendor tag, if available.
For example, “vnd.reviewboard.org.test” in “application/vnd.reviewboard.org.test+json”.
- Type:
- format: str¶
The sub-type format, if available.
For example, “json” in “application/vnd.reviewboard.org.test+json”.
- Type:
- __optional_keys__ = frozenset({})¶
- __orig_bases__ = (<function TypedDict>,)¶
- __required_keys__ = frozenset({'format', 'main_type', 'sub_type', 'type', 'vendor'})¶
- __total__ = True¶
- rbtools.utils.mimetypes.parse_mimetype(mime_type: str) MIMEType [source]¶
Parse a MIME type into its component parts.
New in version 5.0.
- Parameters:
mime_type (
str
) – The MIME type to parse.- Returns:
The type, parsed into its component parts.
- Return type:
ParsedMIMEType
- Raises:
ValueError – The given MIME type could not be parsed.
- rbtools.utils.mimetypes.guess_mimetype(data: bytes) str [source]¶
Guess the MIME type of the given file content.
New in version 5.0.
- rbtools.utils.mimetypes.match_mimetype(pattern: MIMEType, test: MIMEType) float [source]¶
Return a score for how well the pattern matches the mimetype.
This is an ordered list of precedence (
_
indicates non-match):Format
Precedence
Type/Vendor+Subtype
2.0
Type/Subtype
1.9
Type/*
1.8
*/Vendor+Subtype
1.7
*/_ +Subtype
1.6
*/*
1.5
_
0
New in version 5.0.
- Parameters:
pattern (
MIMEType
) – A parsed mimetype pattern to score. This is a 3-tuple of the type, subtype, and parameters as returned bymimeparse.parse_mime_type()
. This may include*
wildcards.test (
MIMEType
) – A parsed mimetype to match against the pattern. This is a 3-tuple of the type, subtype, and parameters as returned bymimeparse.parse_mime_type()
.
- Returns:
The resulting score for the match.
- Return type: