black
This commit is contained in:
parent
eae58e5d2a
commit
0e024b46d9
@ -44,6 +44,7 @@ def parse_arg_as_accept(f):
|
|||||||
|
|
||||||
class MediaType:
|
class MediaType:
|
||||||
"""A media type, as used in the Accept header."""
|
"""A media type, as used in the Accept header."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
type_: str,
|
type_: str,
|
||||||
@ -84,15 +85,27 @@ class MediaType:
|
|||||||
@return `self` if the media types are compatible, else `None`
|
@return `self` if the media types are compatible, else `None`
|
||||||
"""
|
"""
|
||||||
mt = MediaType._parse(mime)
|
mt = MediaType._parse(mime)
|
||||||
return self if (
|
return (
|
||||||
|
self
|
||||||
|
if (
|
||||||
# Subtype match
|
# Subtype match
|
||||||
(self.subtype in (mt.subtype, "*") or mt.subtype == "*")
|
(self.subtype in (mt.subtype, "*") or mt.subtype == "*")
|
||||||
# Type match
|
# Type match
|
||||||
and (self.type_ in (mt.type_, "*") or mt.type_ == "*")
|
and (self.type_ in (mt.type_, "*") or mt.type_ == "*")
|
||||||
# Allow disabling wildcards (backwards compatibility with tests)
|
# Allow disabling wildcards (backwards compatibility with tests)
|
||||||
and (allow_type_wildcard or self.type_ != "*" and mt.type_ != "*")
|
and (
|
||||||
and (allow_subtype_wildcard or self.subtype != "*" and mt.subtype != "*")
|
allow_type_wildcard
|
||||||
) else None
|
or self.type_ != "*"
|
||||||
|
and mt.type_ != "*"
|
||||||
|
)
|
||||||
|
and (
|
||||||
|
allow_subtype_wildcard
|
||||||
|
or self.subtype != "*"
|
||||||
|
and mt.subtype != "*"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def has_wildcard(self) -> bool:
|
def has_wildcard(self) -> bool:
|
||||||
@ -125,6 +138,7 @@ class MediaType:
|
|||||||
|
|
||||||
class Matched(str):
|
class Matched(str):
|
||||||
"""A matching result of a MIME string against a MediaType."""
|
"""A matching result of a MIME string against a MediaType."""
|
||||||
|
|
||||||
def __new__(cls, mime: str, m: Optional[MediaType]):
|
def __new__(cls, mime: str, m: Optional[MediaType]):
|
||||||
return super().__new__(cls, mime)
|
return super().__new__(cls, mime)
|
||||||
|
|
||||||
@ -169,12 +183,14 @@ class AcceptList(list):
|
|||||||
@param mimes: Any MIME types to search for in order of preference.
|
@param mimes: Any MIME types to search for in order of preference.
|
||||||
@return A match object with the mime string and the MediaType object.
|
@return A match object with the mime string and the MediaType object.
|
||||||
"""
|
"""
|
||||||
l = sorted([
|
l = sorted(
|
||||||
|
[
|
||||||
(-acc.q, i, j, mime, acc) # Sort by -q, i, j
|
(-acc.q, i, j, mime, acc) # Sort by -q, i, j
|
||||||
for j, acc in enumerate(self)
|
for j, acc in enumerate(self)
|
||||||
for i, mime in enumerate(mimes)
|
for i, mime in enumerate(mimes)
|
||||||
if acc.match(mime)
|
if acc.match(mime)
|
||||||
])
|
]
|
||||||
|
)
|
||||||
return Matched(*(l[0][3:] if l else ("", None)))
|
return Matched(*(l[0][3:] if l else ("", None)))
|
||||||
|
|
||||||
|
|
||||||
@ -192,7 +208,6 @@ def parse_accept(accept: str) -> AcceptList:
|
|||||||
raise InvalidHeader(f"Invalid header value in Accept: {accept}")
|
raise InvalidHeader(f"Invalid header value in Accept: {accept}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def parse_content_header(value: str) -> Tuple[str, Options]:
|
def parse_content_header(value: str) -> Tuple[str, Options]:
|
||||||
"""Parse content-type and content-disposition header values.
|
"""Parse content-type and content-disposition header values.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user