o
    x[h$  ã                   @   sà   d Z ddl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mZ ddlmZ dZdZd	Ze e¡ZG d
d„ deƒZedƒZG dd„ dƒZG dd„ deƒZddd„Zejdfdd„Zdd„ ZG dd„ dƒZdd„ ZdS )z�
events for reporting.

The events here are designed to be used with reporting.
They can be published to registered handlers with report_event.
é    N)ÚList)Úperformance)Úavailable_handlersÚinstantiated_handler_registry)ÚReportingHandlerÚfinishÚstartÚ	cloudinitc                   @   s   e Zd Zdd„ ZdS )Ú_namesetc                 C   s   || v r|S t d| ƒ‚)Nz%s not a valid value)ÚAttributeError)ÚselfÚname© r   ú</usr/lib/python3/dist-packages/cloudinit/reporting/events.pyÚ__getattr__    s   z_nameset.__getattr__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r
      s    r
   )ÚSUCCESSÚWARNÚFAILc                   @   s.   e Zd ZdZedfdd„Zdd„ Zdd„ ZdS )	ÚReportingEventz"Encapsulation of event formatting.Nc                 C   s2   || _ || _|| _|| _|d u rt ¡ }|| _d S ©N)Ú
event_typer   ÚdescriptionÚoriginÚtimeÚ	timestamp)r   r   r   r   r   r   r   r   r   Ú__init__,   s   
zReportingEvent.__init__c                 C   s   d  | j| j| j¡S )z"The event represented as a string.z{0}: {1}: {2})Úformatr   r   r   ©r   r   r   r   Ú	as_string<   s   ÿzReportingEvent.as_stringc                 C   s   | j | j| j| j| jdœS )z&The event represented as a dictionary.©r   r   r   r   r   r"   r    r   r   r   Úas_dictB   s   ûzReportingEvent.as_dict)r   r   r   Ú__doc__ÚDEFAULT_EVENT_ORIGINr   r!   r#   r   r   r   r   r   )   s    
úr   c                       s8   e Zd Zejdf‡ fdd„	Zdd„ Z‡ fdd„Z‡  ZS )ÚFinishReportingEventNc                    sD   t t| ƒ t||¡ || _|d u rg }|| _|tvr td| ƒ‚d S )NzInvalid result: %s)Úsuperr&   r   ÚFINISH_EVENT_TYPEÚresultÚ
post_filesÚstatusÚ
ValueError)r   r   r   r)   r*   ©Ú	__class__r   r   r   N   s   
ÿÿzFinishReportingEvent.__init__c                 C   s   d  | j| j| j| j¡S )Nz{0}: {1}: {2}: {3})r   r   r   r)   r   r    r   r   r   r!   [   s   ÿzFinishReportingEvent.as_stringc                    s0   t t| ƒ ¡ }| j|d< | jrt| jƒ|d< |S )z'The event represented as json friendly.r)   Úfiles)r'   r&   r#   r)   r*   Ú_collect_file_info)r   Údatar-   r   r   r#   `   s
   
zFinishReportingEvent.as_dict)	r   r   r   r+   r   r   r!   r#   Ú__classcell__r   r   r-   r   r&   M   s
    ÿr&   c                    sT   ˆ si ‰ ‡ fdd„t j ¡ D ƒ}ttj ¡ ƒ}|D ]}t|ƒ|v r"q| | ¡ qdS )a¯  Report an event to all registered event handlers
    except those whose type is in excluded_handler_types.

    This should generally be called via one of the other functions in
    the reporting module.

    :param excluded_handler_types:
         List of handlers types to exclude from reporting the event to.
    :param event_type:
        The type of the event; this should be a constant from the
        reporting module.
    c                    s   h | ]
\}}|ˆ v r|’qS r   r   )Ú.0Ú	hndl_typeÚhndl_cls©Úexcluded_handler_typesr   r   Ú	<setcomp>y   s
    þzreport_event.<locals>.<setcomp>N)r   Úregistered_itemsÚitemsÚlistr   ÚvaluesÚtypeÚpublish_event)Úeventr7   Úexcluded_handler_classesÚhandlersÚhandlerr   r6   r   Úreport_eventi   s   
þÿýrC   c                 C   s   t | |||d�}t|ƒS )zVReport a "finish" event.

    See :py:func:`.report_event` for parameter details.
    ©r*   )r&   rC   )Ú
event_nameÚevent_descriptionr)   r*   r?   r   r   r   Úreport_finish_eventˆ   s   ÿrG   c                 C   s   t t| |ƒ}t|ƒS )a'  Report a "start" event.

    :param event_name:
        The name of the event; this should be a topic which events would
        share (e.g. it will be the same for start and finish events).

    :param event_description:
        A human-readable description of the event that has occurred.
    )r   ÚSTART_EVENT_TYPErC   )rE   rF   r?   r   r   r   Úreport_start_event•   s   
