o k`k @sddlZddlZddlZddlZddlZddlZddlZddlmZm Z m Z ddl m Z ddl Z dZGdddeZeZ[ddZdd d Zdd d ZdddZddZedejZedejZdddZdddZdddddddd d!Zeed"d#d$d%DD] Z e!e"e d&e qed'Z#ed(Z$d)d*Z%d+d,Z&dd.d/Z'dd1d2Z(d3d4Z)dd5d6Z*d0+d7d8ed9d:DZ,d;d<Z-dd>d?Z.Gd@dAdAeZ/dBdCZ0dDdEZ1dFdGZ2dHdIZ3dJdKZ4dLdMZ5dNdOZ6e7Z8dPdQZ7ddSdTZ9dUdVZ:e:Z;ddXdYZZ?dZd0d0ej@dRfd_d`ZAdaZBdbdcZCdddeZDdfdgZEddjdkZFdldmZGdndoZHddqdrZIddtduZJdvdwZKddxdyZLdzd{ZMd|d}ZNed~ZOddZPdS)N)quote quote_plusunquoteeast_asian_width c@seZdZdZgZdS)Emptyz3A special tag object evaluating to the empty stringN)__name__ __module__ __qualname____doc__ __slots__rr0/usr/lib/python3/dist-packages/trac/util/text.pyr#src sbddd}fdd}tdddd d d gd ||d }||tjdi|}|jjtd |S)a8Creates a Jinja2 ``Environment`` configured with Trac conventions. All default parameters can optionally be overriden. The ``loader`` parameter is not set by default, so unless it is set by the caller, only inline templates can be created from the environment. :rtype: `jinja.Environment` )htmlrssxmlcSs|durdS|SNr)vrrrfilterout_none9sz!jinja2env..filterout_nonecs|o |dddvS)N.)rsplit)templateextsrrautoescape_extensions;sz(jinja2env..autoescape_extensionsz${}#z##T)z jinja2.ext.dozjinja2.ext.i18nzjinja2.ext.with_) variable_start_stringvariable_end_stringline_statement_prefixline_comment_prefix trim_blocks lstrip_blocks extensionsfinalize autoescapelenNr)dictupdatejinja2 Environmentglobalsr*)kwargsrrdefaultsjenvrrr jinja2env.s(   r3FcKstdd| i||S)aZCreates a Jinja2 ``Template`` from inlined source. :param template: the template content :param text: if set to `False`, the result of the variable expansion will be XML/HTML escaped :param kwargs: additional arguments to pass to `jinja2env`. See `jinja2.Environment` for supported arguments. r(Nr)r3 from_string)rtextr0rrrjinja2templateOs r6cCst|trzt||p dWStyt|dYSwt|trnzt|}Wnty;ddd|jDYSwtj dkrlt|t rO|j rOt |j }nt|t jr^|jr^t |j}nd}|rl|||dd }|St|S) aConvert input to a `str` object. For a `bytes` object, we'll first try to decode the bytes using the given `charset` encoding (or UTF-8 if none is specified), then we fall back to the latin1 encoding which might be correct or not, but at least preserves the original byte sequence by mapping each byte to the corresponding unicode code point in the range U+0000 to U+00FF. For anything else, a simple `str()` conversion is attempted, with special care taken with `Exception` objects. utf-8latin1 cs|]}t|VqdSN to_unicode).0argrrr uzto_unicode..ntN\\\) isinstancebytesstrUnicodeDecodeError Exception UnicodeErrorjoinargsosnameEnvironmentErrorfilenamerepr configparser ParsingErrorsourcereplace)r5charsetresultrTrrrr=]s,        r=cCsRd|jjt|f}|r'ddlm}|ddd}dtd||f}|S)zConvert an `Exception` to a `str` object. In addition to `to_unicode`, this representation of the exception also contains the class name and optionally the traceback. z%s: %sr)get_last_traceback Nz %s %s) __class__r r= trac.utilrXsplitrK)e tracebackmessagerXtraceback_onlyrrrexception_to_unicodes  rbcCs@t|trzt|tWStyt|dYSwt|S)z@Convert a filesystem path to str, using the filesystem encoding.r8)rErFrGsysgetfilesystemencodingrH)pathrrrpath_to_unicodes  rfu \A[\s​]+u [\s​]+\ZTcCs$|rtd|}|rtd|}|S)aStrips unicode white-spaces and ZWSPs from ``text``. :param leading: strips leading spaces from ``text`` unless ``leading`` is `False`. :param trailing: strips trailing spaces from ``text`` unless ``trailing`` is `False`. r)_ws_leading_resub_ws_trailing_re)r5leadingtrailingrrrstripwss   rlcCsftd|}|rdd|dddD|ddd<|r.dd|dddD|ddd<d|S)a!Strips unicode white-spaces and ZWSPs from each line of ``text``. :param leading: strips leading spaces from ``text`` unless ``leading`` is `False`. :param trailing: strips trailing spaces from ``text`` unless ``trailing`` is `False`. z (\n|\r\n|\r)cs|] }td|VqdSrN)rgrhr>linerrrr@z strip_line_ws..Ncsrmrn)rirhrorrrr@rqr)recompiler]rK)r5rjrklinesrrr strip_line_wss "" rvrCz\"z\bz\fz\nz\rz\tz\')rD" rY  ' cCsg|]}t|qSr)ordr>crrr ru &<>

