i:Oc@sdZddlZddlZddlmZddlmZmZddlm Z dZ d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbdcdddedfdgdhdidjdkdldmdndodpdqdrdsdtdudvdwdxdydzd{d|d}d~dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddfZ d%Z d&Z d'd(Zd'd)d*d+Zd)d*d,Zded-d-d.Zd/Zd0d1d1d)d*d2Zd3Zded-d-d)d*d4ZdZdS(s  ----------------------- Format Text for Display ----------------------- Functions related to displaying unicode text. Unicode characters don't all have the same width so we need helper functions for displaying them. .. versionadded:: 0.2 kitchen.display API 1.0.0 iN(tb_(t to_unicodetto_bytes(tControlCharErrorcCsd}t|d}|||dks>|||dkrBtSxd||kr||d}|||dkr|d}qE|||dkr|d}qEtSqEWtS(sBinary search in an interval table. :arg value: numeric value to search for :arg table: Ordered list of intervals. This is a list of two-tuples. The elements of the two-tuple define an interval's start and end points. :returns: If :attr:`value` is found within an interval in the :attr:`table` return :data:`True`. Otherwise, :data:`False` This function checks whether a numeric value is present within a table of intervals. It checks using a binary search algorithm, dividing the list of values in half and checking against the values until it determines whether the value is in the table. iii(tlentFalsetTrue(tvaluettabletminimumtmaximumtmid((s8/usr/lib/python2.7/site-packages/kitchen/text/display.pyt_interval_bisearchTs(  iioiiiiiiiiiiiiiiiKi^ipiiiiiiiii0iJiiiiiiii#i%i'i)i-i i i< iA iH iM iQ iT ib ic i i i i i i i i i i< iA iB iG iH iK iM ip iq i i i i i i i i i i i i< i? iA iC iM iV i i i i> i@ iF iH iJ iM iU iV i i i i i i i iA iC iM i i i i i1i4i:iGiNiiiiiiiiii5i7i9iqi~iiiiiiiiii-i0i2i6i7i9i:iXiYii`ii_iii2i4iRiSirisiiiiiiiii i ii i"i'i(i2i9i;iii`iui|iiii4i6i:i<iBiDikisii7iiiiiiiiiiii i i* i. i` ic ij io i i i,i,i-i-i*0i/0i0i0ioi|i}iiii i%i&iĨiii+i-iSiiiiiiiiiiiiiii i&iiiii i i i i i i8 i: i? iiieiiimiiiiiiBiDii iiicCsddddddddddddddddddddddddddd d d d d ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbdcdddedfdgdhdidjdkdldmdndodpdqdrdsdtdudvdwdxdydzd{d|f}g}t}g}xtdd}D]r}t||stjt|r|sD|g}t}qDq|rt}|j|d|j|qqW|rk|j||j|ntt j t|S(~seCombine Markus Kuhn's data with :mod:`unicodedata` to make combining char list :rtype: :class:`tuple` of tuples :returns: :class:`tuple` of intervals of :term:`code points` that are combining character. Each interval is a 2-:class:`tuple` of the starting :term:`code point` and the ending :term:`code point` for the combining characters. In normal use, this function serves to tell how we're generating the combining char list. For speed reasons, we use this to generate a static list and just use that later. Markus Kuhn's list of combining characters is more complete than what's in the python :mod:`unicodedata` library but the python :mod:`unicodedata` is synced against later versions of the unicode database This is used to generate the :data:`~kitchen.text.display._COMBINING` table. iioiiiiiiiiiiiiiiiiiKi^ipiiiiiiiii0iJiiiii i i< iA iH iM iQ iT ib ic i i i i i i i i i i< iA iB iG iH iK iM ip iq i i i i i i i i i i i i< i? iA iC iM iV i i i i> i@ iF iH iJ iM iU iV i i i i i i i iA iC iM i i i i i1i4i:iGiNiiiiiiiiii5i7i9iqi~iiiiiiiiii-i0i2i6i7i9iXiYi`ii_iii2i4iRiSirisiiiiiiiii i ii i"i'i(i2i9i;iiiii4i6i:i<iBikisiiiii i i* i. i` ic ij io i i i*0i/0i0i0ii i%i&iiii i#iiii i i i i i i8 i: i? igiiisiiiiiiBiDii iiiiii(iio(ii(ii(ii(ii(ii(ii(ii(ii(ii(iKi^(ipip(ii(ii(ii(ii(ii(i0iJ(ii(ii(i i (i< i< (iA iH (iM iM (iQ iT (ib ic (i i (i i (i i (i i (i i (i i (i< i< (iA iB (iG iH (iK iM (ip iq (i i (i i (i i (i i (i i (i i (i i (i< i< (i? i? (iA iC (iM iM (iV iV (i i (i i (i i (i> i@ (iF iH (iJ iM (iU iV (i i (i i (i i (i i (i i (iA iC (iM iM (i i (i i (i i (i1i1(i4i:(iGiN(ii(ii(ii(ii(ii(i5i5(i7i7(i9i9(iqi~(ii(ii(ii(ii(ii(i-i0(i2i2(i6i7(i9i9(iXiY(i`i(i_i_(ii(i2i4(iRiS(iris(ii(ii(ii(ii(ii(i i (ii(i i"(i'i((i2i2(i9i;(ii(ii(i4i4(i6i:(i<i<(iBiB(ikis(ii(ii(i i (i* i. (i` ic (ij io (i i (i*0i/0(i0i0(ii(i i (i%i&(ii(ii(i i#(ii(ii(i i (i i (i i (i8 i: (i? i? (igii(isi(ii(ii(iBiD(ii(i i(iii( RtxrangeR t unicodedatat combiningtunichrRtappendttuplet itertoolstimap(tmarkus_kuhn_combining_5_0Rt in_intervaltintervalt codepoint((s8/usr/lib/python2.7/site-packages/kitchen/text/display.pyt_generate_combining_tables                                                   cCsot}d}dGHxO|D]G}|dkr7d}Hn|dkrJdGn|d7}d|}|GqWdGHdS( s;Print out a new :data:`_COMBINING` table This will print a new :data:`_COMBINING` table in the format used in :file:`kitchen/text/display.py`. It's useful for updating the :data:`_COMBINING` table with updated data from a new python as the format won't change from what's already in the file. is_COMBINING = (is is (0x%x, 0x%x),t)N(R(Rtentriestpairtentry((s8/usr/lib/python2.7/site-packages/kitchen/text/display.pyt_print_combining_table$s      tguesscCs|dks$|dkri|dkri|dkrEttdn|d#krUdS|d kredSd St|tr|d Sd |d ko|d kp|dkp|dkp|dkr|dkr|dkp|dkr|dkp|dkr|dkp|dkr|dkp|dkr3|dkp|dkrK|dkp|dkrc|dkp|dkr{|d kp|d!ko|d"kS($sGet the :term:`textual width` of a ucs character :arg ucs: integer representing a single unicode :term:`code point` :kwarg control_chars: specify how to deal with :term:`control characters`. Possible values are: :guess: (default) will take a guess for :term:`control character` widths. Most codes will return zero width. ``backspace``, ``delete``, and ``clear delete`` return -1. ``escape`` currently returns -1 as well but this is not guaranteed as it's not always correct :strict: will raise :exc:`~kitchen.text.exceptions.ControlCharError` if a :term:`control character` is encountered :raises ControlCharError: if the :term:`code point` is a unicode :term:`control character` and :attr:`control_chars` is set to 'strict' :returns: :term:`textual width` of the character. .. note:: It's important to remember this is :term:`textual width` and not the number of characters or bytes. i iitstrictsQ_ucp_width does not understand how to assign a width value to control characters.iiiiiiii_i)#i*#i.iϤi?0iiiiiii0ioii`iiiiii(iii(RRR t _COMBINING(tucst control_chars((s8/usr/lib/python2.7/site-packages/kitchen/text/display.pyt _ucp_width<s0$      sutf-8treplacecCsOt|d|d|}ttjttjtjt|tj|S(sGet the :term:`textual width` of a string :arg msg: :class:`unicode` string or byte :class:`str` to get the width of :kwarg control_chars: specify how to deal with :term:`control characters`. Possible values are: :guess: (default) will take a guess for :term:`control character` widths. Most codes will return zero width. ``backspace``, ``delete``, and ``clear delete`` return -1. ``escape`` currently returns -1 as well but this is not guaranteed as it's not always correct :strict: will raise :exc:`kitchen.text.exceptions.ControlCharError` if a :term:`control character` is encountered :kwarg encoding: If we are given a byte :class:`str` this is used to decode it into :class:`unicode` string. Any characters that are not decodable in this encoding will get a value dependent on the :attr:`errors` parameter. :kwarg errors: How to treat errors encoding the byte :class:`str` to :class:`unicode` string. Legal values are the same as for :func:`kitchen.text.converters.to_unicode`. The default value of ``replace`` will cause undecodable byte sequences to have a width of one. ``ignore`` will have a width of zero. :raises ControlCharError: if :attr:`msg` contains a :term:`control character` and :attr:`control_chars` is ``strict``. :returns: :term:`Textual width` of the :attr:`msg`. This is the amount of space that the string will consume on a monospace display. It's measured in the number of cell positions or columns it will take up on a monospace display. This is **not** the number of glyphs that are in the string. .. note:: This function can be wrong sometimes because Unicode does not specify a strict width value for all of the :term:`code points`. In particular, we've found that some Tamil characters take up to four character cells but we return a lesser amount. tencodingterrors( RtsumRtstarmapR$tizipRtordtrepeat(tmsgR#R&R'((s8/usr/lib/python2.7/site-packages/kitchen/text/display.pyt textual_width~s G c Cst|d|d|}t|}||kr4|St|}||dkr]|d}nd}|}||kr~|}nt|| }xtr||krB|||d}||krPn||||krx8||kr|tt||d}|d8}qW|| S|t|||!}|}|}q||kr|||d}||krpPn||||krx:||krtt||} || }|d7}qW|| S|t|||!}|}|}||kr |}Pq q|| SqW|| S(syGiven a string, return it chopped to a given :term:`textual width` :arg msg: :class:`unicode` string or byte :class:`str` to chop :arg chop: Chop :attr:`msg` if it exceeds this :term:`textual width` :kwarg encoding: If we are given a byte :class:`str`, this is used to decode it into a :class:`unicode` string. Any characters that are not decodable in this encoding will be assigned a width of one. :kwarg errors: How to treat errors encoding the byte :class:`str` to :class:`unicode`. Legal values are the same as for :func:`kitchen.text.converters.to_unicode` :rtype: :class:`unicode` string :returns: :class:`unicode` string of the :attr:`msg` chopped at the given :term:`textual width` This is what you want to use instead of ``%.*s``, as it does the "right" thing with regard to :term:`UTF-8` sequences, :term:`control characters`, and characters that take more than one cell position. Eg:: >>> # Wrong: only displays 8 characters because it is operating on bytes >>> print "%.*s" % (10, 'café ñunru!') café ñun >>> # Properly operates on graphemes >>> '%s' % (textual_width_chop('café ñunru!', 10)) café ñunru >>> # takes too many columns because the kanji need two cell positions >>> print '1234567890\n%.*s' % (10, u'一二三四五六七八九十') 1234567890 一二三四五六七八九十 >>> # Properly chops at 10 columns >>> print '1234567890\n%s' % (textual_width_chop(u'一二三四五六七八九十', 10)) 1234567890 一二三四五 R&R'iii(RR.RRR$R+( R-tchopR&R'twidthR R teosR t new_width((s8/usr/lib/python2.7/site-packages/kitchen/text/display.pyttextual_width_chopsV$               tcCst|}|dk r*t||}nt|}||krl|sN|rdj|||g}qnMd||}|rdj||||g}ndj||||g}|S(s Expand a :class:`unicode` string to a specified :term:`textual width` or chop to same :arg msg: :class:`unicode` string to format :arg fill: pad string until the :term:`textual width` of the string is this length :kwarg chop: before doing anything else, chop the string to this length. Default: Don't chop the string at all :kwarg left: If :data:`True` (default) left justify the string and put the padding on the right. If :data:`False`, pad on the left side. :kwarg prefix: Attach this string before the field we're filling :kwarg suffix: Append this string to the end of the field we're filling :rtype: :class:`unicode` string :returns: :attr:`msg` formatted to fill the specified width. If no :attr:`chop` is specified, the string could exceed the fill length when completed. If :attr:`prefix` or :attr:`suffix` are printable characters, the string could be longer than the fill width. .. note:: :attr:`prefix` and :attr:`suffix` should be used for "invisible" characters like highlighting, color changing escape codes, etc. The fill characters are appended outside of any :attr:`prefix` or :attr:`suffix` elements. This allows you to only highlight :attr:`msg` inside of the field you're filling. .. warning:: :attr:`msg`, :attr:`prefix`, and :attr:`suffix` should all be representable as unicode characters. In particular, any escape sequences in :attr:`prefix` and :attr:`suffix` need to be convertible to :class:`unicode`. If you need to use byte sequences here rather than unicode characters, use :func:`~kitchen.text.display.byte_string_textual_width_fill` instead. This function expands a string to fill a field of a particular :term:`textual width`. Use it instead of ``%*.*s``, as it does the "right" thing with regard to :term:`UTF-8` sequences, :term:`control characters`, and characters that take more than one cell position in a display. Example usage:: >>> msg = u'一二三四五六七八九十' >>> # Wrong: This uses 10 characters instead of 10 cells: >>> u":%-*.*s:" % (10, 10, msg[:9]) :一二三四五六七八九 : >>> # This uses 10 cells like we really want: >>> u":%s:" % (textual_width_fill(msg[:9], 10, 10)) :一二三四五: >>> # Wrong: Right aligned in the field, but too many cells >>> u"%20.10s" % (msg) 一二三四五六七八九十 >>> # Correct: Right aligned with proper number of cells >>> u"%s" % (textual_width_fill(msg, 20, 10, left=False)) 一二三四五 >>> # Wrong: Adding some escape characters to highlight the line but too many cells >>> u"%s%20.10s%s" % (prefix, msg, suffix) u' 一二三四五六七八九十' >>> # Correct highlight of the line >>> u"%s%s%s" % (prefix, display.textual_width_fill(msg, 20, 10, left=False), suffix) u' 一二三四五' >>> # Correct way to not highlight the fill >>> u"%s" % (display.textual_width_fill(msg, 20, 10, left=False, prefix=prefix, suffix=suffix)) u' 一二三四五' uu N(RtNoneR3R.tjoin(R-tfillR/tlefttprefixtsuffixR0textra((s8/usr/lib/python2.7/site-packages/kitchen/text/display.pyttextual_width_fill4sD     cGsdj|}tjd|}t||kr7tSt|d|krQtStt||krmtSt|}||kS(stOptimize the common case when deciding which :term:`textual width` is larger :arg width: :term:`textual width` to compare against. :arg \*args: :class:`unicode` strings to check the total :term:`textual width` of :returns: :data:`True` if the total length of :attr:`args` are less than or equal to :attr:`width`. Otherwise :data:`False`. We often want to know "does X fit in Y". It takes a while to use :func:`textual_width` to calculate this. However, we know that the number of canonically composed :class:`unicode` characters is always going to have 1 or 2 for the :term:`textual width` per character. With this we can take the following shortcuts: 1) If the number of canonically composed characters is more than width, the true :term:`textual width` cannot be less than width. 2) If the number of canonically composed characters * 2 is less than the width then the :term:`textual width` must be ok. :term:`textual width` of a canonically composed :class:`unicode` string will always be greater than or equal to the the number of :class:`unicode` characters. So we can first check if the number of composed :class:`unicode` characters is less than the asked for width. If it is we can return :data:`True` immediately. If not, then we must do a full :term:`textual width` lookup. R4tNFCi(R6Rt normalizeRRRRR.(R0targststringt true_width((s8/usr/lib/python2.7/site-packages/kitchen/text/display.pyt_textual_width_les iFuc sfdt|d|d|}t|d|d|}t|}t|d|d|jd}|jjd}g}|} t} d} d} x|D]} | jd} | | }}| \} } t}| r| rt}n| r| t| krt}n| rG| rG| dkrG| |krGt}qGn|rr|j| jd|} t} n| t| krd} n| r| j d} |} nt || | rt} |j| | |} qnt} | jd}| } | }| r| dkr| }nxn|D]f}t || | rvt| |krv|j| jd|d|} n| |7} | d7} q$W| jdd} qW| r|j| jdn|S( s Works like we want :func:`textwrap.wrap` to work, :arg text: :class:`unicode` string or byte :class:`str` to wrap :kwarg width: :term:`textual width` at which to wrap. Default: 70 :kwarg initial_indent: string to use to indent the first line. Default: do not indent. :kwarg subsequent_indent: string to use to wrap subsequent lines. Default: do not indent :kwarg encoding: Encoding to use if :attr:`text` is a byte :class:`str` :kwarg errors: error handler to use if :attr:`text` is a byte :class:`str` and contains some undecodable characters. :rtype: :class:`list` of :class:`unicode` strings :returns: list of lines that have been text wrapped and indented. :func:`textwrap.wrap` from the |stdlib|_ has two drawbacks that this attempts to fix: 1. It does not handle :term:`textual width`. It only operates on bytes or characters which are both inadequate (due to multi-byte and double width characters). 2. It malforms lists and blocks. csy|jd}Wntk r(dSX|j|}|dkrN|dfS||d}|dps|d}|r||d|fS|dfS(sfReturn the indent to use for this and (possibly) subsequent lines :arg line: :class:`unicode` line of text to process :rtype: tuple :returns: tuple of count of whitespace before getting to the start of this line followed by a count to the following indent if this block of text is an entry in a list. iu -*.o•‣∘i(ii(tstript IndexErrortfind(tlinetchartcounttnxt(t_indent_at_beg(s8/usr/lib/python2.7/site-packages/kitchen/text/display.pyRJs    R&R'u iu iu( RR.trstript expandtabstsplitRRRRtlstripRB(ttextR0tinitial_indenttsubsequent_indentR&R'tsubsequent_indent_widthtlinestrettindentt wrap_lasttcur_sabtcur_spc_indentRFtlast_sabtlast_spc_indenttforce_nltwordstspcstword((RJs8/usr/lib/python2.7/site-packages/kitchen/text/display.pytwrapsp&   !             cOsdjt|||S(sWorks like we want :func:`textwrap.fill` to work :arg text: :class:`unicode` string or byte :class:`str` to process :returns: :class:`unicode` string with each line separated by a newline .. seealso:: :func:`kitchen.text.display.wrap` for other parameters that you can give this command. This function is a light wrapper around :func:`kitchen.text.display.wrap`. Where that function returns a :class:`list` of lines, this function returns one string with each line separated by a newline. u (R6R_(ROR?tkwargs((s8/usr/lib/python2.7/site-packages/kitchen/text/display.pyR73sc Cst|d|d|}t|d|d|}|dk rZt||d|d|}nt|}t|}||kr|s|rdj|||g}qnMd||} |rdj|||| g}ndj| |||g}|S(s%Expand a byte :class:`str` to a specified :term:`textual width` or chop to same :arg msg: byte :class:`str` encoded in :term:`UTF-8` that we want formatted :arg fill: pad :attr:`msg` until the :term:`textual width` is this long :kwarg chop: before doing anything else, chop the string to this length. Default: Don't chop the string at all :kwarg left: If :data:`True` (default) left justify the string and put the padding on the right. If :data:`False`, pad on the left side. :kwarg prefix: Attach this byte :class:`str` before the field we're filling :kwarg suffix: Append this byte :class:`str` to the end of the field we're filling :rtype: byte :class:`str` :returns: :attr:`msg` formatted to fill the specified :term:`textual width`. If no :attr:`chop` is specified, the string could exceed the fill length when completed. If :attr:`prefix` or :attr:`suffix` are printable characters, the string could be longer than fill width. .. note:: :attr:`prefix` and :attr:`suffix` should be used for "invisible" characters like highlighting, color changing escape codes, etc. The fill characters are appended outside of any :attr:`prefix` or :attr:`suffix` elements. This allows you to only highlight :attr:`msg` inside of the field you're filling. .. seealso:: :func:`~kitchen.text.display.textual_width_fill` For example usage. This function has only two differences. 1. it takes byte :class:`str` for :attr:`prefix` and :attr:`suffix` so you can pass in arbitrary sequences of bytes, not just unicode characters. 2. it returns a byte :class:`str` instead of a :class:`unicode` string. R&R'R4t N(RR5R3R.R6( R-R7R/R8R9R:R&R'R0R;((s8/usr/lib/python2.7/site-packages/kitchen/text/display.pytbyte_string_textual_width_fillHs(     RbR7R.R3R<R_(iio(ii(ii(ii(ii(ii(ii(ii(ii(iKi^(ipip(ii(ii(ii(ii(ii(i0iJ(ii(ii(ii(ii#(i%i'(i)i-(i i (i< i< (iA iH (iM iM (iQ iT (ib ic (i i (i i (i i (i i (i i (i i (i< i< (iA iB (iG iH (iK iM (ip iq (i i (i i (i i (i i (i i (i i (i i (i< i< (i? i? (iA iC (iM iM (iV iV (i i (i i (i i (i> i@ (iF iH (iJ iM (iU iV (i i (i i (i i (i i (i i (iA iC (iM iM (i i (i i (i i (i1i1(i4i:(iGiN(ii(ii(ii(ii(ii(i5i5(i7i7(i9i9(iqi~(ii(ii(ii(ii(ii(i-i0(i2i2(i6i7(i9i:(iXiY(ii(i`i(i_i_(ii(i2i4(iRiS(iris(ii(ii(ii(ii(ii(i i (ii(i i"(i'i((i2i2(i9i;(ii(i`i`(iui|(ii(ii(i4i4(i6i:(i<i<(iBiB(iDiD(ikis(ii(i7i7(ii(ii(ii(ii(ii(ii(i i (i* i. (i` ic (ij io (i i (i,i,(i-i-(i*0i/0(i0i0(ioio(i|i}(ii(ii(i i (i%i&(iĨiĨ(ii(i+i-(iSiS(ii(ii(ii(ii(ii(ii(ii(ii(ii(ii(i i&(ii(ii(ii(i i (i i (i i (i8 i: (i? i? (ii(ieii(imi(ii(ii(iBiD(ii(i i(ii(sbyte_string_textual_width_fillsfills textual_widthstextual_width_chopstextual_width_fillswrap(t__doc__RRtkitchenRtkitchen.text.convertersRRtkitchen.text.exceptionsRR R!RRR$R.R3R5RR<RBR_R7Rbt__all__(((s8/usr/lib/python2.7/site-packages/kitchen/text/display.pyt&s  (                                                             d  B SbS +   ;