B
    \                 @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlmZ dd	lm	Z	 d!ddZ
d"ddZd#ddZd$ddZdd Zd%ddZdd Zdd Zd&ddZd'dd ZdS )(zSHelpers related to deprecation of functions, methods, classes, other
functionality.    N   )compat)	decorator)inject_docstring_text)inject_param_text   )exc   c             C   s   t j| tj|d d S )N)
stacklevel)warningswarnr   SADeprecationWarning)msgr
    r   ;lib/python3.7/site-packages/sqlalchemy/util/deprecations.pywarn_deprecated   s    r   c             C   s   t j| tj|d d S )N)r
   )r   r   r   SAPendingDeprecationWarning)r   r
   r   r   r   warn_pending_deprecation   s    r   __init__c                s$   d| p
df  fdd}|S )Nz.. deprecated:: %s %s c                s   t |  tjt d S )N)func)_decorate_cls_with_warningr   r   dict)cls)constructorheadermessager   r   decorate    s    z deprecated_cls.<locals>.decorater   )versionr   r   r   r   )r   r   r   r   deprecated_cls   s    	r   Tc                s8   |rd| pdf  nd dkr&d fdd}|S )a  Decorates a function and issues a deprecation warning on use.

    :param version:
      Issue version in the warning.

    :param message:
      If provided, issue message in the warning.  A sensible default
      is used if not provided.

    :param add_deprecation_to_docstring:
      Default True.  If False, the wrapped function's __doc__ is left
      as-is.  If True, the 'message' is prepended to the docs if
      provided, or sensible default if message is omitted.

    z.. deprecated:: %s %sr   Nz$Call to deprecated function %(func)sc                s   t | tjt| jd  S )N)r   )_decorate_with_warningr   r   r   __name__)fn)r   r   r   r   r   E   s
    zdeprecated.<locals>.decorater   )r   r   add_deprecation_to_docstringr   r   )r   r   r   
deprecated,   s    r$   c                 s<   i  x$  D ]\}\}}t| |< qW  fdd}|S )a"  Decorates a function to warn on use of certain parameters.

    e.g. ::

        @deprecated_params(
            weak_identity_map=(
                "0.7",
                "the :paramref:`.Session.weak_identity_map parameter "
                "is deprecated."
            )

        )

    c                s   t | }|jd k	r\tt|jt|jt|j d  |jt t	nd t|j
d k	}t fdd}| jd k	r| jpd}|rt|dd  D }|| }||_|S )Nr   c                sj   x2 D ]*}|| | krt j| tjdd qW x*D ]"}||kr:t j| tjdd q:W | ||S )Nr	   )r
   )r   r   r   r   )r"   argskwargsm)check_defaultscheck_kwdefaultsmessagesr   r   warnedu   s    

z3deprecated_params.<locals>.decorate.<locals>.warnedr   c             S   s&   i | ]\}\}}d ||pdf |qS )z.. deprecated:: %s %sr   r   ).0paramr   r   r   r   r   
<dictcomp>   s   z7deprecated_params.<locals>.decorate.<locals>.<dictcomp>)r   Zinspect_getfullargspecr*   r   zipr%   lensetintersection
differenceZvarkwr   __doc__r   items)r"   specZhas_kwr,   doc	decorated)r+   specs)r(   r)   r*   r   r   d   s*    



z#deprecated_params.<locals>.decorate)r6   _sanitize_restructured_text)r:   r.   r   r   r   r   )r+   r:   r   deprecated_paramsP   s
    -r<   c                s8   |rd| pdf  nd dkr&d fdd}|S )aE  Decorates a function and issues a pending deprecation warning on use.

    :param version:
      An approximate future version at which point the pending deprecation
      will become deprecated.  Not used in messaging.

    :param message:
      If provided, issue message in the warning.  A sensible default
      is used if not provided.

    :param add_deprecation_to_docstring:
      Default True.  If False, the wrapped function's __doc__ is left
      as-is.  If True, the 'message' is prepended to the docs if
      provided, or sensible default if message is omitted.
    z.. deprecated:: %s (pending) %sr   Nz$Call to deprecated function %(func)sc                s   t | tjt| jd  S )N)r   )r    r   r   r   r!   )r"   )r   r   r   r   r      s
    z%pending_deprecation.<locals>.decorater   )r   r   r#   r   r   )r   r   r   pending_deprecation   s    r=   c             C   s   | d kr|S t | | S d S )N)r   )Zparameter_valueZdefault_valueZwarning_textr   r   r   deprecated_option_value   s    r>   c             C   s   dd }t d|| S )Nc             S   s$   |  dd\}}|dkr |d7 }|S )Nr   r   )r   methz())group)r'   Ztype_namer   r   r   repl   s    z)_sanitize_restructured_text.<locals>.replz\:(\w+)\:`~?\.?(.+?)`)resub)textrB   r   r   r   r;      s    r;   c             C   s   | j d k	r| j pd}|d k	r~|t|d; }t||d}t| tkrnt| j}||d< t| j| j|} || }n|| _ t| |}t| |t	|||d  | S )Nr   )r   r   r5   )
r5   r   r   type__dict__r!   	__bases__getattrsetattrr    )r   r   wtyper   docstring_headerr8   ZclsdictZconstructor_fnr   r   r   r      s     


r   c                st   t   t fdd}| jdk	r*| jp,d}|dk	rR|t| jd; }t||d}|| }||_ fdd|_|S )	z=Wrap a function with a warnings.warn and augmented docstring.c                s   t j dd | ||S )Nr	   )r
   )r   r   )r"   r%   r&   )r   rK   r   r   r,      s    z&_decorate_with_warning.<locals>.warnedNr   )r   r   c                  s   t j ddS )Nr	   )r
   )r   r   r   )r   rK   r   r   <lambda>   s    z(_decorate_with_warning.<locals>.<lambda>)r;   r   r5   r   r!   r   Z_sa_warn)r   rK   r   rL   r,   r8   r9   r   )r   rK   r   r       s    r    )r	   )r	   )r   )NT)NT)N)N)r5   rC   r   r   r   Zlanghelpersr   r   r   r   r   r   r   r$   r<   r=   r>   r;   r   r    r   r   r   r   <module>	   s"   



$E
%
