
    x[h$                     $   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j$                  e      Z G d
 de      Z ed      Z G d d      Z G d de      ZddZej6                  dfdZd Z G d d      Zd Zy)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                       e Zd Zd Zy)_namesetc                 *    || v r|S t        d|z        )Nz%s not a valid value)AttributeError)selfnames     </usr/lib/python3/dist-packages/cloudinit/reporting/events.py__getattr__z_nameset.__getattr__    s    4<K3d:;;    N)__name__
__module____qualname__r    r   r   r   r      s    <r   r   )SUCCESSWARNFAILc                   (    e Zd ZdZedfdZd Zd Zy)ReportingEventz"Encapsulation of event formatting.Nc                 v    || _         || _        || _        || _        |t	        j                         }|| _        y N)
event_typer   descriptionorigintime	timestamp)r   r   r   r    r!   r#   s         r   __init__zReportingEvent.__init__,   s9     %	&		I"r   c                 d    dj                  | j                  | j                  | j                        S )z"The event represented as a string.z{0}: {1}: {2})formatr   r   r    r   s    r   	as_stringzReportingEvent.as_string<   s*    %%OOTYY(8(8
 	
r   c                 v    | j                   | j                  | j                  | j                  | j                  dS )z&The event represented as a dictionary.r   r    r   r!   r#   r*   r'   s    r   as_dictzReportingEvent.as_dictB   s3     II++//kk
 	
r   )r   r   r   __doc__DEFAULT_EVENT_ORIGINr$   r(   r+   r   r   r   r   r   )   s    , $# 

r   r   c                   H     e Zd Zej                  df fd	Zd Z fdZ xZS )FinishReportingEventNc                     t         t        |   t        ||       || _        |g }|| _        |t        vrt        d|z        y )NzInvalid result: %s)superr/   r$   FINISH_EVENT_TYPEresult
post_filesstatus
ValueError)r   r   r    r3   r4   	__class__s        r   r$   zFinishReportingEvent.__init__N   sU     	"D2t[	
 J$1F:;;  r   c                 z    dj                  | j                  | j                  | j                  | j                        S )Nz{0}: {1}: {2}: {3})r&   r   r   r3   r    r'   s    r   r(   zFinishReportingEvent.as_string[   s0    #**OOTYYT5E5E
 	
r   c                     t         t        |          }| j                  |d<   | j                  rt        | j                        |d<   |S )z'The event represented as json friendly.r3   files)r1   r/   r+   r3   r4   _collect_file_info)r   datar7   s     r   r+   zFinishReportingEvent.as_dict`   s>    )48:X??.t?DMr   )	r   r   r   r5   r   r$   r(   r+   __classcell__)r7   s   @r   r/   r/   M   s     (.4<

 r   r/   c                    |si }t         j                  j                         D ch c]  \  }}||v r| }}}t        t        j                  j                               }|D ]!  }t        |      |v r|j                  |        # yc c}}w )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.
    N)r   registered_itemsitemslistr   valuestypepublish_event)eventexcluded_handler_types	hndl_typehndl_clsexcluded_handler_classeshandlershandlers          r   report_eventrL   i   s     "!# $6#F#F#L#L#N Ix.. 	    (,%66==?(H  %=44e$% s   Bc                 6    t        | |||      }t        |      S )zVReport a "finish" event.

    See :py:func:`.report_event` for parameter details.
    r4   )r/   rL   )
event_nameevent_descriptionr3   r4   rE   s        r   report_finish_eventrQ      s%     !%v*E r   c                 :    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_TYPErL   )rO   rP   rE   s      r   report_start_eventrT      s     +Z9JKEr   c                       e Zd ZdZdddej
                  dfdZd Zd Zd Z	e
d        Zej                  d        Ze
d	        Zej                  d
        Zd Zd Zy)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                 F   || _         || _        || _        || _        || _        t
        j                  | _        |g }|| _        ||r|j                  }nd}|| _	        |r*dj                  |j                  |f      | _        i | _        y | j                  | _        i | _        y )NT/)parentr   r    messageresult_on_exceptionr5   r   r3   r4   reporting_enabledjoinfullnamechildren)r   r   r    rZ   rY   r\   r[   r4   s           r   r$   zReportEventStack.__init__   s     	&#6 nnJ$ $$*$<$<!$(!!2HHOODM  !IIDMr   c                 V    d| j                   d| j                  d| j                  dS )NzReportEventStack(z, z, reporting_enabled=))r   r    r\   r'   s    r   __repr__zReportEventStack.__repr__   s%    II""
 	
r   c                     t         j                  | _        | j                  r t	        | j
                  | j                         | j                  r#d| j                  j                  | j                  <   | S )N)NN)
r5   r   r3   r\   rT   r^   r    rY   r_   r   r'   s    r   	__enter__zReportEventStack.__enter__   sN    nn!!t}}d.>.>?;;.:DKK  +r   c                     t         j                  t         j                  fD ]>  }| j                  j	                         D ]  \  }\  }}||k(  s|| j
                  fc c S  @ | j                  | j
                  fS r   )r5   r   r   r_   r@   rZ   r3   )r   cand_result_namevalue_msgs        r   _childrens_finish_infoz'ReportEventStack._childrens_finish_info   so    "KK5 	1K(,(;(;(= 1$}tK'!4<<001	1 T\\**r   c                     | j                   S r   )_resultr'   s    r   r3   zReportEventStack.result  s    ||r   c                 >    |t         vrt        d|z        || _        y )Nz'%s' not a valid result)r5   r6   rl   r   rh   s     r   r3   zReportEventStack.result  s"    6>??r   c                 J    | j                   | j                   S | j                  S r   )_messager    r'   s    r   rZ   zReportEventStack.message  s"    ==$== r   c                     || _         y r   )rp   rn   s     r   rZ   zReportEventStack.message  s	    r   c                 V    |r| j                   | j                  fS | j                         S r   )r[   rZ   rj   )r   excs     r   _finish_infozReportEventStack._finish_info  s)    ,,dll;;**,,r   c                     | j                  |      \  }}| j                  r%||f| j                  j                  | j                  <   | j                  r$t        | j                  ||| j                         y y )NrN   )rt   rY   r_   r   r\   rQ   r^   r4   )r   exc_type	exc_value	tracebackr3   msgs         r   __exit__zReportEventStack.__exit__!  s`    )))4;;/5smDKK  +!!sFt "r   )r   r   r   r,   r5   r   r$   rb   rd   rj   propertyr3   setterrZ   rt   rz   r   r   r   rV   rV      s    $T "KK%N
+   ]] 
    
 ^^ -r   rV   c                    | sy g }| D ]  }t         j                  j                  |      sd }ngt        j                  d|       5  t        |d      5 }t        j                  |j                               j                         }d d d        d d d        |j                  |dd        |S # 1 sw Y   *xY w# 1 sw Y   .xY w)NzReading rbbase64)pathcontentencoding)osr   isfiler   Timedopenr   	b64encodereaddecodeappend)r:   retfnamer   fps        r   r;   r;   +  s    
C Nww~~e$G""XeW#56 ?t9 ? **27795<<>? ? 	

Eg8LMN J? ? ? ?s$   B92B-B9-B62B99C	r   ) r,   r   loggingos.pathr   r"   typingr   r
   r   cloudinit.reportingr   r   cloudinit.reporting.handlersr   r2   rS   r-   	getLoggerr   LOGsetr   r5   r   r/   rL   r   rQ   rT   rV   r;   r   r   r   <module>r      s   
      ! :  " g!<s < 
-	.!
 !
H> 8%@ +1..T
E EPr   