Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""Parses the Accept-Language header.
Returns a list of tuples, each like:
(language_tag, qvalue, accept_parameters)
"""
alist, k = parse_qvalue_accept_list( header_value)
if k < len(header_value):
raise ParseError('Accept-Language header is invalid',header_value,k)
langlist = []
for token, langparms, q, acptparms in alist:
if langparms:
raise ParseError('Language tag may not have any parameters',header_value,0)
lang = language_tag( token )
langlist.append( (lang, q, acptparms) )
return langlist
for langtag, qvalue, aargs in accept_list:
if len(langtag) >= 2:
for suptag in langtag.all_superiors( include_wildcard=False ):
if suptag not in all_tags:
# Add in superior at half the qvalue
to_add.append( (suptag, qvalue / 2, '') )
all_tags.append( suptag )
accept_list.extend( to_add )
# Convert server_languages to a list of language_tags
if _is_string(server_languages):
server_languages = [language_tag(server_languages)]
elif isinstance(server_languages, language_tag):
server_languages = [server_languages]
else:
server_languages = [language_tag(lang) for lang in server_languages]
# Select the best one
best = None # tuple (langtag, qvalue, matchlen)
for langtag, qvalue, aargs in accept_list:
# aargs is ignored for Accept-Language
if qvalue <= 0:
continue # UA doesn't accept this language
if ignore_wildcard and langtag.is_universal_wildcard():
continue # "*" being ignored
for svrlang in server_languages:
# The best match is determined first by the quality factor,
# and then by the most specific match.
accept_list.sort()
all_tags = [a[0] for a in accept_list]
if assume_superiors:
to_add = []
for langtag, qvalue, aargs in accept_list:
if len(langtag) >= 2:
for suptag in langtag.all_superiors( include_wildcard=False ):
if suptag not in all_tags:
# Add in superior at half the qvalue
to_add.append( (suptag, qvalue / 2, '') )
all_tags.append( suptag )
accept_list.extend( to_add )
# Convert server_languages to a list of language_tags
if _is_string(server_languages):
server_languages = [language_tag(server_languages)]
elif isinstance(server_languages, language_tag):
server_languages = [server_languages]
else:
server_languages = [language_tag(lang) for lang in server_languages]
# Select the best one
best = None # tuple (langtag, qvalue, matchlen)
for langtag, qvalue, aargs in accept_list:
# aargs is ignored for Accept-Language
if qvalue <= 0:
continue # UA doesn't accept this language
if ignore_wildcard and langtag.is_universal_wildcard():
continue # "*" being ignored
all_tags = [a[0] for a in accept_list]
if assume_superiors:
to_add = []
for langtag, qvalue, aargs in accept_list:
if len(langtag) >= 2:
for suptag in langtag.all_superiors( include_wildcard=False ):
if suptag not in all_tags:
# Add in superior at half the qvalue
to_add.append( (suptag, qvalue / 2, '') )
all_tags.append( suptag )
accept_list.extend( to_add )
# Convert server_languages to a list of language_tags
if _is_string(server_languages):
server_languages = [language_tag(server_languages)]
elif isinstance(server_languages, language_tag):
server_languages = [server_languages]
else:
server_languages = [language_tag(lang) for lang in server_languages]
# Select the best one
best = None # tuple (langtag, qvalue, matchlen)
for langtag, qvalue, aargs in accept_list:
# aargs is ignored for Accept-Language
if qvalue <= 0:
continue # UA doesn't accept this language
if ignore_wildcard and langtag.is_universal_wildcard():
continue # "*" being ignored
for svrlang in server_languages: