def _list_0_or_more__compiled_re(element_re):
# RFC 7230 Section 7 "ABNF List Extension: #rule":
# #element => [ ( "," / element ) *( OWS "," [ OWS element ] ) ]
- return re.compile(
- '^(?:$)|' +
- '(?:' +
- '(?:,|(?:' + element_re + '))' +
- '(?:' + OWS_re + ',(?:' + OWS_re + element_re + ')?)*' +
- ')$',
- )
+ pat = '^(?:$)|(?:(?:,|(?:' + element_re + '))(?:'
+ pat += OWS_re + ',(?:' + OWS_re + element_re + ')?)*)$'
+ return re.compile(pat)
def _list_1_or_more__compiled_re(element_re):
# RFC 7230 Section 7 "ABNF List Extension: #rule":
# 1#element => *( "," OWS ) element *( OWS "," [ OWS element ] )
# and RFC 7230 Errata ID: 4169
- return re.compile(
- '^(?:,' + OWS_re + ')*' + element_re +
- '(?:' + OWS_re + ',(?:' + OWS_re + element_re + ')?)*$',
- )
+ pat = '^(?:,' + OWS_re + ')*' + element_re
+ pat += '(?:' + OWS_re + ',(?:' + OWS_re + element_re + ')?)*$'
+ return re.compile(pat)
class AcceptOffer(namedtuple('AcceptOffer', ['type', 'subtype', 'params'])):
# / ( type "/" "*" )
# / ( type "/" subtype )
# ) *( OWS ";" OWS parameter )
- media_range_re = (
- '(' +
- '(?:' + type_re + '/' + subtype_re + ')' +
- # '*' is included through type_re and subtype_re, so this covers */*
- # and type/*
- ')' +
- '(' +
- '(?:' + OWS_re + ';' + OWS_re +
- '(?![qQ]=)' + # media type parameter cannot be named "q"
- parameter_re + ')*' +
- ')'
- )
+ media_range_re = '((?:' + type_re + '/' + subtype_re + '))((?:'
+ media_range_re += OWS_re + ';' + OWS_re + '(?![qQ]=)' + parameter_re + ')*)'
# accept-params = weight *( accept-ext )
# accept-ext = OWS ";" OWS token [ "=" ( token / quoted-string ) ]
- accept_ext_re = (
- OWS_re + ';' + OWS_re + token_re + '(?:' +
- '=(?:' +
- '(?:' + token_re + ')|(?:' + quoted_string_re + ')' +
- ')' +
- ')?'
- )
+
+ accept_ext_re = OWS_re + ';' + OWS_re + token_re
+ accept_ext_re += '(?:' + '=(?:' + '(?:'
+ accept_ext_re += token_re + ')|(?:' + quoted_string_re + ')))?'
+
accept_params_re = weight_re + '((?:' + accept_ext_re + ')*)'
media_range_n_accept_params_re = media_range_re + '(?:' + \
# For parsing repeated groups within the media type parameters and
# extension parameters segments
parameters_compiled_re = re.compile(
- OWS_re + ';' + OWS_re + '(' + token_re + ')=(' + token_re + '|' +
- quoted_string_re + ')',
- )
- accept_ext_compiled_re = re.compile(
- OWS_re + ';' + OWS_re + '(' + token_re + ')' +
- '(?:' +
- '=(' +
- '(?:' +
- '(?:' + token_re + ')|(?:' + quoted_string_re + ')' +
- ')' +
- ')' +
- ')?',
+ OWS_re + ';' + OWS_re + '(' + token_re + ')=(' + token_re + '|' + quoted_string_re + ')',
)
+ pattern = OWS_re + ';' + OWS_re + '(' + token_re + ')'
+ pattern += '(?:=((?:(?:' + token_re + ')|(?:' + quoted_string_re
+ pattern += '))))?'
+ accept_ext_compiled_re = re.compile(pattern)
+
# For parsing the media types in the `offers` argument to
# .acceptable_offers(), we re-use the media range regex for media types.
# This is not intended to be a validation of the offers; its main purpose
# to do this in steps using multiple regexes.
if cls.accept_compiled_re.match(value) is None:
raise ValueError('Invalid value for an Accept header.')
+
def generator(value):
for match in (
cls.media_range_n_accept_params_compiled_re.finditer(value)
extension_params
):
if token_value:
- if (
- token_value.startswith('"') and
- token_value.endswith('"')
- ):
+ if (token_value.startswith('"') and token_value.endswith('"')):
token_value = cls._process_quoted_string_token(
token=token_value,
)
of the public APIs that uses this method.
"""
# Match if comparisons are the same or either is a complete wildcard
- if (mask.lower() == offer.lower() or
- '*/*' in (mask, offer) or
- '*' == offer):
+ if (mask.lower() == offer.lower() or '*/*' in (mask, offer) or '*' == offer):
return True
# Set mask type with wildcard subtype for malformed masks
# items in reverse order, so specificity 4, 3, 2, 1 correspond
# to 1, 2, 3, 4 in the list, respectively (so that higher
# specificity has higher precedence).
- if (
- offer_type == range_type
- and offer_subtype == range_subtype
- ):
+ if (offer_type == range_type and offer_subtype == range_subtype):
if range_media_type_params == ():
# If offer_media_type_params == () the offer and the
# range match exactly, with neither having media type
# to use one regex to check the match, and another to get the groups.
if cls.accept_charset_compiled_re.match(value) is None:
raise ValueError('Invalid value for an Accept-Charset header.')
+
def generator(value):
for match in (cls.charset_n_weight_compiled_re.finditer(value)):
charset = match.group(1)
'future.',
DeprecationWarning,
)
- for m,q in sorted(
+ for m, q in sorted(
self._parsed_nonzero,
key=lambda i: i[1],
reverse=True
# to use one regex to check the match, and another to get the groups.
if cls.accept_encoding_compiled_re.match(value) is None:
raise ValueError('Invalid value for an Accept-Encoding header.')
+
def generator(value):
for match in (cls.codings_n_weight_compiled_re.finditer(value)):
codings = match.group(1)
DeprecationWarning,
)
- for m,q in sorted(
+ for m, q in sorted(
self._parsed_nonzero,
key=lambda i: i[1],
reverse=True
# to use one regex to check the match, and another to get the groups.
if cls.accept_language_compiled_re.match(value) is None:
raise ValueError('Invalid value for an Accept-Language header.')
+
def generator(value):
for match in (
cls.lang_range_n_weight_compiled_re.finditer(value)
"""
item = item.replace('_', '-').lower()
mask = mask.lower()
- return (mask == '*'
- or item == mask
- or item.split('-')[0] == mask
- or item == mask.split('-')[0]
- )
+ return (
+ mask == '*' or item == mask or # noqa
+ item.split('-')[0] == mask or item == mask.split('-')[0])
def basic_filtering(self, language_tags):
"""
matched_by = mask
return best_offer
- def lookup(
+ def lookup( # noqa
self, language_tags, default_range=None, default_tag=None,
default=None,
):
else: # {non-* range};q=0
not_acceptable_ranges.append(range_.lower())
elif not asterisk_q0_found and range_ == '*': # *;q={not 0}
- asterisk_non0_found = True
+ asterisk_non0_found = True # noqa
# if asterisk_q0_found, then it does not matter whether
# asterisk_non0_found
else: # {non-* range};q={not 0}
break
# len(subtags) >= 2
if len(subtag_before_this) == 1 and (
- subtag_before_this.isdigit() or
- subtag_before_this.isalpha()
+ subtag_before_this.isdigit() or subtag_before_this.isalpha()
): # if subtag_before_this is a single-letter or -digit subtag
subtags.pop(-1) # pop twice instead of once
subtags.pop(-1)