
"T`                  @   s   d  Z  d d l Z d d l Z d d l Z d d l m Z m Z d Z d Z	 d Z
 Gd d	   d	 e  Z e d  Z Gd d   d e  Z Gd d   d e  Z d d d  Z e j d d d  Z d d   Z Gd d   d e  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   )instantiated_handler_registryavailable_handlersfinishstart	cloudinitc               @   s   e  Z d  Z d d   Z d S)_namesetc             C   s$   | |  k r | St  d |   d  S)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 d d  Z d d   Z d d   Z d S)	ReportingEventz"Encapsulation of event formatting.Nc             C   sI   | |  _  | |  _ | |  _ | |  _ | d  k r< t j   } | |  _ 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 |  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   s1   d |  j  d |  j d |  j d |  j d |  j i S)z&The event represented as a dictionary.r   r   r   r   r   )r   r   r   r   r   )r
   r   r   r   as_dict3   s    zReportingEvent.as_dict)r   r   r   __doc__DEFAULT_EVENT_ORIGINr   r   r   r   r   r   r   r   !   s   	r   c                   sI   e  Z d  Z e j d   f d d  Z d d   Z   f d d   Z   S)FinishReportingEventNc                s`   t  t |   j t | |  | |  _ | d  k r7 g  } | |  _ | t k 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   <   s    		zFinishReportingEvent.__init__c             C   s"   d j  |  j |  j |  j |  j  S)Nz{0}: {1}: {2}: {3})r   r   r   r$   r   )r
   r   r   r   r   G   s    zFinishReportingEvent.as_stringc                sB   t  t |   j   } |  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
   data)r(   r   r   r   K   s
    	zFinishReportingEvent.as_dict)r   r   r   r&   r   r   r   r   r   r   )r(   r   r!   :   s   
r!   c                sz     s i      f d d   t  j j   D } t j j   } x6 | D]. \ } } t |  | k re qD | j |   qD Wd 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  |  ] \ } } |   k r |  q Sr   r   ).0Z	hndl_typeZhndl_cls)excluded_handler_typesr   r   	<setcomp>d   s   		zreport_event.<locals>.<setcomp>N)r   registered_itemsitemsr   typepublish_event)eventr-   Zexcluded_handler_classeshandlers_handlerr   )r-   r   report_eventT   s    r7   c             C   s"   t  |  | | d | } t |  S)zVReport a "finish" event.

    See :py:func:`.report_event` for parameter details.
    r%   )r!   r7   )
event_nameevent_descriptionr$   r%   r3   r   r   r   report_finish_eventq   s    	r:   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_TYPEr7   )r8   r9   r3   r   r   r   report_start_event|   s    
r<   c               @   s   e  Z d  Z d Z d d d e j d 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)ReportEventStacka  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.
    Nc             C   s   | |  _  | |  _ | |  _ | |  _ | |  _ t j |  _ | d  k rK g  } | |  _ | d  k rx | rr | j	 } n d } | |  _	 | r d j
 | 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   r@   r?   rB   rA   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   rB   )r
   r   r   r   __repr__   s    zReportEventStack.__repr__c             C   sH   t  j |  _ |  j r( t |  j |  j  |  j rD d |  j j |  j	 <|  S)N)NN)
r&   r   r$   rB   r<   rD   r   r?   rE   r   )r
   r   r   r   	__enter__   s    		zReportEventStack.__enter__c             C   sl   xY t  j t  j f D]E } x< |  j j   D]+ \ } \ } } | | k r) | |  j f Sq) Wq W|  j |  j f S)N)r&   r   r   rE   r0   r@   r$   )r
   Zcand_result_namevalueZ_msgr   r   r   _childrens_finish_info   s
    "z'ReportEventStack._childrens_finish_infoc             C   s   |  j  S)N)_result)r
   r   r   r   r$      s    zReportEventStack.resultc             C   s)   | t  k r t d |   | |  _ d  S)Nz'%s' not a valid result)r&   r'   rK   )r
   rI   r   r   r   r$      s    c             C   s   |  j  d  k	 r |  j  S|  j S)N)_messager   )r
   r   r   r   r@      s    zReportEventStack.messagec             C   s   | |  _  d  S)N)rL   )r
   rI   r   r   r   r@      s    c             C   s    | r |  j  |  j f S|  j   S)N)rA   r@   rJ   )r
   excr   r   r   _finish_info   s    zReportEventStack._finish_infoc             C   s`   |  j  |  \ } } |  j r7 | | f |  j j |  j <|  j r\ t |  j | | d |  j d  S)Nr%   )rN   r?   rE   r   rB   r:   rD   r%   )r
   exc_type	exc_value	tracebackr$   msgr   r   r   __exit__   s    		zReportEventStack.__exit__)r   r   r   r   r&   r   r   rF   rG   rJ   propertyr$   setterr@   rN   rS   r   r   r   r   r=      s   	r=   c             C   s   |  s
 d  Sg  } x| |  D]t } t  j j |  s8 d  } n4 t | d  " } t j | j    j   } Wd  QRX| j d | d | d d i  q W| S)Nrbpathcontentencodingbase64)	osrW   isfileopenrZ   	b64encodereaddecodeappend)r)   retfnamerX   fpr   r   r   r*      s    	"r*   )zSUCCESSzWARNzFAIL)r   rZ   Zos.pathr[   r    r   r   r#   r;   r    setr   r&   objectr   r!   r7   r   r:   r<   r=   r*   r   r   r   r   <module>
   s    
p