rI   c                   @   s‚   e Zd ZdZdddejdfdd„Zdd„ Zdd„ Zd	d
„ Z	e
dd„ ƒZejdd„ ƒZe
dd„ ƒZejdd„ ƒZdd„ Zdd„ ZdS )ÚReportEventStackaK  Context Manager for using :py:func:`report_event`

    This enables calling :py:func:`report_start_event` and
    :py:func:`report_finish_event` through a context manager.

    :param name:
        the name of the event

    :param description:
        the event's description, passed on to :py:func:`report_start_event`

    :param message:
        the description to use for the finish event. defaults to
        :param:description.

    :param parent:
    :type parent: :py:class:ReportEventStack or None
        The parent of this event.  The parent is populated with
        results of all its children.  The name used in reporting
        is <parent.name>/<name>

    :param reporting_enabled:
        Indicates if reporting events should be generated.
        If not provided, defaults to the parent's value, or True if no parent
        is provided.

    :param result_on_exception:
        The result value to set if an exception is caught. default
        value is FAIL.

    :param post_files:
        Can hold filepaths of files that are to get posted/created
        regarding a given event. Something like success or failure information
        in a given log file. For each filepath, if it's a valid regular file
        it will get: read & encoded as base64 at the close of the event.
        Default value, if None, is an empty list.
    Nc                 C   s€   || _ || _|| _|| _|| _tj| _|d u rg }|| _|d u r(|r&|j	}nd}|| _	|r7d 
|j|f¡| _n| j| _i | _d S )NTú/)Úparentr   r   ÚmessageÚresult_on_exceptionr+   r   r)   r*   Úreporting_enabledÚjoinÚfullnameÚchildren)r   r   r   rM   rL   rO   rN   r*   r   r   r   r   Ê   s,   
þÿ
zReportEventStack.__init__c                 C   s   d| j | j| jf S )Nz.ReportEventStack(%s, %s, reporting_enabled=%s))r   r   rO   r    r   r   r   Ú__repr__ñ   s
   ýzReportEventStack.__repr__c                 C   s4   t j| _| jrt| j| jƒ | jrd| jj| j	< | S )N)NN)
r+   r   r)   rO   rI   rQ   r   rL   rR   r   r    r   r   r   Ú	__enter__ø   s   zReportEventStack.__enter__c                 C   sP   t jt jfD ]}| j ¡ D ]\}\}}||kr || jf    S qq| j| jfS r   )r+   r   r   rR   r:   rM   r)   )r   Úcand_resultÚ_nameÚvalueÚ_msgr   r   r   Ú_childrens_finish_info   s   ÿÿz'ReportEventStack._childrens_finish_infoc                 C   s   | j S r   )Ú_resultr    r   r   r   r)     s   zReportEventStack.resultc                 C   s   |t vr
td| ƒ‚|| _d S )Nz'%s' not a valid result)r+   r,   rZ   ©r   rW   r   r   r   r)     s   
c                 C   s   | j d ur| j S | jS r   )Ú_messager   r    r   r   r   rM     s   
zReportEventStack.messagec                 C   s
   || _ d S r   )r\   r[   r   r   r   rM     s   
c                 C   s   |r| j | jfS |  ¡ S r   )rN   rM   rY   )r   Úexcr   r   r   Ú_finish_info  s   zReportEventStack._finish_infoc                 C   sH   |   |¡\}}| jr||f| jj| j< | jr"t| j||| jd� d S d S )NrD   )r^   rL   rR   r   rO   rG   rQ   r*   )r   Úexc_typeÚ	exc_valueÚ	tracebackr)   Úmsgr   r   r   Ú__exit__!  s   
ÿÿzReportEventStack.__exit__)r   r   r   r$   r+   r   r   rS   rT   rY   Úpropertyr)   ÚsetterrM   r^   rc   r   r   r   r   rJ   £   s*    *
ø'



rJ   c              
   C   sª   | sd S g }| D ]J}t j |¡sd }n6t d|› �¡�& t|dƒ�}t | ¡ ¡ 	¡ }W d   ƒ n1 s5w   Y  W d   ƒ n1 sDw   Y  | 
||ddœ¡ q|S )NzReading ÚrbÚbase64)ÚpathÚcontentÚencoding)Úosrh   Úisfiler   ÚTimedÚopenrg   Ú	b64encodeÚreadÚdecodeÚappend)r/   ÚretÚfnameri   Úfpr   r   r   r0   +  s    ÿý€ r0   r   ) r$   rg   ÚloggingÚos.pathrk   r   Útypingr   r	   r   Úcloudinit.reportingr   r   Úcloudinit.reporting.handlersr   r(   rH   r%   Ú	getLoggerr   ÚLOGÚsetr
   r+   r   r&   rC   r   rG   rI   rJ   r0   r   r   r   r   Ú<module>   s0   
$
 
ÿ 	