Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/textutils.cpython-37.pyc

B

�d{T}C�@s�dZdZddlZddlZddlmZddlmZddl	m
Zyddlm
Z
Wnek
rddZ
YnXddlmZd	d
ddd
dddddddddd�Zdddd�Zdd�Ze�d�Ze�d�Zded d!�Zdfd"d#�Zdgd$d%�Zd&d'�Zdhd)d*�Zed+�e�Zd,d-�Zd.d/�Zd0Ze�e�Z d1Z!d2Z"e�d3e!e"f�Z#e�d4e!e"e!f�Z$d5d6d7d8d9d:�Z%d;d5d<d=d>d?�Z&de'e e#fd@dA�Z(e�dB�Z)didDdE�Z*dFZ+dGZ,dHZ-dIdJdKdLdMdNdOdP�Z.dIdQdRdSdTdUdVdWdXdY�	Z/djdZd[�Z0dkd\d]�Z1d^d_d`da�Z2ej3e2fdbdc�Z4dS)laSome text manipulation utility functions.


:group text formatting: normalize_text, normalize_paragraph, pretty_match,unquote, colorize_ansi
:group text manipulation: searchall, splitstrip
:sort: text formatting, text manipulation

:type ANSI_STYLES: dict(str)
:var ANSI_STYLES: dictionary mapping style identifier to ANSI terminal code

:type ANSI_COLORS: dict(str)
:var ANSI_COLORS: dictionary mapping color identifier to ANSI terminal code

:type ANSI_PREFIX: str
:var ANSI_PREFIX:
  ANSI terminal code notifying the start of an ANSI escape sequence

:type ANSI_END: str
:var ANSI_END:
  ANSI terminal code notifying the end of an ANSI escape sequence

:type ANSI_RESET: str
:var ANSI_RESET:
  ANSI terminal code resetting format defined by a previous ANSI escape sequence
zrestructuredtext en�N)�warn)�	normalize)�linesep�
)�
deprecated�!�l�/ZAEz(c)�"Zaez(r)ZoeZOE�O�o�ss)�¡ułu⁄�Æ�©�«�æ�®uœuŒ�Ø�ø�»�ßc	Cs�|dk	rtdtdd�|rd}g}xn|dd�D]^}yt|}WnBtk
r�td|�d}t|�dkr~|dkrztd	��|}YnX|�|�q0Wd�|�S)
aVreplace diacritical characters with their corresponding ascii characters

    Convert the unicode string to its long normalized form (unicode character
    will be transform into several characters) and keep the first one only.
    The normal form KD (NFKD) will apply the compatibility decomposition, i.e.
    replace all compatibility characters with their equivalents.

    :type substitute: str
    :param substitute: replacement character to use if decomposition fails

    :see: Another project about ASCII transliterations of Unicode text
          http://pypi.python.org/pypi/Unidecode
    NzDignorenonascii is deprecated, use substitute named parameter instead�)�
stacklevel�ZNFKDr�z*can't deal with non-ascii based characters)	r�DeprecationWarning�MANUAL_UNICODE_MAP�KeyError�
_uninormalize�ord�
ValueError�append�join)ZustringZignorenonascii�
substitute�res�letter�replacement�r(�I/opt/alt/python37/lib/python3.7/site-packages/logilab/common/textutils.py�
unormalizeKs"

r*cCs<|s|S|ddkr |dd�}|ddkr8|dd�}|S)z�remove optional quotes (simple or double) from the string

    :type string: str or unicode
    :param string: an optionally quoted string

    :rtype: str or unicode
    :return: the unquoted string (or the input string if it wasn't quoted)
    rz"'�N���r()�stringr(r(r)�unquotels	r.z
?

?
z\s+�PrFcCsL|r
t}nt}g}x$t�|�D]}|�||||��qWdt|tf�|�S)a�normalize a text to display it with a maximum line size and
    optionally arbitrary indentation. Line jumps are normalized but blank
    lines are kept. The indentation string may be used to insert a
    comment (#) or a quoting (>) mark  for instance.

    :type text: str or unicode
    :param text: the input text to normalize

    :type line_len: int
    :param line_len: expected maximum line's length, default to 80

    :type indent: str or unicode
    :param indent: optional string to use as indentation

    :rtype: str or unicode
    :return:
      the input text normalized to fit on lines with a maximized size
      inferior to `line_len`, and optionally prefixed by an
      indentation string
    z%s%s%s)�normalize_rest_paragraph�normalize_paragraph�_BLANKLINES_RGX�splitr"rr#)�text�line_len�indent�rest�normp�resultr(r(r)�normalize_text�sr:cCsPt�d|�}|t|�}g}x(|rDt|��|�\}}|�||�qWt�|�S)a�normalize a text to display it with a maximum line size and
    optionally arbitrary indentation. Line jumps are normalized. The
    indentation string may be used top insert a comment mark for
    instance.

    :type text: str or unicode
    :param text: the input text to normalize

    :type line_len: int
    :param line_len: expected maximum line's length, default to 80

    :type indent: str or unicode
    :param indent: optional string to use as indentation

    :rtype: str or unicode
    :return:
      the input text normalized to fit on lines with a maximized size
      inferior to `line_len`, and optionally prefixed by an
      indentation string
    � )�_NORM_SPACES_RGX�sub�len�	splittext�stripr"rr#)r4r5r6�linesZaliner(r(r)r1�sr1cCs�d}g}|t|�}x�|��D]v}|t�d|���}d}xBt|�|kr|t||�\}}|�||�|rv|d}d}q<d}q<W|r|�||���qWt�|�S)a�normalize a ReST text to display it with a maximum line size and
    optionally arbitrary indentation. Line jumps are normalized. The
    indentation string may be used top insert a comment mark for
    instance.

    :type text: str or unicode
    :param text: the input text to normalize

    :type line_len: int
    :param line_len: expected maximum line's length, default to 80

    :type indent: str or unicode
    :param indent: optional string to use as indentation

    :rtype: str or unicode
    :return:
      the input text normalized to fit on lines with a maximized size
      inferior to `line_len`, and optionally prefixed by an
      indentation string
    rr;)	r>�
splitlinesr<r=r@r?r"rr#)r4r5r6ZtoreportrA�liner(r(r)r0�s r0cCs�t|�|kr|dfStt|�d|�}x |dkrF||dkrF|d8}q(W|dkr�tt|�|�}x$t|�|kr�||dkr�|d7}q`W|d|�||dd���fS)z�split the given text on space according to the given max line size

    return a 2-uple:
    * a line <= line_len if possible
    * the rest of the text which has to be reported on another line
    rr+rr;N)r>�minr@)r4r5�posr(r(r)r?�sr?�,cCsdd�|�|�D�S)a�return a list of stripped string by splitting the string given as
    argument on `sep` (',' by default). Empty string are discarded.

    >>> splitstrip('a, b, c   ,  4,,')
    ['a', 'b', 'c', '4']
    >>> splitstrip('a')
    ['a']
    >>>

    :type string: str or unicode
    :param string: a csv line

    :type sep: str or unicode
    :param sep: field separator, default to the comma (',')

    :rtype: str or unicode
    :return: the unquoted string (or the input string if it wasn't quoted)
    cSsg|]}|��r|���qSr()r@)�.0�wordr(r(r)�
<listcomp>szsplitstrip.<locals>.<listcomp>)r3)r-�sepr(r(r)�
splitstrip�srKz%get_csv is deprecated, use splitstripcCs,d|kr|�d��dd�St�|�tj��S)z�return the latest component of a string containing either an url of the
    form <scheme>://<path> or a local file system path
    z://r	r+)�rstrip�rsplit�ospr3rJ)Zurl_or_pathr(r(r)�split_url_or_pathsrOc	Cs�i}|s|Sx�|��D]~}|��}|r|�d�sdd�|�dd�D�\}}||kr�y||�|�Wq�tk
r�|||g||<Yq�Xq|||<qW|S)amparse multilines text containing simple 'key=value' lines and return a
    dict of {'key': 'value'}. When the same key is encountered multiple time,
    value is turned into a list containing all values.

    >>> d = text_to_dict('''multiple=1
    ... multiple= 2
    ... single =3
    ... ''')
    >>> d['single']
    '3'
    >>> d['multiple']
    ['1', '2']

    �#cSsg|]}|���qSr()r@)rG�wr(r(r)rI/sz text_to_dict.<locals>.<listcomp>�=r+)rBr@�
startswithr3r"�AttributeError)r4r%rC�key�valuer(r(r)�text_to_dictsrWz(\s|,)+z#-?(([0-9]+\.[0-9]*)|((0x?)?[0-9]+))z	[a-zA-Z]+z(?P<value>%s)(?P<unit>%s)?z^((%s)(%s))*(%s)?$r+iii@l)�bZkbZmbZgb�tbg-C��6?�<ii�Q)�ms�srD�h�dc
	Cs�|dkr|}t�d|�}|r&t�|�s2td|��g}x�|�|�D]x}|��}	|	d|	�d�}
}||
�}|dk	r�y|||��9}Wn(t	k
r�t	d||�
�f��YnX|�|�qBW|t|��S)amParse the string applying the units defined in units
    (e.g.: "1.5m",{'m',60} -> 80).

    :type string: str or unicode
    :param string: the string to parse

    :type units: dict (or any object with __getitem__ using basestring key)
    :param units: a dict mapping a unit string repr to its value

    :type inter: type
    :param inter: used to parse every intermediate value (need __sum__)

    :type blank_reg: regexp
    :param blank_reg: should match every blank char to ignore.

    :type value_reg: regexp with "value" and optional "unit" group
    :param value_reg: match a value and it's unit into the
    NrzInvalid unit string: %r.rV�unitz#invalid unit %s. valid units are %s)
