a
    4ñ»j.  ã                   @   s�   d Z ddlmZmZ ddlZddlZddlZddlZej 	dd¡Z
ddd„Zd	d
„ Zdd„ Zdd„ Zddd„Zddd„Zddd„Zddd„ZdS )z=
Provides some utility functions related to text processing.
é    )Úabsolute_importÚprint_functionNÚBEHAVE_UNICODE_ERRORSÚreplaceú c                 C   s   | | S )z7Creates an indentation prefix string of the given size.© )Úindent_sizeÚpartr   r   ú5E:\soccer\ml_env\Lib\site-packages\behave/textutil.pyÚmake_indentation   s    r   c                    sN   | }d}t | tjƒr |  d¡}n|r6|d  d¡s6d}| ‡ fdd„|D ƒ¡S )zæIndent text or a number of text lines (with newline).

    :param lines:  Text lines to indent (as string or list of strings).
    :param prefix: Line prefix to use (as string).
    :return: Indented text (as unicode string).
    Ú Tr   Ú
c                    s   g | ]}ˆ t  |¡ ‘qS r   )ÚsixÚ	text_type)Ú.0Úline©Úprefixr   r
   Ú
<listcomp>*   ó    zindent.<locals>.<listcomp>)Ú
isinstancer   Ústring_typesÚ
splitlinesÚendswithÚjoin)Útextr   ÚlinesÚnewliner   r   r
   Úindent   s    r   c                 C   s&   d}| D ]}t |ƒ|krt |ƒ}q|S )z£Compute the maximum word size from a list of words (or strings).

    :param words: List of words (or strings) to use.
    :return: Maximum size of all words.
    r   )Úlen)ÚwordsÚmax_sizeÚwordr   r   r
   Úcompute_words_maxsize-   s
    
r#   c                 C   s,   zt  | ¡jdkW S  ty&   Y dS 0 dS )z$Checks if a given encoding is ASCII.ÚasciiFN)ÚcodecsÚlookupÚnameÚLookupError)Úencodingr   r   r
   Úis_ascii_encoding:   s    r*   c                 C   s.   | pt j} t| ddƒpt  ¡ }t|ƒr*dS |S )af  Select the *best* encoding for an output stream/file.
    Uses:
    * ``outstream.encoding`` (if available)
    * ``sys.getdefaultencoding()`` (otherwise)

    Note: If encoding=ascii, uses encoding=UTF-8

    :param outstream:  Output stream to select encoding for (or: stdout)
    :return: Unicode encoding name (as string) to use (for output stream).
    r)   Nzutf-8)ÚsysÚstdoutÚgetattrÚgetdefaultencodingr*   )Ú	outstreamr)   r   r   r
   Úselect_best_encodingA   s
    
r0   c              
   C   sþ   |du rt ƒ }|du rt}t| tjƒr*| S t| tjƒrfzt | ||¡W S  tyb   t | ¡ Y S 0 n”z`tjrºzt | ¡}W qÄ ty¶ } z"t	| ƒ}t |dd¡}W Y d}~qÄd}~0 0 n
t | ¡}W n. tyô } zt |¡}W Y d}~n
d}~0 0 |S dS )aº  Convert into a unicode string.

    :param value:  Value to convert into a unicode string (bytes, str, object).
    :return: Unicode string

    SYNDROMES:
      * Convert object to unicode: Has only __str__() method (Python2)
      * Windows: exception-traceback and encoding=unicode-escape are BAD
      * exception-traceback w/ weird encoding or bytes

    ALTERNATIVES:
      * Use traceback2 for Python2: Provides unicode tracebacks
    Nzunicode-escaper   )
r0   r   r   r   r   Úbinary_typeÚUnicodeErrorÚuÚPY2Ústr)Úvaluer)   ÚerrorsZtext2ÚeÚdatar   r   r
   r   U   s,    	& r   c                    s"   ˆ du rt ƒ ‰ ‡ ‡fdd„| D ƒS )z¶Process a list of string-like objects into list of unicode values.
    Optionally converts binary text into unicode for each item.
    
    :return: List of text/unicode values.
    Nc                    s   g | ]}t |ˆ ˆƒ‘qS r   )r   )r   Úarg©r)   r7   r   r
   r   ”   r   zto_texts.<locals>.<listcomp>)r0   )Úargsr)   r7   r   r;   r
   Úto_textsŒ   s    r=   c                 C   sD   |st | ƒ}tjr| S t| dƒr$| S tjs.J ‚t |¡| ƒ} | S d S )NÚstream)r0   r   ÚPY3Úhasattrr4   r%   Ú	getwriter)r>   r)   r   r   r
   Úensure_stream_with_encoder—   s    

rB   )r   )N)NN)NN)N)Ú__doc__Ú
__future__r   r   r%   Úosr+   r   ÚenvironÚgetr   r   r   r#   r*   r0   r   r=   rB   r   r   r   r
   Ú<module>   s   


7
