a
    ’¸æaî  ã                   @   sv   d Z ddlmZmZmZ ddlmZ ddlmZm	Z	m
Z
 esLddlZddlZg d¢Zdd	„ Zedfd
d„Zdd„ ZdS )z
Commonly useful converters.
é    )Úabsolute_importÚdivisionÚprint_functioné   )ÚPY2)ÚNOTHINGÚFactoryÚpipeN)Údefault_if_noneÚoptionalr	   Úto_boolc              	      sœ   ‡ fdd„}t s˜d}zt ˆ ¡}W n ttfy8   Y n0 |r˜t|j ¡ ƒ}|rx|d jtj	j
urxtj|d j |jd< |jtjj
ur˜tj|j |jd< |S )aO  
    A converter that allows an attribute to be optional. An optional attribute
    is one which can be set to ``None``.

    Type annotations will be inferred from the wrapped converter's, if it
    has any.

    :param callable converter: the converter that is used for non-``None``
        values.

    .. versionadded:: 17.1.0
    c                    s   | d u rd S ˆ | ƒS ©N© ©Úval©Ú	converterr   ú.lib/python3.9/site-packages/attr/converters.pyÚoptional_converter(   s    z$optional.<locals>.optional_converterNr   r   Úreturn)r   ÚinspectZ	signatureÚ
ValueErrorÚ	TypeErrorÚlistÚ
parametersÚvaluesÚ
annotationZ	ParameterÚemptyÚtypingÚOptionalÚ__annotations__Zreturn_annotationZ	Signature)r   r   ZsigÚparamsr   r   r   r      s$    ÿ
ÿ
r   c                    sv   ˆ t u r|du rtdƒ‚ˆ t ur0|dur0tdƒ‚|dur@t|ƒ‰ tˆ tƒrfˆ jrXtdƒ‚‡ fdd„}n‡ fdd„}|S )aœ  
    A converter that allows to replace ``None`` values by *default* or the
    result of *factory*.

    :param default: Value to be used if ``None`` is passed. Passing an instance
       of `attrs.Factory` is supported, however the ``takes_self`` option
       is *not*.
    :param callable factory: A callable that takes no parameters whose result
       is used if ``None`` is passed.

    :raises TypeError: If **neither** *default* or *factory* is passed.
    :raises TypeError: If **both** *default* and *factory* are passed.
    :raises ValueError: If an instance of `attrs.Factory` is passed with
       ``takes_self=True``.

    .. versionadded:: 18.2.0
    Nz(Must pass either `default` or `factory`.z5Must pass either `default` or `factory` but not both.z1`takes_self` is not supported by default_if_none.c                    s   | d ur| S ˆ   ¡ S r   )Úfactoryr   ©Údefaultr   r   Údefault_if_none_converterd   s    z2default_if_none.<locals>.default_if_none_converterc                    s   | d ur| S ˆ S r   r   r   r#   r   r   r%   l   s    )r   r   r   Ú
isinstanceZ
takes_selfr   )r$   r"   r%   r   r#   r   r
   A   s     ÿ
ÿr
   c                 C   sh   t | tƒr|  ¡ } h d£}h d£}z | |v r2W dS | |v r@W dS W n tyT   Y n0 td | ¡ƒ‚dS )a  
    Convert "boolean" strings (e.g., from env. vars.) to real booleans.

    Values mapping to :code:`True`:

    - :code:`True`
    - :code:`"true"` / :code:`"t"`
    - :code:`"yes"` / :code:`"y"`
    - :code:`"on"`
    - :code:`"1"`
    - :code:`1`

    Values mapping to :code:`False`:

    - :code:`False`
    - :code:`"false"` / :code:`"f"`
    - :code:`"no"` / :code:`"n"`
    - :code:`"off"`
    - :code:`"0"`
    - :code:`0`

    :raises ValueError: for any other value.

    .. versionadded:: 21.3.0
    >   ÚtTÚtrueÚyZonÚ1Zyes>   FÚnÚfZfalseZoffÚ0ÚnoTFz Cannot convert value to bool: {}N)r&   ÚstrÚlowerr   r   Úformat)r   ZtruthyZfalsyr   r   r   r   u   s    

r   )Ú__doc__Z
__future__r   r   r   Z_compatr   Ú_maker   r   r	   r   r   Ú__all__r   r
   r   r   r   r   r   Ú<module>   s   '4