1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
U
Þ=®d®)ã@sddlmZddlZddlZddlmZGdd„deƒZe d¡Zdd    „Z    Gd
d „d eƒZ
e d ¡Z d d„Z Gdd„deƒZ Gdd„deƒZdS)é)Ú annotationsNé)Ú ImmutableListc@seZdZdZd"dd„Zdd„Zdd„Zd    d
„Zd d „Zd d„Z    dd„Z
dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zd#dd„Zed d!„ƒZdS)$ÚAccepta§An :class:`Accept` object is just a list subclass for lists of
    ``(value, quality)`` tuples.  It is automatically sorted by specificity
    and quality.
 
    All :class:`Accept` objects work similar to a list but provide extra
    functionality for working with the data.  Containment checks are
    normalized to the rules of that header:
 
    >>> a = CharsetAccept([('ISO-8859-1', 1), ('utf-8', 0.7)])
    >>> a.best
    'ISO-8859-1'
    >>> 'iso-8859-1' in a
    True
    >>> 'UTF8' in a
    True
    >>> 'utf7' in a
    False
 
    To get the quality for an item you can use normal item lookup:
 
    >>> print a['utf-8']
    0.7
    >>> a['utf7']
    0
 
    .. versionchanged:: 0.5
       :class:`Accept` objects are forced immutable now.
 
    .. versionchanged:: 1.0.0
       :class:`Accept` internal values are no longer ordered
       alphabetically for equal quality tags. Instead the initial
       order is preserved.
 
    ©csf|dkrt ˆ¡dˆ_nHt|tƒr:|jˆ_t ˆ|¡n(dˆ_t|‡fdd„dd}t ˆ|¡dS)NFTcsˆ |d¡|dfS)Nrr)Ú _specificity)Úx©ÚselfrúUd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\werkzeug/datastructures/accept.pyÚ<lambda>7óz!Accept.__init__.<locals>.<lambda>)ÚkeyÚreverse)ÚlistÚ__init__ÚprovidedÚ
isinstancerÚsorted)r
Úvaluesrr    r r-s
 
 
ÿzAccept.__init__cCs
|dkfS)z3Returns a tuple describing the value's specificity.Ú*r©r
Úvaluerrr r;szAccept._specificitycCs|dkp| ¡| ¡kS)z-Check if a value matches a given accept item.r)Úlower©r
rÚitemrrr Ú_value_matches?szAccept._value_matchescCs t|tƒr| |¡St ||¡S)z½Besides index lookup (getting item n) you can also pass it a string
        to get the quality for the item.  If the item is not in the list, the
        returned quality is ``0``.
        )rÚstrÚqualityrÚ __getitem__©r
rrrr rCs
 
zAccept.__getitem__cCs&|D]\}}| ||¡r|SqdS)zËReturns the quality of the key.
 
        .. versionadded:: 0.6
           In previous versions you had to use the item-lookup syntax
           (eg: ``obj[key]`` instead of ``obj.quality(key)``)
        r©r)r
rrrrrr rLs  
zAccept.qualitycCs$|D]\}}| ||¡rdSqdS)NTFr!)r
rrÚ_qualityrrr Ú __contains__Xs  zAccept.__contains__cCs*d dd„|Dƒ¡}t|ƒj›d|›dS)Nú, css$|]\}}d|›d|›dVqdS)ú(r$ú)Nr)Ú.0rÚyrrr Ú    <genexpr>_sz"Accept.__repr__.<locals>.<genexpr>z([z]))ÚjoinÚtypeÚ__name__)r
Z    pairs_strrrr Ú__repr__^szAccept.__repr__cCsHt|tƒr<t|ƒD] \}\}}| ||¡r|Sqt|ƒ‚t ||¡S)zöGet the position of an entry or raise :exc:`ValueError`.
 
        :param key: The key to be looked up.
 
        .. versionchanged:: 0.5
           This used to raise :exc:`IndexError`, which was inconsistent
           with the list API.
        )rrÚ    enumeraterÚ
ValueErrorrÚindex)r
rÚidxrr"rrr r0bs     
 
z Accept.indexcCs(z | |¡WStk
r"YdSXdS)zaGet the position of an entry or return -1.
 
        :param key: The key to be looked up.
        éÿÿÿÿN)r0r/r rrr Úfindrs z Accept.findccs|D]}|dVqdS)zIterate over all values.rNr)r
rrrr r|sz Accept.valuescCs<g}|D](\}}|dkr&|›d|›}| |¡qd |¡S)z2Convert the header set into an HTTP header string.rz;q=ú,)Úappendr*)r
Úresultrrrrr Ú    to_headers   zAccept.to_headercCs| ¡S©N)r7r    rrr Ú__str__ŠszAccept.__str__cCs*|D] \}}| ||¡r||fSqdSr8r!)r
ÚmatchÚ client_itemrrrr Ú_best_single_matchs  zAccept._best_single_matchNc Csj|}d}d}|D]T}| |¡}|s$q|\}}    | |¡}
|    dks|    |krHq|    |ksX|
|kr|}|    }|
}q|S)aVReturns the best match from a list of possible matches based
        on the specificity and quality of the client. If two items have the
        same quality and specificity, the one is returned that comes first.
 
        :param matches: a list of matches to check for
        :param default: the value that is returned if none match
        r2)r2r)r<r) r