z\u%04xu#[\x00-\x1f\\"\b\f\n\r\t\'&<>

]u![\x00-\x1f\\"\b\f\n\r\t&<>

]cCs|sdSdd}t||S)z[Quote strings for inclusion in single or double quote delimited Javascript strings rcSt|dSNr _js_quotegroupmatchrrrrUz!javascript_quote..replace) _js_quote_rerhr5rUrrrjavascript_quotes rcCs |sdSdd}dt||S)zhEmbed the given string in a double quote delimited Javascript string (conform to the JSON spec) z""cSrrrrrrrrUrzto_js_string..replacez"%s") _js_string_rerhrrrr to_js_stringsr/cC tt|tr ||St||S)a/A unicode aware version of `urllib.quote` :param value: anything that converts to a `bytes`. If `str` input is given, it will be UTF-8 encoded. :param safe: as in `quote`, the characters that would otherwise be quoted but shouldn't here (defaults to '/') )rrErFrGvaluesaferrr unicode_quotes rrcCr)aKA unicode aware version of `urllib.quote_plus`. :param value: anything that converts to a `bytes`. If `str` input is given, it will be UTF-8 encoded. :param safe: as in `quote_plus`, the characters that would otherwise be quoted but shouldn't here (defaults to '/') )rrErFrGrrrrunicode_quote_pluss rcCs"t|tr |d}t|dddS)zA unicode aware version of `urllib.unquote`. :param value: UTF-8 encoded `str` value (for example, as obtained by `unicode_quote`). :rtype: `str` r8r7strict)encodingerrors)rErFdecoder)rrrrunicode_unquotes  rcCsrt|trt|ddd}g}|D] \}}|tur$|t||q|t||dt||qd|S)zA unicode aware version of `urllib.urlencode`. Values set to `empty` are converted to the key alone, without the equal sign. cSs|dSrr)irrrsz#unicode_urlencode..)key=&)rEr+sorteditemsemptyappendrrK)paramsrlkrrrrunicode_urlencodes   rccr:r;)chrrrrrr@rAr@!cCs t|tS)z#Quote strings for query string )r_qs_quote_safer5rrrquote_query_strings rr8c Cslt|tr-zt|d}W|Sty,zt||}Wnty)t|d}YnwYnwt|}|dS)zConvert input to a UTF-8 `bytes` object. If the input is not an `str` object, we assume the encoding is already UTF-8, ISO Latin-1, or as specified by the optional *charset* parameter. r7r8)rErFrGrJr=encode)r5rVurrrto_utf8s     rc@seZdZdZddZdS)unicode_passwdz?Conceal the actual content of the string when `repr` is called.cCsdS)Nz*******r)selfrrr__repr__4szunicode_passwd.__repr__N)r r r r rrrrrr2s rcCst|dd}|dvr |SdS)z5Return the appropriate encoding for the given stream.rN)Ncp0r7)getattr)streamrrrrstream_encoding8s rcOs8|ddd|D|ddr|ddSdS)zOutput the given arguments to the console, encoding the output as appropriate. :param kwargs: ``newline`` controls whether a newline will be appended (defaults to `True`) r9csr:r;r<)r>arrrr@FrAz console_print..newlineTrYN)writerKget)outrLr0rrr console_print?s rcOttjg|Ri|dS)z%Do a `console_print` on `sys.stdout`.N)rrcstdoutrLr0rrrprintoutKrcOr)z%Do a `console_print` on `sys.stderr`.N)rrcstderrrrrrprinterrPrrcO*|r||;}t|fi|tjdS)zUFormat `message`, do a `console.print` on `sys.stdout` and flush the buffer. N)rrcrflushr`rLr0rrr printfoutUrcOr)zUFormat `message`, do a `console.print` on `sys.stderr` and flush the buffer. N)rrcrrrrrr printferr_rrcCst|ddtttjjS)zSInput one line from the console and converts it to unicode as appropriate. F)r)rr=inputrcstdinr)promptrrr raw_inputis rcCstS)zReturn the encoding, which is retrieved on ahead, according to user preference. We should use this instead of `locale.getpreferredencoding()` which is not thread-safe.)_preferredencodingrrrrgetpreferredencodingssrrcs*|dkrdndtfddt|DS)aPDetermine the column width of `text` in Unicode characters. The characters in the East Asian Fullwidth (F) or East Asian Wide (W) have a column width of 2. The other characters in the East Asian Halfwidth (H) or East Asian Narrow (Na) have a column width of 1. That `ambiwidth` parameter is used for the column width of the East Asian Ambiguous (A). If `1`, the same width as characters in US-ASCII. This is expected by most users. If `2`, twice the width of US-ASCII characters. This is expected by CJK users. cf. http://www.unicode.org/reports/tr11/. rrFWAFWcs g|] }t|vr dndqS)rrrr)r>rtwicerrrsztext_width..)sumr=)r5 ambiwidthrrr text_width~s rcCstjdkrddl}|jj}|dvrdSd SdD]*}tj|p"d}|rC|dkr4d |vr4|d d}| d r?dSd Sqd S) zhReturn width of East Asian Ambiguous based on locale environment variables or Windows codepage. rBrN)iiiirr)LANGUAGELC_ALL LC_MESSAGESLANGrr:)zhjakor) rMrNctypeswindllkernel32GetConsoleOutputCPenvironrr]lower startswith)rcodepagerNrrrr_get_default_ambiwidths   r cs^|durtj}dur tt|rd|nsdSddfddfdd}|td}fd d t|D}|d tD]Z\}} t| D]2\} } | d |krb| } n |ro|dkrod |} n|} d|| | t| | | f} || qU|d |dkr|r|d|| t ||d qM|d dS)aHPrint data according to a tabular layout. :param data: a sequence of rows; assume all rows are of equal length. :param headers: an optional row containing column headers; must be of the same length as each row in `data`. :param sep: column separator :param out: output file descriptor (`None` means use `sys.stdout`) :param ambiwidth: column width of the East Asian Ambiguous (A). If None, detect ambiwidth with the locale settings. If others, pass to the `ambiwidth` parameter of `text_width`. NrcSs|durdSt|Srr<)valrrrto_textszprint_table..to_textcs t|dSNr)rrrrrtws zprint_table..twcsg}|D]NfddDtddDrMddDtddD}D]}t||kr=|dg|t|7}q*|fddt|Dq|q|S) Ncsg|]}|qSrrr>cellrrrrrz1print_table..to_lines..css|]}d|vVqdS)rYNrrrrrr@rAz0print_table..to_lines..cSsg|]}|qSr) splitlinesrrrrrrcsr:r;r)rrrrr@rArc3s"|] fddDVqdS)csg|]}|qSrrridxrrrrz;print_table..to_lines...Nrr>rowrrr@s)anymaxr*extendranger)dataru max_linesrrrrto_liness  zprint_table..to_linescs$g|]tfddDqS)c3s|] }|VqdSr;r)r>r)rrrrr@rqz)print_table...)rr)rrrrrszprint_table..rYrr9z%-*s%s-) rcr_default_ambiwidthlistinsertr*rr enumerater)rheadersseprrrnum_cols col_widthridxrcidxrrpspr)rrrrr print_tablesF            r KcCs^t|pd|kr |Sd}|t|}t|dd||dd|}|dkr'|}|d||S)zTruncates `text` to length less than or equal to `maxlen` characters. This tries to be (a bit) clever and attempts to find a proper word boundary for doing so. rz ...r9rrYN)r*rrfind)r5maxlensuffix maxtextlencutrrr shorten_lines rc@sNeZdZgdZdZdZeddZ  ddd Zd d Z d d Z ddZ dS)UnicodeTextWrapper))ii)i.i.)i0i?0)i@0i0)i0i0)i01i1)i1i1)i1i1)i2i2)i3i3)i4iM)iNi)i`i)ii)ii)ii)i0iO)ii)iiu[-][-])iiu[-][-]NcCsg}|jD]}t|d}t|d}|d||fqd|}d|}td|ddd tj|_td |tj|_dS) Nrrz%s-%srz[%s]+z(\s+||z$[^\s\w]*\w+[^0-9\W]-(?=\w+[^0-9\W])|z#(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))z\A) breakable_char_rangesrrrKrsrtUNICODEsplit_re breakable_re)cls char_rangesrhighlowpatternrrr_init_patterns"s(    z!UnicodeTextWrapper._init_patternsrrrcCs8tjj||dd||d||_|jdur|dSdS)Nr)replace_whitespacebreak_long_wordsinitial_indentsubsequent_indent)textwrap TextWrapper__init__rrr)rcolsr r!r"r#rrrrr&4s  zUnicodeTextWrapper.__init__cCs|jt|}ttd|Sr;)rr]r=rfilter)rr5chunksrrr_split>szUnicodeTextWrapper._splitcCst||jdSr)rr)rr5rrr _text_widthBrzUnicodeTextWrapper._text_widthc Csjg}||j}|rg}d}|r|j}n|j}|j||}|ddkr,|r,|d=|r|d}||} || |krH|||| 7}n@|j |r||} t t |D]} ||| } | | krfn| | 8} qX| dkr||d| || d}||d<||} nn|s.|r| |kr| |||||r|ddkr|d=|r||d ||s |S)Nrr)reverser+r#r"widthstriprpoprrrr*_handle_long_wordrK) rr)rurcur_line cur_widthindentr.chunkw left_spacerrrr _wrap_chunksEsR       ,zUnicodeTextWrapper._wrap_chunks)rrrrr) r r r rrr classmethodrr&r*r+r8rrrrrs    rc Csb|dddd}t|dd|||d}g}|dD]}|||p)dg7}q||S)aWraps the single paragraph in `t`, which contains unicode characters. The every line is at most `cols` characters long. That `ambiwidth` parameter is used for the column width of the East Asian Ambiguous (A). If `1`, the same width as characters in US-ASCII. This is expected by most users. If `2`, twice the width of US-ASCII characters. This is expected by CJK users. rrYrzr)r r!r"r#rr)r/rUrr]wraprstriprK) tr'r"r#lineseprwrapper wrappedLinesrprrrr:ys  r:u@…cCs@|r|d}|dkr|d|t|ddkrdSdS|S)uqReplace anything looking like an e-mail address (``'@something'``) with a trailing ellipsis (``'@…'``) @r,N>r)find_obfuscation_char)addressatrrrobfuscate_email_addresss rFcCst|vS)z_Returns `True` if the `word` looks like an obfuscated e-mail address. :since: 1.2 )rC)wordrrr is_obfuscatedsrHcCsF|s|Sd}|drd}|dd}||dddddd S) z_Make a path breakable after path separators, and conversely, avoid breaking at spaces. rrrNu/​rDu\​r9 )rrU)reprefixrrrbreakable_paths  rKrI​cCs8|s|S|D]}||d}q|D]}||d}q|S)zrNormalize whitespace in a string, by replacing special spaces by normal spaces and removing zero-width spaces.r9r)rU)r5to_spaceremoveeachrrrnormalize_whitespacesrPcCs0|r|ddvr|d|dkr|ddS|S)zYRemove (one level of) enclosing single or double quotes. .. versionadded :: 1.0 rz'"r,rr)txtrrr unquote_labels0rRcCsddlm}||S)z.repl) sub_vars_rerh)r5rLrrrrsub_vars?s  r)Fr;)TT)r)r)r8)r)NrNN)r )rIrL)rU)rbN)T)QrorRlocalerMrsrcr$ urllib.parserrr unicodedatarr-CRLFrGrrr3r6r=rbrfrtrrgrirlrvrrrr setdefaultrrrrrrrrrrKrrrrrrrrrrrrrrrrr rr%rr=r:rCrFrHrKrPrRrSrarcrmrtrvrrrrrrrs  !  '              Kr      %