�	_BLANK_REr=�_VALIDATION_RE�matchr!�finditer�	groupdict�get�lowerr�keysr"�sum)
r-ZunitsZinter�finalZ	blank_regZ	value_regZfstring�valuesrbZdicZlitr_rVr(r(r)�apply_unitsRs$rkz
|
+|
�^c
Cs�|��}|��}t�t|�}|�td|�}|dkr<d}g}n|d|�g}|tt�7}||}d||||}|�t|�}	|	dkr�||d�}|�|�|�|�n>||	tt�d�}|||	�}|�|�|�|�|�|�t�	|��
�S)a�return a string with the match location underlined:

    >>> import re
    >>> print(pretty_match(re.search('mange', 'il mange du bacon'), 'il mange du bacon'))
    il mange du bacon
       ^^^^^
    >>>

    :type match: _sre.SRE_match
    :param match: object returned by re.match, re.search or re.finditer

    :type string: str or unicode
    :param string:
      the string on which the regular expression has been applied to
      obtain the `match` object

    :type underline_char: str or unicode
    :param underline_char:
      character to use to underline the matched section, default to the
      carret '^'

    :rtype: str or unicode
    :return:
      the original string with an inserted line to underline the match
      location
    rr,Nr;)�start�end�	_LINE_RGXr=r�rfindr>�findr"r#rL)
rbr-Zunderline_charrmrnZstart_line_posr9�offset�	underlineZend_line_posr(r(r)�pretty_match|s,



rtz[�mz�0�1�3�4�5�7�9)�resetZboldZitalicrsZblinkZinverseZstrikeZ30Z31Z32Z33Z34Z35Z36Z37)	r}Zblack�red�greenZyellowZblueZmagenta�cyanZwhitecCszg}|r,t|�}x|D]}|�t|�qW|r`|��rR|�ddg�|�|�n|�t|�|rvtd�|�tSdS)areturn ansi escape code corresponding to color and style

    :type color: str or None
    :param color:
      the color name (see `ANSI_COLORS` for available values)
      or the color number when 256 colors are available

    :type style: str or None
    :param style:
      style string (see `ANSI_COLORS` for available values). To get
      several style effects at the same time, use a coma as separator.

    :raise KeyError: if an unexistent color or style identifier is given

    :rtype: str
    :return: the built escape code
    Z38rz�;r)	rKr"�ANSI_STYLES�isdigit�extend�ANSI_COLORS�ANSI_PREFIXr#�ANSI_END)�color�styleZ	ansi_codeZstyle_attrsZeffectr(r(r)�_get_ansi_code�s
r�cCs4|dkr|dkr|St||�}|r0d||tfS|S)aCcolorize message by wrapping it with ansi escape codes

    :type msg: str or unicode
    :param msg: the message string to colorize

    :type color: str or None
    :param color:
      the color identifier (see `ANSI_COLORS` for available values)

    :type style: str or None
    :param style:
      style string (see `ANSI_COLORS` for available values). To get
      several style effects at the same time, use a coma as separator.

    :raise KeyError: if an unexistent color or style identifier is given

    :rtype: str or unicode
    :return: the ansi escaped string
    Nz%s%s%s)r��
ANSI_RESET)�msgr�r�Zescape_coder(r(r)�
colorize_ansi�s
r�r�r~r)�	separator�remove�addcCs�x�|D]�}|dd�dkr0|�t||d��q|ddkrR|�t||d��q|ddkrt|�t||d��q|dd�d	kr�|�t||d��q|dd�d
kr�|�t||d��q|�|�qWdS)N�)z--- z+++ r�r�-r��+r�z--- z+++ )�writer�)rA�outr�rCr(r(r)�diff_colorize_ansis
r�)NN)r/rF)r/r)r/r)rF)rl)NN)NN)5�__doc__�
__docformat__�sys�re�os.path�pathrN�warningsr�unicodedatarr�osr�ImportErrorZlogilab.common.deprecationrrr*r.�compiler2r<r:r1r0r?rKZget_csvrOrWZ
_BLANK_UREr`Z__VALUE_UREZ__UNITS_UREZ	_VALUE_REraZ
BYTE_UNITSZ
TIME_UNITS�floatrkrortr�r�r�r�r�r�r�Z
DIFF_STYLE�stdoutr�r(r(r(r)�<module>+s�

!




)
	 


'

7
!