K i7 dZddlZddlZddlZddlZddlmZddlZddlm Z m Z m Z m Z m Z mZde eefdefdZde eefdefdZde defd Zde eefde fd Zdedefd Zd+de eefd edefd Zej0d+de eefddd edefdZej0 d,de eefded edefdZ d,de eefde ed ede eeffdZ d-de eefdedede ee effdZeedfZej0dedefdZej0dedefdZej0d.dZde deefde efdZeedfZej0dedefdZej0dedefdZej0d.dZde deefde efdZeZ eZ!eZ"de de fd Z#ejHed!Z%dd"dd#d$gfd%e eefd&ed'e eeegeffd(ed)e edef d*Z&y)/aEscaping/unescaping methods for HTML, JSON, URLs, and others. Also includes a few other miscellaneous string manipulation functions that have crept in over time. Many functions in this module have near-equivalents in the standard library (the differences mainly relate to handling of bytes and unicode strings, and were more relevant in Python 2). In new code, the standard library functions are encouraged instead of this module where applicable. See the docstrings on each function for details. N) unicode_type)UnionAnyOptionalDictListCallablevaluereturnc>tjt|S)aEscapes a string so it is valid within HTML or XML. Escapes the characters ``<``, ``>``, ``"``, ``'``, and ``&``. When used in attribute values the escaped strings must be enclosed in quotes. Equivalent to `html.escape` except that this function always returns type `str` while `html.escape` returns `bytes` if its input is `bytes`. .. versionchanged:: 3.2 Added the single quote to the list of escaped characters. .. versionchanged:: 6.4 Now simply wraps `html.escape`. This is equivalent to the old behavior except that single quotes are now escaped as ``'`` instead of ``'`` and performance may be different. )htmlescape to_unicoder s T/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/tornado/escape.py xhtml_escaper's( ;;z%( ))c>tjt|S)aZUn-escapes an XML-escaped string. Equivalent to `html.unescape` except that this function always returns type `str` while `html.unescape` returns `bytes` if its input is `bytes`. .. versionchanged:: 6.4 Now simply wraps `html.unescape`. This changes behavior for some inputs as required by the HTML 5 specification https://html.spec.whatwg.org/multipage/parsing.html#numeric-character-reference-end-state Some invalid inputs such as surrogates now raise an error, and numeric references to certain ISO-8859-1 characters are now handled correctly. )r unescaperrs rxhtml_unescaper>s ==E* ++rcLtj|jddS)zJSON-encodes the given Python object. Equivalent to `json.dumps` with the additional guarantee that the output will never contain the character sequence ```` tag. zReplace all sequences of whitespace chars with a single space.z [\x00-\x20]+ )resubstriprs rsqueezer$ks 66/3 . 4 4 66rplusc~|rtjjntjj}||S)a}Returns a URL-encoded version of the given value. Equivalent to either `urllib.parse.quote_plus` or `urllib.parse.quote` depending on the ``plus`` argument. If ``plus`` is true (the default), spaces will be represented as ``+`` and slashes will be represented as ``%2F``. This is appropriate for query strings. If ``plus`` is false, spaces will be represented as ``%20`` and slashes are left as-is. This is appropriate for the path component of a URL. Note that the default of ``plus=True`` is effectively the reverse of Python's urllib module. .. versionadded:: 3.1 The ``plus`` argument )urllibparse quote_plusquote)r r%r*s r url_escaper+ps+(,FLL # #1C1CE <rencodingcyNr r,r%s r url_unescaper1rcyr.r/r0s rr1r1s rc|<|rt|jdd}tjj |S|rtjj ntjj }|t||S)aODecodes the given value from a URL. The argument may be either a byte or unicode string. If encoding is None, the result will be a byte string and this function is equivalent to `urllib.parse.unquote_to_bytes` if ``plus=False``. Otherwise, the result is a unicode string in the specified encoding and this function is equivalent to either `urllib.parse.unquote_plus` or `urllib.parse.unquote` except that this function also accepts `bytes` as input. If ``plus`` is true (the default), plus signs will be interpreted as spaces (literal plus signs must be represented as "%2B"). This is appropriate for query strings and form-encoded values but not for the path component of a URL. Note that this default is the reverse of Python's urllib module. .. versionadded:: 3.1 The ``plus`` argument +r )r,) to_basestringrr'r(unquote_to_bytes unquote_plusunquote)r r,r%r9s rr1r1sh( !%(00c:E||,,U33/3&,,++9M9M}U+h??rFqskeep_blank_valuesstrict_parsingct|tr|jd}tjj |||dd}i}|j D]'\}}|Dcgc]}|jdc}||<)|Scc}w)a/Parses a query string like urlparse.parse_qs, but takes bytes and returns the values as byte strings. Keys still become type str (interpreted as latin1 in python3!) because it's too painful to keep them as byte strings in python3 and in practice they're nearly always ascii anyway. latin1strict)r,errors) isinstancebytesdecoder'r(parse_qsitemsencode)r:r;r<resultencodedkvis rparse_qs_bytesrLs"e YYx  \\ " " ~#FG 51234Qahhx(4 5 N5s"Bcyr.r/rs rutf8rNr2rcyr.r/rs rrNrNr2rcyr.r/rs rrNrNr2rct|tr|St|tstdt |z|j dS)zConverts a string argument to a byte string. If the argument is already a byte string or None, it is returned unchanged. Otherwise it must be a unicode string and is encoded as utf8. (Expected bytes, unicode, or None; got %rutf-8)rA _UTF8_TYPESr TypeErrortyperFrs rrNrNsA %% e\ *BT%[PQQ <<  rcyr.r/rs rrrr2rcyr.r/rs rrrr2rcyr.r/rs rrrr2rct|tr|St|tstdt |z|j dS)zConverts a string argument to a unicode string. If the argument is already a unicode string or None, it is returned unchanged. Otherwise it must be a byte string and is decoded as utf8. rRrS)rA_TO_UNICODE_TYPESrBrUrVrCrs rrrsB %*+ eU #BT%[PQQ <<  robjc\t|tr6|jDcic]\}}t|t|c}}St|trt d|DSt|t rt d|DSt|t r t|S|Scc}}w)zvWalks a simple data structure, converting byte strings to unicode. Supports lists, tuples, and dictionaries. c32K|]}t|ywr.recursive_unicode.0rKs r z$recursive_unicode..s6Q%a(6c32K|]}t|ywr.r_ras rrcz$recursive_unicode..s7a&q)7rd)rAdictrEr`listtuplerBr)r\rIrJs rr`r` s #tILUv1!!$&7&::UU C 6#666 C 73777 C # VsB(z\b((?:([\w-]+):(/{1,3})|www[.])(?:(?:(?:[^\s&()]|&|")*(?:[^!"#$%&'()*+,.:;<=>?@\[\]^`{|}~\s]))|(?:\((?:[^\s&()]|&|")*\)))+)httphttpstextshorten extra_paramsrequire_protocolpermitted_protocolscrtsdjzdtjdtffd }t t |}tj||S)aConverts plain text into HTML with links. For example: ``linkify("Hello http://tornadoweb.org!")`` would return ``Hello http://tornadoweb.org!`` Parameters: * ``shorten``: Long urls will be shortened for display. * ``extra_params``: Extra text to include in the link tag, or a callable taking the link as an argument and returning the extra text e.g. ``linkify(text, extra_params='rel="nofollow" class="external"')``, or:: def extra_params_cb(url): if url.startswith("http://example.com"): return 'class="internal"' else: return 'class="external" rel="nofollow"' linkify(text, extra_params=extra_params_cb) * ``require_protocol``: Only linkify urls which include a protocol. If this is False, urls such as www.facebook.com will also be linkified. * ``permitted_protocols``: List (or set) of protocols which should be linkified, e.g. ``linkify(text, permitted_protocols=["http", "ftp", "mailto"])``. It is very unsafe to include protocols such as ``javascript``. r mr c|jd}|jd} r|s|S|r| vr|S|jd}|sd|z}t rd |jz}n }d} rt||kDr|}|r.t|dzt|jdxsdz}nd}||dj d }t|dkDr;|d||dzd z|ddd j d dj d dz}t||d zkDr|d|}||k7rE|j d} | |dz kDr|d| }|dz }t|t|k\r|}n|d|zz }d|d|d|dS)Nzhttp://r rir/?.g?&z...z title="%s"z z)groupcallabler#lensplitrfind)rrurlprotohrefparamsmax_len before_clip proto_lenpartsamprnrprorms r make_linkzlinkify..make_linkRsggaj  EJ U"55Jwwqzt#D L !<-3355F!F s3x')KJNS1Ar-BB   O))#.E5zA~  OAhAhrl((-a066s;A>?3x'C-'(7mk!iin1$ds)Cu s8s;//%Cmd22F4&&3%t44r) rr#typingMatchstr_unicoder_URL_REr")rlrmrnrorprs ```` rlinkifyr+sbHH\2\//11 95V\\95c9595| L& 'D ;;y$ ''r)T)rST)FF)r Nr N)'__doc__r rr! urllib.parser' tornado.utilrrrrrrrr rrBrrrrr$boolr+overloadr1rLrVrTrNr[rr native_strr6r`compilerrr/rrrs  % ==*c5j)*c*.,%U +,,* 3s 3s 3 uS%Z(S73737 eCJ'ts& c5j) T  QV  DH e  (+ =A   OS@ e @(0 @GK@ 3:@>TY c5j.2LP #tE{ .d4j!   %         !dC&' !HUO !"4:. c c   e      !eD#u,- !(3- !   330 "** ] 57"&,g%6 f( U f( f(XseSj112f( f( c f(  f(r