3
Êb_u  ã               @   sx   d Z ddlmZ ddlZddlZddlZddlmZ eejfdd„ƒZ	ejfdd„Z
d	d
„ Zdd„ Zddefdd„ZdS )zx
Helpers for configuring locale settings.

Name `localization` is chosen to avoid overlap with builtin `locale` module.
é    )ÚcontextmanagerN)Úoptionsc             c   sZ   t jƒ }z>t j|| ƒ t jƒ }tdd„ |D ƒƒr>dj|ƒV  n| V  W dt j||ƒ X dS )aE  
    Context manager for temporarily setting a locale.

    Parameters
    ----------
    new_locale : str or tuple
        A string of the form <language_country>.<encoding>. For example to set
        the current locale to US English with a UTF8 encoding, you would pass
        "en_US.UTF-8".
    lc_var : int, default `locale.LC_ALL`
        The category of the locale being set.

    Notes
    -----
    This is useful when you want to run a particular block of code under a
    particular locale, without globally setting the locale. This probably isn't
    thread-safe.
    c             s   s   | ]}|d k	V  qd S )N© )Ú.0Úxr   r   ú=/tmp/pip-build-jkwsxyiy/pandas/pandas/_config/localization.pyú	<genexpr>'   s    zset_locale.<locals>.<genexpr>Ú.N)ÚlocaleÚ	getlocaleÚ	setlocaleÚallÚjoin)Z
new_localeÚlc_varZcurrent_localeZnormalized_localer   r   r   Ú
set_locale   s    
r   c             C   s@   yt | |d�� W dQ R X W n ttjfk
r6   dS X dS dS )au  
    Check to see if we can set a locale, and subsequently get the locale,
    without raising an Exception.

    Parameters
    ----------
    lc : str
        The locale to attempt to set.
    lc_var : int, default `locale.LC_ALL`
        The category of the locale being set.

    Returns
    -------
    is_valid : bool
        Whether the passed locale can be set
    )r   NFT)r   Ú
ValueErrorr
   ÚError)Úlcr   r   r   r   Úcan_set_locale/   s    r   c             C   s*   |rdd„ }ndd„ }t ttt|| ƒƒƒS )ar  
    Return a list of normalized locales that do not throw an ``Exception``
    when set.

    Parameters
    ----------
    locales : str
        A string where each locale is separated by a newline.
    normalize : bool
        Whether to call ``locale.normalize`` on each locale.

    Returns
    -------
    valid_locales : list
        A list of valid locales.
    c             S   s   t j| jƒ ƒS )N)r
   Ú	normalizeÚstrip)r   r   r   r   Ú<lambda>]   s    z _valid_locales.<locals>.<lambda>c             S   s   | j ƒ S )N)r   )r   r   r   r   r   _   s    )ÚlistÚfilterr   Úmap)Zlocalesr   Z
normalizerr   r   r   Ú_valid_localesK   s    
r   c              C   s   t jdgdd�} | S )Nz	locale -aT)Úshell)Ú
subprocessÚcheck_output)Úraw_localesr   r   r   Ú_default_locale_getterd   s    r    Tc             C   sÖ   y
|ƒ }W n t jk
r    dS X yb|jdƒ}g }xN|D ]F}y|jt|tjjd�ƒ W q8 tk
r|   |jt|dd�ƒ Y q8X q8W W n t	k
r˜   Y nX | dkr¬t
||ƒS tj| › d�ƒ}|jdj|ƒƒ}t
||ƒS )aÑ  
    Get all the locales that are available on the system.

    Parameters
    ----------
    prefix : str
        If not ``None`` then return only those locales with the prefix
        provided. For example to get all English language locales (those that
        start with ``"en"``), pass ``prefix="en"``.
    normalize : bool
        Call ``locale.normalize`` on the resulting list of available locales.
        If ``True``, only locales that can be set without throwing an
        ``Exception`` are returned.
    locale_getter : callable
        The function to use to retrieve the current locales. This should return
        a string with each locale separated by a newline character.

    Returns
    -------
    locales : list of strings
        A list of locale strings that can be set with ``locale.setlocale()``.
        For example::

            locale.setlocale(locale.LC_ALL, locale_string)

    On error will return None (no locale available, e.g. Windows)

    Nó   
)Úencodingzwindows-1252z.*Ú
)r   ÚCalledProcessErrorÚsplitÚappendÚstrr   Údisplayr"   ÚUnicodeErrorÚ	TypeErrorr   ÚreÚcompileÚfindallr   )Úprefixr   Zlocale_getterr   Zout_localesr   ÚpatternÚfoundr   r   r   Úget_localesi   s&    


 
r1   )Ú__doc__Ú
contextlibr   r
   r+   r   Zpandas._config.configr   ÚLC_ALLr   r   r   r    r1   r   r   r   r   Ú<module>   s    