diff --git a/mimeparse.py b/mimeparse.py index e579e27..e48cc82 100644 --- a/mimeparse.py +++ b/mimeparse.py @@ -4,7 +4,7 @@ __license__ = 'MIT License' __credits__ = '' -from collections.abc import Generator, Iterable +from typing import Dict, Generator, Iterable, Tuple class MimeTypeParseException(ValueError): @@ -28,15 +28,15 @@ def _parseparam(s: str) -> Generator[str, None, None]: # Vendored version of cgi.parse_header from Python 3.11 (deprecated and slated # for removal in 3.13) -def _parse_header(line: str) -> tuple[str, dict[str, str]]: +def _parse_header(line: str) -> Tuple[str, Dict[str, str]]: """Parse a Content-type like header. - Return the main content-type and a dictionary of options. + Return the main content-type and a Dictionary of options. """ parts = _parseparam(';' + line) key = parts.__next__() - pdict = {} + pDict = {} for p in parts: i = p.find('=') if i >= 0: @@ -45,15 +45,15 @@ def _parse_header(line: str) -> tuple[str, dict[str, str]]: if len(value) >= 2 and value[0] == value[-1] == '"': value = value[1:-1] value = value.replace('\\\\', '\\').replace('\\"', '"') - pdict[name] = value - return key, pdict + pDict[name] = value + return key, pDict -def parse_mime_type(mime_type: str) -> tuple[str, str, dict[str, str]]: +def parse_mime_type(mime_type: str) -> Tuple[str, str, Dict[str, str]]: """Parses a mime-type into its component parts. - Carves up a mime-type and returns a tuple of the (type, subtype, params) - where 'params' is a dictionary of all the parameters for the media range. + Carves up a mime-type and returns a Tuple of the (type, subtype, params) + where 'params' is a Dictionary of all the parameters for the media range. For example, the media range 'application/xhtml;q=0.5' would get parsed into: @@ -75,18 +75,18 @@ def parse_mime_type(mime_type: str) -> tuple[str, str, dict[str, str]]: return (type.strip(), subtype.strip(), params) -def parse_media_range(range: str) -> tuple[str, str, dict[str, str]]: +def parse_media_range(range: str) -> Tuple[str, str, Dict[str, str]]: """Parse a media-range into its component parts. - Carves up a media range and returns a tuple of the (type, subtype, - params) where 'params' is a dictionary of all the parameters for the media + Carves up a media range and returns a Tuple of the (type, subtype, + params) where 'params' is a Dictionary of all the parameters for the media range. For example, the media range 'application/*;q=0.5' would get parsed into: ('application', '*', {'q', '0.5'}) In addition this function also guarantees that there is a value for 'q' - in the params dictionary, filling it in with a proper default if + in the params Dictionary, filling it in with a proper default if necessary. """ (type, subtype, params) = parse_mime_type(range) @@ -102,12 +102,12 @@ def parse_media_range(range: str) -> tuple[str, str, dict[str, str]]: def quality_and_fitness_parsed( mime_type: str, - parsed_ranges: Iterable[tuple[str, str, dict[str, str]]], -) -> tuple[float, float]: + parsed_ranges: Iterable[Tuple[str, str, Dict[str, str]]], +) -> 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 - that have already been parsed by parse_media_range(). Returns a tuple of + that have already been parsed by parse_media_range(). Returns a Tuple of the fitness value and the value of the 'q' quality parameter of the best match, or (-1, 0) if no match was found. Just as for quality_parsed(), 'parsed_ranges' must be a list of parsed media ranges. @@ -150,7 +150,7 @@ def quality_and_fitness_parsed( return float(best_fit_q), best_fitness -def quality_parsed(mime_type: str, parsed_ranges: Iterable[tuple[str, str, dict[str, str]]]) -> float: +def quality_parsed(mime_type: str, parsed_ranges: Iterable[Tuple[str, str, Dict[str, str]]]) -> 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