Skip to content

Commit

Permalink
feat: Type vendor mimeparse
Browse files Browse the repository at this point in the history
  • Loading branch information
copalco committed Aug 22, 2023
1 parent 30ad9f5 commit 11ca7ca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
35 changes: 22 additions & 13 deletions falcon/vendor/mimeparse/mimeparse.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import cgi
from typing import Dict
from typing import Iterable
from typing import Iterator
from typing import List
from typing import Tuple

__version__ = '1.6.0'
__author__ = 'Joe Gregorio'
__email__ = 'joe@bitworking.org'
__license__ = 'MIT License'
__credits__ = ''

Range = Tuple[str, str, Dict[str, str]]


class MimeTypeParseException(ValueError):
pass


def parse_mime_type(mime_type):
def parse_mime_type(mime_type: str) -> Range:
"""Parses a mime-type into its component parts.
Carves up a mime-type and returns a tuple of the (type, subtype, params)
Expand Down Expand Up @@ -39,7 +46,7 @@ def parse_mime_type(mime_type):
return (type.strip(), subtype.strip(), params)


def parse_media_range(range):
def parse_media_range(range: str) -> Range:
"""Parse a media-range into its component parts.
Carves up a media range and returns a tuple of the (type, subtype,
Expand All @@ -56,7 +63,7 @@ def parse_media_range(range):
:rtype: (str,str,dict)
"""
(type, subtype, params) = parse_mime_type(range)
params.setdefault('q', params.pop('Q', None)) # q is case insensitive
params.setdefault('q', params.pop('Q', '')) # q is case insensitive
try:
if not params['q'] or not 0 <= float(params['q']) <= 1:
params['q'] = '1'
Expand All @@ -66,7 +73,9 @@ def parse_media_range(range):
return (type, subtype, params)


def quality_and_fitness_parsed(mime_type, parsed_ranges):
def quality_and_fitness_parsed(
mime_type: str , parsed_ranges: List[Range]
) -> Tuple[float, float]:
"""Find the best match for a mime-type amongst parsed media-ranges.
Find the best match for a given mime-type against a list of media_ranges
Expand All @@ -77,8 +86,8 @@ def quality_and_fitness_parsed(mime_type, parsed_ranges):
:rtype: (float,int)
"""
best_fitness = -1
best_fit_q = 0
best_fitness = -1.0
best_fit_q = 0.0
(target_type, target_subtype, target_params) = \
parse_media_range(mime_type)

Expand All @@ -98,7 +107,7 @@ def quality_and_fitness_parsed(mime_type, parsed_ranges):
if type_match and subtype_match:

# 100 points if the type matches w/o a wildcard
fitness = type == target_type and 100 or 0
fitness: float = type == target_type and 100 or 0

# 10 points if the subtype matches w/o a wildcard
fitness += subtype == target_subtype and 10 or 0
Expand All @@ -115,12 +124,12 @@ def quality_and_fitness_parsed(mime_type, parsed_ranges):

if fitness > best_fitness:
best_fitness = fitness
best_fit_q = params['q']
best_fit_q = float(params['q'])

return float(best_fit_q), best_fitness
return best_fit_q, best_fitness


def quality_parsed(mime_type, parsed_ranges):
def quality_parsed(mime_type: str, parsed_ranges: List[Range]) -> float:
"""Find the best match for a mime-type amongst parsed media-ranges.
Find the best match for a given mime-type against a list of media_ranges
Expand All @@ -135,7 +144,7 @@ def quality_parsed(mime_type, parsed_ranges):
return quality_and_fitness_parsed(mime_type, parsed_ranges)[0]


def quality(mime_type, ranges):
def quality(mime_type: str, ranges: str) -> float:
"""Return the quality ('q') of a mime-type against a list of media-ranges.
Returns the quality 'q' of a mime-type when compared against the
Expand All @@ -152,7 +161,7 @@ def quality(mime_type, ranges):
return quality_parsed(mime_type, parsed_ranges)


def best_match(supported, header):
def best_match(supported: Iterable[str], header: str) -> str:
"""Return mime-type with the highest quality ('q') from list of candidates.
Takes a list of supported mime-types and finds the best match for all the
Expand Down Expand Up @@ -184,7 +193,7 @@ def best_match(supported, header):
return weighted_matches[-1][0][0] and weighted_matches[-1][2] or ''


def _filter_blank(i):
def _filter_blank(i: Iterable[str]) -> Iterator[str]:
"""Return all non-empty items in the list."""
for s in i:
if s.strip():
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"falcon.middleware",
"falcon.redirects",
"falcon.stream",
"falcon.vendor.*",
]
disallow_untyped_defs = true

Expand Down

0 comments on commit 11ca7ca

Please sign in to comment.