
    im&                     $   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durationstatus
ValueError)r   r   r    r5   r3   r4   	__class__s         r   r$   zFinishReportingEvent.__init__N   s\     	"D2t[	
 J$ 1F:;;  r   c                     dj                  | j                  | j                  | j                  | j                  | j
                        S )Nz'{0}: {1}: {2}: {3} (duration: {4:.3f}s))r&   r   r   r3   r    r5   r'   s    r   r(   zFinishReportingEvent.as_stringa   s:    8??OOIIKKMM
 	
r   c                     t         t        |          }| j                  |d<   t	        | j
                  d      |d<   | j                  rt        | j                        |d<   |S )z'The event represented as json friendly.r3      r5   files)r1   r/   r+   r3   roundr5   r4   _collect_file_info)r   datar8   s     r   r+   zFinishReportingEvent.as_dictj   sS    )48:X 2Z??.t?DMr   )	r   r   r   r6   r   r$   r(   r+   __classcell__)r8   s   @r   r/   r/   M   s#     ~~<&
 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_eventrO   t   s     "!# $6#F#F#L#L#N Ix.. 	    (,%66==?(H  %=44e$% s   Bc                 8    t        | ||||      }t        |      S )zVReport a "finish" event.

    See :py:func:`.report_event` for parameter details.
    )r4   )r/   rO   )
event_nameevent_descriptionr5   r3   r4   rH   s         r   report_finish_eventrS      s'     !%xJ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_TYPErO   )rQ   rR   rH   s      r   report_start_eventrV      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                 T   || _         || _        || _        || _        || _        t
        j                  | _        d | _        |g }|| _	        ||r|j                  }nd}|| _
        |r*dj                  |j                  |f      | _        i | _        y | j                  | _        i | _        y )NT/)parentr   r    messageresult_on_exceptionr6   r   r3   start_timestampr4   reporting_enabledjoinfullnamechildren)r   r   r    r\   r[   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                  | _        t        j                         | _        | j                  r t        | j                  | j                         | j                  r#d| j                  j                  | j                  <   | S )N)NN)r6   r   r3   r"   	monotonicr^   r_   rV   ra   r    r[   rb   r   r'   s    r   	__enter__zReportEventStack.__enter__  s]    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   )r6   r   r   rb   rC   r\   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)r6   r7   rp   r   rl   s     r   r3   zReportEventStack.result  s"    6>??r   c                 J    | j                   | j                   S | j                  S r   )_messager    r'   s    r   r\   zReportEventStack.message"  s"    ==$== r   c                     || _         y r   )rt   rr   s     r   r\   zReportEventStack.message(  s	    r   c                 V    |r| j                   | j                  fS | j                         S r   )r]   r\   rn   )r   excs     r   _finish_infozReportEventStack._finish_info,  s)    ,,dll;;**,,r   c                 P   | j                  |      \  }}| j                  r%||f| j                  j                  | j                  <   | j                  rU| j
                  "t        j                         | j
                  z
  }nd}t        | j                  |||| j                         y y )Ng        )r5   r3   r4   )rx   r[   rb   r   r_   r^   r"   rg   rS   ra   r4   )r   exc_type	exc_value	tracebackr3   msgr5   s          r   __exit__zReportEventStack.__exit__2  s    )))4;;/5smDKK  +!!##/>>+d.B.BB!?? "r   )r   r   r   r,   r6   r   r$   re   rh   rn   propertyr3   setterr\   rx   r~   r   r   r   rX   rX      s    $T "KK&P
+   ]] 
    
 ^^ -r   rX   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>   D  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   rU   r-   	getLoggerr   LOGsetr   r6   r   r/   rO   r   rS   rV   rX   r>   r   r   r   <module>r      s   
      ! :  " g!<s < 
-	.!
 !
H$> $N%F >>"O Odr   