U
    Ph9                     @  s   d dl mZ d dl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mZ d dlmZ dd	lmZ d
dddgZedeeZG dd deZefddZdddeefddddddddd
ZdddedefdddddddddddZdddeefdddddddddd	ddZdS )    )annotationsN)Callable)wraps)FunctionType)AnyTypeVar)version_leq   )__version__
deprecateddeprecated_argDeprecatedErrordeprecated_arg_defaultTc                   @  s   e Zd ZdS )r   N)__name__
__module____qualname__ r   r   P/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/utils/deprecate_utils.pyr      s   c                 C  s   t j|  d| |dd dS )z*
    Issue the warning message `msg`.
    z: r	   )category
stacklevelN)warningswarn)objmsgwarning_categoryr   r   r   warn_deprecated"   s    r    z
str | Nonestrztype[FutureWarning]zCallable[[T], T])sinceremoved
msg_suffixversion_valr   returnc                   s   dk	r0dk	r0t s0td d ddk	oH|koHt |}|rVdd S dkrpdkrpdd n$dk	ot | dk	ot | fdd	}|S )
a*  
    Marks a function or class as deprecated. If `since` is given this should be a version at or earlier than the
    current version and states at what version of the definition was marked as deprecated. If `removed` is given
    this can be any version and marks when the definition was removed.

    When the decorated definition is called, that is when the function is called or the class instantiated,
    a `warning_category` is issued if `since` is given and the current version is at or later than that given.
    a `DeprecatedError` exception is instead raised if `removed` is given and the current version is at or later
    than that, or if neither `since` nor `removed` is provided.

    The relevant docstring of the deprecating function should also be updated accordingly,
    using the Sphinx directives such as `.. versionchanged:: version` and `.. deprecated:: version`.
    https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-versionadded

    Args:
        since: version at which the definition was marked deprecated but not removed.
        removed: version at which the definition was/will be removed and no longer usable.
        msg_suffix: message appended to warning/exception detailing reasons for deprecation and what to use instead.
        version_val: (used for testing) version to compare since and removed against, default is MONAI version.
        warning_category: a warning category class, defaults to `FutureWarning`.

    Returns:
        Decorated definition which warns or raises exception when used
    N2since must be less or equal to removed, got since=
, removed=.c                 S  s   | S Nr   r   r   r   r   <lambda>N       zdeprecated.<locals>.<lambda>Tc                   s   t t}|rnj |r dnd dj d}rDd d}n.rnd d}d k	rr|d d7 }nd	}| d
| d
  t  fdd}|r|S |_S )NFunctionZClassz ``was removed in version r&   "has been deprecated since version  It will be removed in version has been deprecated. c                    s&   rt rt  | |S r'   )r   r   argskwargs)call_objis_deprecated
is_removedr   r   r   r   r   _wrapperj   s
    z0deprecated.<locals>._decorator.<locals>._wrapper)
isinstancer   __init__r   stripr   )r   is_func
msg_prefix	msg_infixr8   r6   r7   r!   r    r   r   )r5   r   r   r   
_decoratorY   s"    
zdeprecated.<locals>._decorator)r   
ValueError)r   r    r!   r"   r   is_not_yet_deprecatedr@   r   r?   r   r   )   s     )namer   r    r!   r"   new_namer   r#   c           	        s   | ds|  d  s$tj }dk	rTdk	rTtsTtd d ddk	ol|kolt|}|rzdd S dkrdkrd	d	 n0dk	ot| dk	o|tj kot| fd
d}|S )a  
    Marks a particular named argument of a callable as deprecated. The same conditions for `since` and `removed` as
    described in the `deprecated` decorator.

    When the decorated definition is called, that is when the function is called or the class instantiated with args,
    a `warning_category` is issued if `since` is given and the current version is at or later than that given.
    a `DeprecatedError` exception is instead raised if `removed` is given and the current version is at or later
    than that, or if neither `since` nor `removed` is provided.

    The relevant docstring of the deprecating function should also be updated accordingly,
    using the Sphinx directives such as `.. versionchanged:: version` and `.. deprecated:: version`.
    https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-versionadded


    Args:
        name: name of position or keyword argument to mark as deprecated.
        since: version at which the argument was marked deprecated but not removed.
        removed: version at which the argument was/will be removed and no longer usable.
        msg_suffix: message appended to warning/exception detailing reasons for deprecation and what to use instead.
        version_val: (used for testing) version to compare since and removed against, default is MONAI version.
        new_name: name of position or keyword argument to replace the deprecated argument.
            if it is specified and the signature of the decorated function has a `kwargs`, the value to the
            deprecated argument `name` will be removed.
        warning_category: a warning category class, defaults to `FutureWarning`.

    Returns:
        Decorated callable which warns or raises exception when deprecated argument used.
    0+r   Nr$   r%   r&   c                 S  s   | S r'   r   r(   r   r   r   r)      r*   z deprecated_arg.<locals>.<lambda>Tc              
     s   j  dj d  d d}r6d	 d}n.r`d
 d}	d k	rd|d	 d7 }nd	}| d| d  tt f	d
d}|S )Nr1   :
Argument `r,   r-   r&   r.   r/   r0   c                    s   d k	rX|krX|krX| |< zj | |j W n  tk
rV   |d  Y nX j | |j}|k}d}j D ]:\}}|jtjj	kr|||kr||| kr|d}|d  q||s|r܈rt
rt  | |S )NFT)bind	arguments	TypeErrorpop
parametersitemskindinspect	ParameterVAR_KEYWORDr   r   )r3   r4   bindingZpositional_foundZkw_foundkparam)	argnamefuncr6   r7   r   rC   rD   sigr   r   r   r8      s&    "z4deprecated_arg.<locals>._decorator.<locals>._wrapper)r   r   r;   rO   	signaturer   )rV   r=   r>   r8   r6   r7   r!   rC   rD   r    r   r   rU   rV   r   rW   r   r@      s    
z"deprecated_arg.<locals>._decorator
startswithr;   isdigitsysmaxsizer   rA   )	rC   r   r    r!   r"   rD   r   rB   r@   r   rY   r   r   {   s    &0r   )	rC   old_defaultnew_defaultr   replacedr!   r"   r   r#   c           
   	     s   | ds|  d  s$tj }dk	rTdk	rTtsTtd d ddk	ol|kolt|}|rzdd S dkrdkrd	d	 n0dk	ot| dk	o|tj kot| f	d
