;i/ddlmZddlZddlmZddlZddlZddl m Z Gdde e e e fZejdZdd ZGd deZejdZddZGddeZGddeZdS)) annotationsN) ImmutableListcXeZdZdZ d3d4fd Zd5d Zd6dZejd7dZ ejd8dZ ejd9dZ d:dZ d7dZ d;dZ dd#Zd>> 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. values0Accept | cabc.Iterable[tuple[str, float]] | NonereturnNonecb|)td_dSt|tr/|j_t|dSd_t |fdd}t|dS)NFTcJ|d|dfS)Nrr) _specificity)xselfs RC:\PYTHON\MyICR_Workspace\venv\Lib\site-packages\werkzeug/datastructures/accept.pyz!Accept.__init__..;s"t'8'81'>'>!&E)keyreverse)super__init__provided isinstancersorted)rr __class__s` rrzAccept.__init__/s > GG     !DMMM  ' ' %"ODM GG  V $ $ $ $ $ DMEEEEtF GG  V $ $ $ $ $rvaluestrtuple[bool, ...]c|dkfS)z3Returns a tuple describing the value's specificity.*rrrs rrzAccept._specificity?s ritemboolcb|dkp)||kS)z-Check if a value matches a given accept item.r!)lowerrrr#s r_value_matcheszAccept._value_matchesCs&s{;djjllekkmm;;rrfloatcdSNrrrs r __getitem__zAccept.__getitem__Gs.1crt.SupportsIndextuple[str, float]cdSr+rr,s rr-zAccept.__getitem__IsFIcrslicelist[tuple[str, float]]cdSr+rr,s rr-zAccept.__getitem__KsBE#rstr | t.SupportsIndex | slice3float | tuple[str, float] | list[tuple[str, float]]ct|tr||St||S)zBesides 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``. )rrqualitylistr-r,s rr-zAccept.__getitem__Ms> c3   %<<$$ $c***rcJ|D]\}}|||r|cS dS)zReturns 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)``) rr()rrr#r7s rr7zAccept.qualityXsB"  MD'""3--  qrcH|D]\}}|||rdSdS)NTFr:)rrr#_qualitys r __contains__zAccept.__contains__ds>"  ND(""5$// tt urctdd|D}t|jd|dS)N, c3.K|]\}}d|d|dVdS)(r?)Nr).0rys r z"Accept.__repr__..ks4??$!Qo!ooooo??????rz([z]))jointype__name__)r pairs_strs r__repr__zAccept.__repr__jsBII??$????? t**%666666rstr | tuple[str, float]intct|trAt|D]"\}\}}|||r|cS#t |t ||S)zGet 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( ValueErrorr8index)rridxr#r<s rrPz Accept.indexns{ c3   ")24  %%dH&&sD11JJJS// !zz$$$$rcP ||S#t$rYdSwxYw)zaGet the position of an entry or return -1. :param key: The key to be looked up. )rPrOr,s rfindz Accept.find~s9  ::c?? "   22 s  %%cabc.Iterator[str]c#(K|D] }|dV dS)zIterate over all values.rNr)rr#s rr z Accept.valuess.  Dq'MMMM  rcg}|D]'\}}|dkr|d|}||(d|S)z2Convert the header set into an HTTP header string.rz;q=,)appendrF)rresultrr7s r to_headerzAccept.to_headers]" ! !NE7!|| ..W.. MM% xxrc*|Sr+)r[rs r__str__zAccept.__str__s~~rmatchtuple[str, float] | NonecN|D]!\}}|||r||fcS"dSr+r:)rr_ client_itemr7s r_best_single_matchzAccept._best_single_matchsK$( , , K""5+66 ,"G++++ ,trmatchescabc.Iterable[str] str | NonecdSr+rrrds r best_matchzAccept.best_matchEHSr.defaultcdSr+rrrdrks rrizAccept.best_matchRURUrNc|}d}d}|D]S}||}|s|\}} ||} | dks| |krA| |ks| |kr|}| }| }T|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 rS)rSr)rcr) rrdrkrZ best_qualitybest_specificity server_itemr_rbr7 specificitys rrizAccept.best_matchs .3" / /K++K88E #( K++K88K!||w55%%7G)G)G$& #.  rc&|r|ddSdS)zThe best match as value.rNrr]s rbestz Accept.bests  71: tr)r)r r r r rrr rrrr#rr r$)rrr r))rr.r r/)rr1r r2)rr4r r5)rrr r$)r r)rrKr rL)r rU)r_rr r`rdrer rf.rdrerkrr rr+rdrerkrfr rf)r rf)rH __module__ __qualname____doc__rrr(toverloadr-r7r=rJrPrTr r[r^rcripropertyru __classcell__rs@rrr s!!HJL%%%%%%% <<<<Z111Z1ZIIIZIZEEEZE + + + +     7777%%%%          ZHHHZHZUUUUZUAE8Xrrz /|(?:\s*;\s*)rrr list[str]cZt|Sr+)_mime_split_resplitr&rs r_normalize_mimers    . ..rcjeZdZdZddZdd Zedd Zedd Zedd Z d S) MIMEAcceptzRLike :class:`Accept` but with special methods and behavior for mimetypes. rrr rcdtdt|DS)Nc3"K|] }|dkV dS)r!Nr)rCrs rrEz*MIMEAccept._specificity..s&CC!Q#XCCCCCCr)tuplerrr"s rrzMIMEAccept._specificitys-CC~';';E'B'BCCCCCCrr#r$cd|vrdSd|vrtd|t|}|dd\}}t|dd}|dkr|dkrtd|t|}|dd\}} t|dd} |dkr| dkrdS|dkr| dkp |dko|dkp||ko| dkp|dkp | |ko| |kS)N/Fzinvalid mimetype r!)rOrr) rrr#normalized_value value_type value_subtype value_paramsnormalized_item item_type item_subtype item_paramss rr(zMIMEAccept._value_matchessb d??5 e  :::;; ;+511$4RaR$8! M.qrr233    #!5!5:::;; ;*$//"1"1""5 <_QRR011      3 35#  5,#"5 <c!:ms&:    # #S C'S M1Qk\6Q rcd|vp|jS)z!True if this object accepts HTML.z text/html) accept_xhtmlr]s r accept_htmlzMIMEAccept.accept_htmlsd"7d&77rcd|vpd|vS)z"True if this object accepts XHTML.zapplication/xhtml+xmlzapplication/xmlrr]s rrzMIMEAccept.accept_xhtmls'$.K2Ct2KKrc d|vS)z!True if this object accepts JSON.zapplication/jsonrr]s r accept_jsonzMIMEAccept.accept_json s"T))rNrvrw)r r$) rHr|r}r~rr(rrrrrrrrrsDDDD& & & & P888X8LLLXL***X***rrz[_-]cZt|S)z$Process a language tag for matching.)_locale_delim_rerr&rs r_normalize_langrs  ! !%++-- 0 00rcveZdZdZddZejdd ZejdddZ ddfd ZxZS)LanguageAcceptz>Like :class:`Accept` but with normalization for language tags.rrr#r r$cN|dkpt|t|kS)Nr!)rr's rr(zLanguageAccept._value_matchess&s{Moe448M8MMMrrdrerfcdSr+rrhs rrizLanguageAccept.best_matchrjr.rkcdSr+rrms rrizLanguageAccept.best_match rnrNcLt|Std|D}||Sd|D}t|tfd|DS|S)aGiven 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. Nclg|]1}t|ddd|df2S)rrrrrCr#s r z-LanguageAccept.best_match..@s; O O O$$$T!Wa003T!W = O O OrcPg|]#}t|dd$S)rrrrs rrz-LanguageAccept.best_match..Is.SSS4,224;;A>SSSrc3FK|]}||VdSr+) startswith)rCr#rZs rrEz,LanguageAccept.best_match..Os4LLDOOF4K4KLLLLLLLr)rrirnext)rrdrkfallbackfallback_matchesrZrs @rrizLanguageAccept.best_match"s*##G,,  M  O O$ O O O  $$W--  MTS7SSS##$455  LLLLLLLLL Lrrwrxryrzr+r{) rHr|r}r~r(rrrirrs@rrrsHHNNNNZHHHZHZUUUUZUAE///////////rrceZdZdZd dZdS) CharsetAcceptz9Like :class:`Accept` but with normalization for charsets.rrr#r r$cFdd}|dkp||||kS)Nnamerr c~ tj|jS#t$r|cYSwxYwr+)codecslookupr LookupErrorr&)rs r _normalizez0CharsetAccept._value_matches.._normalizeXsI $}T**// $ $ $zz||### $s <<r!)rrr rr)rrr#rs rr(zCharsetAccept._value_matchesWs@ $ $ $ $ s{Cjj//::d3C3CCCrNrw)rHr|r}r~r(rrrrrTs4CCDDDDDDrr)rrr r) __future__rrcollections.abcabccabcretypingr structuresrrrr)rcompilerrrrrrrrrrrs~""""""  %%%%%%{{{{{]5e, -{{{|,--////=*=*=*=*=*=*=*=*@2:g&&1111 99999V999x D D D D DF D D D D Dr