o
    i9                     @  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 g d
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Zdddedefd(dd Zdddeef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   ]/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/utils/deprecate_utils.pyr      s    r   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    since
str | Noneremoved
msg_suffixstrversion_valr   type[FutureWarning]returnCallable[[T], T]c                   s   durdurt std d dduo$|ko$t |}|r+dd S du r8du r8dd nduo@t | duoIt | 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 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}r"d d}nr7d d}d ur6|d d7 }nd	}| d
| d
  t  fdd}|rX|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  | i |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   Zis_func
msg_prefix	msg_infixr<   r:   r;   r!   r    r   r   )r9   r   r   r   
_decoratorY   s$   
zdeprecated.<locals>._decorator)r   
ValueError)r   r    r!   r#   r   is_not_yet_deprecatedrC   r   rB   r   r   )   s    r   namenew_namec           	        s   | ds|  d  stj }dur*dur*ts*td d dduo6|ko6t|}|r=dd S du rJdu rJd	d	 nduoRt| duoa|tj koat| 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  r*   r+   r   r,   r   r   r   r-      r.   z deprecated_arg.<locals>.<lambda>Tc              
     s   j  dj d  d d}rd	 d}nr0d
 d}	d ur/|d	 d7 }nd	}| d| d  tt f	d
d}|S )Nr5   :
Argument `r0   r1   r)   r2   r3   r4   c                    s   d ur-|v r-|vr-| |< zj | i |j W n ty,   |d  Y nw j | i |j}|v }d}j D ]\}}|jtjj	kr^||v r^|| v r^d}|d  qA|sc|rqrit
rqt  | i |S )NFT)bind	arguments	TypeErrorpop
parametersitemskindinspect	ParameterVAR_KEYWORDr   r   )r7   r8   ZbindingZpositional_foundZkw_foundkparam)	argnamefuncr:   r;   r   rF   rG   sigr   r   r   r<      s*   "z4deprecated_arg.<locals>._decorator.<locals>._wrapper)r   r   r?   rR   	signaturer   )rX   r@   rA   r<   r:   r;   r!   rF   rG   r    r   r   rW   rX   r   rY   r   rC      s   
z"deprecated_arg.<locals>._decorator
startswithr?   isdigitsysmaxsizer   rD   )	rF   r   r    r!   r#   rG   r   rE   rC   r   r[   r   r   {   s   &0r   old_defaultr   new_defaultreplacedc           
   	     s   | ds|  d  stj }dur*dur*ts*td d dduo6|ko6t|}|r=dd S du rJdu rJd	d	 nduoRt| duoa|tj koat| 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.
    rH   r   Nz3since must be less or equal to replaced, got since=z, replaced=r)   c                 S  r*   r+   r   r,   r   r   r   r-     r.   z(deprecated_arg_default.<locals>.<lambda>Tc                   sb  j  dj d  d d	 d}r*d
 d d	 d d d	}n*rEd
 d}
d urD|d d d
 d7 }nd d	 d d d		}| d| d  tjvrvtd dj dj }|jtjj	u rtd d|jkrstd d d
 dt
 fdd}|S )Nr5   rI   z$ Current default value of argument `=r0   zwas changed in version z from `z` to `z`.r2   r)   z It will be changed to `z` in version zhas been deprecated from `rJ   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                    s4   j | i |jvrrt  | i |S r+   )rK   rL   r   r6   )rW   rX   r:   r   rF   rY   r   r   r   r<   =  s   z<deprecated_arg_default.<locals>._decorator.<locals>._wrapper)r   r   r?   rR   rZ   rO   rD   defaultrS   emptyr   )rX   r@   rA   rV   r<   	r:   is_replacedr!   rF   rc   rb   rd   r   r   r\   r   rC   !  s0   &


z*deprecated_arg_default.<locals>._decoratorr]   )
rF   rb   rc   r   rd   r!   r#   r   rE   rC   r   rh   r   r      s   )&r   )r   r   r    r   r!   r"   r#   r"   r   r$   r%   r&   )rF   r"   r   r   r    r   r!   r"   r#   r"   rG   r   r   r$   r%   r&   )rF   r"   rb   r   rc   r   r   r   rd   r   r!   r"   r#   r"   r   r$   r%   r&   )
__future__r   rR   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>   sB   To