U
    ð¤ùdõ  ã                   @   s~   d dl Z d dlmZ d dlmZ ddlmZ e  d¡Ze  d¡Z	dZ
d	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zejfdd„ZdS )é    N)ÚOrderedDict)Údeepcopyé   )Ú
HTTPStatusz(.)([A-Z][a-z]+)z([a-z0-9])([A-Z]))ÚmergeÚcamel_to_dashÚ
default_idÚnot_noneÚnot_none_sortedÚunpackc                 C   sb   t |tƒs|S t| ƒ}| ¡ D ]>\}}||krPt || tƒrPt|| |ƒ||< qt|ƒ||< q|S )aA  
    Recursively merges two dictionaries.

    Second dictionary values will take precedence over those from the first one.
    Nested dictionaries are merged too.

    :param dict first: The first dictionary
    :param dict second: The second dictionary
    :return: the resulting merged dictionary
    :rtype: dict
    )Ú
isinstanceÚdictr   Úitemsr   )ÚfirstÚsecondÚresultÚkeyÚvalue© r   ú5/tmp/pip-unpacked-wheel-dt_sn2ih/flask_restx/utils.pyr      s    
r   c                 C   s   t  d| ¡}t d|¡ ¡ S )z¦
    Transform a CamelCase string into a low_dashed one

    :param str value: a CamelCase string to transform
    :return: the low_dashed string
    :rtype: str
    z\1_\2)ÚFIRST_CAP_REÚsubÚ
ALL_CAP_REÚlower)r   Z	first_capr   r   r   r   .   s    r   c                 C   s   d  |t| ƒ¡S )zDefault operation ID generatorz{0}_{1})Úformatr   )ÚresourceÚmethodr   r   r   r   :   s    r   c                 C   s   t dd„ |  ¡ D ƒƒS )zÕ
    Remove all keys where value is None

    :param dict data: A dictionary with potentially some values set to None
    :return: The same dictionary without the keys with values to ``None``
    :rtype: dict
    c                 s   s"   | ]\}}|d k	r||fV  qd S ©Nr   ©Ú.0ÚkÚvr   r   r   Ú	<genexpr>G   s      znot_none.<locals>.<genexpr>)r   r   ©Údatar   r   r   r	   ?   s    r	   c                 C   s   t dd„ t|  ¡ ƒD ƒƒS )zã
    Remove all keys where value is None

    :param OrderedDict data: A dictionary with potentially some values set to None
    :return: The same dictionary without the keys with values to ``None``
    :rtype: OrderedDict
    c                 s   s"   | ]\}}|d k	r||fV  qd S r   r   r   r   r   r   r"   R   s      z"not_none_sorted.<locals>.<genexpr>)r   Úsortedr   r#   r   r   r   r
   J   s    r
   c                 C   s|   t | tƒs| |i fS t| ƒdkr.| d |i fS t| ƒdkrL| \}}||i fS t| ƒdkrp| \}}}||pj||fS tdƒ‚dS )ao  
    Unpack a Flask standard response.

    Flask response can be:
    - a single value
    - a 2-tuple ``(value, code)``
    - a 3-tuple ``(value, code, headers)``

    .. warning::

        When using this function, you must ensure that the tuple is not the response data.
        To do so, prefer returning list instead of tuple for listings.

    :param response: A Flask style response
    :param int default_code: The HTTP code to use as default if none is provided
    :return: a 3-tuple ``(data, code, headers)``
    :rtype: tuple
    :raise ValueError: if the response does not have one of the expected format
    r   r   é   é   zToo many response valuesN)r   ÚtupleÚlenÚ
ValueError)ÚresponseZdefault_coder$   ÚcodeÚheadersr   r   r   r   U   s    



r   )ÚreÚcollectionsr   Úcopyr   Z_httpr   Úcompiler   r   Ú__all__r   r   r   r	   r
   ÚOKr   r   r   r   r   Ú<module>   s   