ÚmatchesÚdefaultr6Z best_qualityZbest_specificityZ server_itemr:r;rZ specificityrrr Ú
best_match”s 
 
zAccept.best_matchcCs|r|ddSdS)zThe best match as value.rNrr    rrr Úbest®sz Accept.best)r)N)r,Ú
__module__Ú __qualname__Ú__doc__rrrrrr#r-r0r3rr7r9r<r?Úpropertyr@rrrr r    s"#
     
    
rz /|(?:\s*;\s*)cCst | ¡¡Sr8)Ú_mime_split_reÚsplitr©rrrr Ú_normalize_mime¸srHc@sDeZdZdZdd„Zdd„Zedd„ƒZedd    „ƒZed
d „ƒZ    d S) Ú
MIMEAcceptzRLike :class:`Accept` but with special methods and behavior for
    mimetypes.
    cCstdd„t |¡DƒƒS)Ncss|]}|dkVqdS)rNr)r'rrrr r)Âsz*MIMEAccept._specificity.<locals>.<genexpr>)ÚtuplerErFrrrr rÁszMIMEAccept._specificityc Csìd|kr dSd|kr"td|›ƒ‚t|ƒ}|dd…\}}t|dd…ƒ}|dkrh|dkrhtd|›ƒ‚t|ƒ}|dd…\}}    t|dd…ƒ}
|dkr¤|    dkr¤dS|dkr´|    dkpê|dkrÄ|dkpê||koê|    dkpê|dkpê|    |koê|
|kS)Nú/Fzinvalid mimetype ér)r/rHr) r
rrZnormalized_valueZ
value_typeZ value_subtypeZ value_paramsÚnormalized_itemZ    item_typeZ item_subtypeZ item_paramsrrr rÄs.þÿøzMIMEAccept._value_matchescCsd|kpd|kp|jS)z!True if this object accepts HTML.z    text/htmlúapplication/xhtml+xml)Ú accept_xhtmlr    rrr Ú accept_htmlìsÿzMIMEAccept.accept_htmlcCsd|kpd|kS)z"True if this object accepts XHTML.rNzapplication/xmlrr    rrr rOószMIMEAccept.accept_xhtmlcCsd|kS)z!True if this object accepts JSON.zapplication/jsonrr    rrr Ú accept_jsonøszMIMEAccept.accept_jsonN)
r,rArBrCrrrDrPrOrQrrrr rI¼s(
 
rIz[_-]cCst | ¡¡S)z$Process a language tag for matching.)Ú_locale_delim_rerFrrGrrr Ú_normalize_langsrScs*eZdZdZdd„Zd‡fdd„    Z‡ZS)ÚLanguageAcceptz>Like :class:`Accept` but with normalization for language tags.cCs|dkpt|ƒt|ƒkS)Nr)rSrrrr r    szLanguageAccept._value_matchesNcs|tƒ |¡‰ˆdk    rˆStdd„|Dƒƒ}| |¡‰ˆdk    r@ˆSdd„|Dƒ}tƒ |¡‰ˆdk    rxt‡fdd„|DƒƒS|S)a Given a list of supported values, finds the best match from
        the list of accepted values.
 
        Language tags are normalized for the purpose of matching, but
        are returned unchanged.
 
        If no exact match is found, this will fall back to matching
        the first subtag (primary language only), first with the
        accepted values then with the match values. This partial is not
        applied to any other language subtags.
 
        The default is returned if no exact or fallback match is found.
 
        :param matches: A list of supported languages to find a match.
        :param default: The value that is returned if none match.
        NcSs(g|] }t |dd¡d|df‘qS)rr©rRrF©r'rrrr Ú
<listcomp>(sz-LanguageAccept.best_match.<locals>.<listcomp>cSsg|]}t |d¡d‘qS)rrrUrVrrr rW1sc3s|]}| ˆ¡r|VqdSr8)Ú
startswithrV©r6rr r)7s
z,LanguageAccept.best_match.<locals>.<genexpr>)Úsuperr?rÚnext)r
r=r>ÚfallbackZfallback_matches©Ú    __class__rYr r? s  ÿ
 zLanguageAccept.best_match)N)r,rArBrCrr?Ú __classcell__rrr]r rTsrTc@seZdZdZdd„ZdS)Ú CharsetAcceptz9Like :class:`Accept` but with normalization for charsets.cCs dd„}|dkp||ƒ||ƒkS)NcSs0zt |¡jWStk
r*| ¡YSXdSr8)ÚcodecsÚlookupÚnameÚ LookupErrorr)rcrrr Ú
_normalize@sz0CharsetAccept._value_matches.<locals>._normalizerr)r
rrrerrr r?szCharsetAccept._value_matchesN)r,rArBrCrrrrr r`<sr`)Ú
__future__rraÚreÚ
structuresrrÚcompilerErHrIrRrSrTr`rrrr Ú<module>s  -
B
6