d}	|	S )a  
    Marks a particular arguments default of a callable as deprecated. It is changed from `old_default` to `new_default`
    in version `changed`.

    When the decorated definition is called, a `warning_category` is issued if `since` is given,
    the default is not explicitly set by the caller and the current version is at or later than that given.
    Another warning with the same category is issued if `changed` is given and the current version is at or later.

    The relevant docstring of the deprecating function should also be updated accordingly,
    using the Sphinx directives such as `.. versionchanged:: version` and `.. deprecated:: version`.
    https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-versionadded


    Args:
        name: name of position or keyword argument where the default is deprecated/changed.
        old_default: name of the old default. This is only for the warning message, it will not be validated.
        new_default: name of the new default.
            It is validated that this value is not present as the default before version `replaced`.
            This means, that you can also use this if the actual default value is `None` and set later in the function.
            You can also set this to any string representation, e.g. `"calculate_default_value()"`
            if the default is calculated from another function.
        since: version at which the argument default was marked deprecated but not replaced.
        replaced: version at which the argument default was/will be replaced.
        msg_suffix: message appended to warning/exception detailing reasons for deprecation.
        version_val: (used for testing) version to compare since and removed against, default is MONAI version.
        warning_category: a warning category class, defaults to `FutureWarning`.

    Returns:
        Decorated callable which warns when deprecated default argument is not explicitly specified.
    rE   r   Nz3since must be less or equal to replaced, got since=z, replaced=r&   c                 S  s   | S r'   r   r(   r   r   r   r)     r*   z(deprecated_arg_default.<locals>.<lambda>Tc                   sh  j  dj d  d d	 d}rTd
 d d	 d d d	}nTrd
 d}
d k	r|d d d
 d7 }nd d	 d d d		}| d| d  tjkrtd dj dj }|jtjj	krtd d|jkrDsDtd d d
 dt
 fdd}|S )Nr1   rF   z$ Current default value of argument `=r,   zwas changed in version z from `z` to `z`.r.   r&   z It will be changed to `z` in version zhas been deprecated from `rG   z` not found in signature of z` has no default value.z)` was replaced to the new default value `z` before the specified version c                    s,   j | |jkr"r"t  | |S r'   )rH   rI   r   r2   )rU   rV   r6   r   rC   rW   r   r   r   r8   =  s    z<deprecated_arg_default.<locals>._decorator.<locals>._wrapper)r   r   r;   rO   rX   rL   rA   defaultrP   emptyr   )rV   r=   r>   rT   r8   	r6   Zis_replacedr!   rC   ra   r`   rb   r   r   rZ   r   r@   !  s.    &


z*deprecated_arg_default.<locals>._decoratorr[   )
rC   r`   ra   r   rb   r!   r"   r   rB   r@   r   rf   r   r      s    )&)
__future__r   rO   r^   r   collections.abcr   	functoolsr   typesr   typingr   r   Zmonai.utils.moduler   r   r
   __all__typer   	Exceptionr   FutureWarningr   r   r   r   r   r   r   r   <module>   s@   To