U
    Ӈg+                     @   s   d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	 e
eZG dd deZejG dd de dd	d
ddgZeeedddZejeeeedddZddddeeee eeedddZddeeedddZdS )    N)
NamedTupleOptional)features)loggersc                   @   s   e Zd ZU eed< eed< dS )DeprecationLogZ	log_levelmessageN)__name__
__module____qualname__int__annotations__str r   r   5/usr/lib/python3/dist-packages/cloudinit/lifecycle.pyr      s   
r   c                       s|   e Zd ZdZdeeeed d fddZeed dddZd	d
 Z	dd Z
dd Zdd Zdd Zd edddZ  ZS )Versiona>  A class for comparing versions.

    Implemented as a named tuple with all ordering methods. Comparisons
    between X.Y.N and X.Y always treats the more specific number as larger.

    :param major: the most significant number in a version
    :param minor: next greatest significant number after major
    :param patch: next greatest significant number after minor
    :param rev: the least significant number in a version

    :raises TypeError: If invalid arguments are given.
    :raises ValueError: If invalid arguments are given.

    Examples:
        >>> Version(2, 9) == Version.from_str("2.9")
        True
        >>> Version(2, 9, 1) > Version.from_str("2.9.1")
        False
        >>> Version(3, 10) > Version.from_str("3.9.9.9")
        True
        >>> Version(3, 7) >= Version.from_str("3.7")
        True

    )majorminorpatchrevreturnc                    s   t t| | ||||S )zPDefault of -1 allows us to tiebreak in favor of the most specific
        number)superr   __new__)clsr   r   r   r   	__class__r   r   r   /   s    zVersion.__new__)versionr   c                 C   s   | t tt|d S )a%  Create a Version object from a string.

        :param version: A period-delimited version string, max 4 segments.

        :raises TypeError: Raised if invalid arguments are given.
        :raises ValueError: Raised if invalid arguments are given.

        :return: A Version object.
        .)listmapr   split)r   r   r   r   r   from_str6   s    zVersion.from_strc                 C   s   d|  |kS )N   )_compare_versionselfotherr   r   r   __gt__C   s    zVersion.__gt__c                 C   s0   | j |j ko.| j|jko.| j|jko.| j|jkS Nr   r   r   r   r$   r   r   r   __eq__F   s    


zVersion.__eq__c                 c   s6   | j | j| j| jfD ]}|dkr,t|V  q q2qdS )z)Iterate over the version (drop sentinels)r   N)r   r   r   r   r   )r%   nr   r   r   __iter__N   s    zVersion.__iter__c                 C   s
   d | S )Nr   )joinr%   r   r   r   __str__V   s    zVersion.__str__c                 C   s   t t| S r(   )hashr   r.   r   r   r   __hash__Y   s    zVersion.__hash__)r&   r   c                 C   sP   | |krdS | j |j krdS | j|jkr,dS | j|jkr<dS | j|jkrLdS dS )zCompare this Version to another.

        :param other: A Version object.

        :return: -1 if self > other, 1 if self < other, else 0
        r   r"   r   r)   r$   r   r   r   r#   \   s    zVersion._compare_version)r   r   r   r   )r   r	   r
   __doc__r   r   classmethodr   r!   r'   r*   r,   r/   r1   r#   __classcell__r   r   r   r   r      s(             r   r   r   r   r   )r   boundary_versionr   c                 C   s   |dkpt | t |kS )a  Determine if a deprecation message should be logged.

    :param version: The version in which the thing was deprecated.
    :param boundary_version: The version at which deprecation level is logged.

    :return: True if the message should be logged, else False.
    Zdevel)r   r!   )r   r5   r   r   r   should_log_deprecationp   s
    r6   Zloggerr   Zrequested_levelmsgargsc                 C   s4   t |tjr | j||f|  n| j|f|  dS )au  Log a message at the requested level, if that is acceptable.

    If the log level is too high due to the version boundary, log at DEBUG
    level. Useful to add new warnings to previously unguarded code without
    disrupting stable downstreams.

    :param logger: Logger object to log with
    :param version: Version string of the version that this log was introduced
    :param level: Preferred level at which this message should be logged
    :param msg: Message, as passed to the logger.
    :param args: Message formatting args, as passed to the logger

    :return: True if the message should be logged, else False.
    N)r6   r   DEPRECATION_INFO_BOUNDARYlogdebugr7   r   r   r   log_with_downgradable_level}   s    r=      F)extra_messagescheduleskip_log)
deprecateddeprecated_versionr?   r@   rA   r   c                 C   s   t tdsttdt  |pd}t| | | t| }t|}t|j| |j	}|  d| d| d| 
 }	t|tjstj}
nt tdrtj}
ntj}
ttd}|s||kr|| t|
|	 t|
|	S )a  Mark a "thing" as deprecated. Deduplicated deprecations are
    logged.

    :param deprecated: Noun to be deprecated. Write this as the start
        of a sentence, with no period. Version and extra message will
        be appended.
    :param deprecated_version: The version in which the thing was
        deprecated
    :param extra_message: A remedy for the user's problem. A good
        message will be actionable and specific (i.e., don't use a
        generic "Use updated key." if the user used a deprecated key).
        End the string with a period.
    :param schedule: Manually set the deprecation schedule. Defaults to
        5 years. Leave a comment explaining your reason for deviation if
        setting this value.
    :param skip_log: Return log text rather than logging it. Useful for
        running prior to logging setup.
    :return: NamedTuple containing log level and log message
        DeprecationLog(level: int, message: str)

    Note: uses keyword-only arguments to improve legibility
    r;    z is deprecated in z  and scheduled to be removed in z. rB   )hasattr	deprecatesetattrsetr0   r   r   r!   r   r   rstripr6   r   r:   loggingINFOLOGr   Z
DEPRECATEDZWARNgetattraddr;   r   )rB   rC   r?   r@   rA   r   Zdedupr   Zversion_removedZdeprecate_msglevelZ	log_cacher   r   r   rF      s*    

 


rF   )r@   rC   r?   r@   c                    s    fdd}|S )a~  Mark a "thing" as deprecated. Deduplicated deprecations are
    logged.

    :param deprecated_version: The version in which the thing was
        deprecated
    :param extra_message: A remedy for the user's problem. A good
        message will be actionable and specific (i.e., don't use a
        generic "Use updated key." if the user used a deprecated key).
        End the string with a period.
    :param schedule: Manually set the deprecation schedule. Defaults to
        5 years. Leave a comment explaining your reason for deviation if
        setting this value.

    Note: uses keyword-only arguments to improve legibility
    c                    s    t   fdd}|S )Nc                     s    | |}t  jd |S )N)rC   rB   r?   r@   )rF   r   )r9   kwargsout)rC   r?   funcr@   r   r   	decorator   s    
z2deprecate_call.<locals>.wrapper.<locals>.decorator)	functoolswraps)rS   rT   rP   )rS   r   wrapper   s    zdeprecate_call.<locals>.wrapperr   )rC   r?   r@   rW   r   rP   r   deprecate_call   s    rX   )collectionsrU   rJ   typingr   r   Z	cloudinitr   Zcloudinit.logr   Z	getLoggerr   rL   r   total_ordering
namedtupler   r   boolr6   ZLoggerr   tupler=   rF   rX   r   r   r   r   <module>   sF   

] 9  