
    f#                         d Z ddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZ dZdZdZ G d	 d
e      Z ed      Z G d d      Z G d de      ZddZej*                  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)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_string9   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_dict?   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   r2   r3   	__class__s        r   r#   zFinishReportingEvent.__init__K   sU     	"D2t[	
 J$1F:;;  r   c                 z    dj                  | j                  | j                  | j                  | j                        S )Nz{0}: {1}: {2}: {3})r%   r   r   r2   r   r&   s    r   r'   zFinishReportingEvent.as_stringX   s0    #**OOTYYT5E5E
 	
r   c                     t         t        |          }| j                  |d<   | j                  rt        | j                        |d<   |S )z'The event represented as json friendly.r2   files)r0   r.   r*   r2   r3   _collect_file_info)r   datar6   s     r   r*   zFinishReportingEvent.as_dict]   s>    )48:X??.t?DMr   )	r   r   r   r4   r   r#   r'   r*   __classcell__)r6   s   @r   r.   r.   J   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_eventrK   f   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.
    r3   )r.   rK   )
event_nameevent_descriptionr2   r3   rD   s        r   report_finish_eventrP      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_TYPErK   )rN   rO   rD   s      r   report_start_eventrS      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_exceptionr4   r   r2   r3   reporting_enabledjoinfullnamechildren)r   r   r   rY   rX   r[   rZ   r3   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)
r4   r   r2   r[   rS   r]   r   rX   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   )r4   r   r   r^   r?   rY   r2   )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   r2   zReportEventStack.result  s    ||r   c                 >    |t         vrt        d|z        || _        y )Nz'%s' not a valid result)r4   r5   rk   r   rg   s     r   r2   zReportEventStack.result  s"    6>??r   c                 J    | j                   | j                   S | j                  S r   )_messager   r&   s    r   rY   zReportEventStack.message  s"    ==$== r   c                     || _         y r   )ro   rm   s     r   rY   zReportEventStack.message  s	    r   c                 V    |r| j                   | j                  fS | j                         S r   )rZ   rY   ri   )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 )NrM   )rs   rX   r^   r   r[   rP   r]   r3   )r   exc_type	exc_value	tracebackr2   msgs         r   __exit__zReportEventStack.__exit__  s`    )))4;;/5smDKK  +!!sFt "r   )r   r   r   r+   r4   r   r#   ra   rc   ri   propertyr2   setterrY   rs   ry   r   r   r   rU   rU      s    $T "KK%N
+   ]] 
    
 ^^ -r   rU   c                 0   | sy g }| D ]  }t         j                  j                  |      sd }nFt        |d      5 }t	        j
                  |j                               j                         }d d d        |j                  |dd        |S # 1 sw Y   "xY w)Nrbbase64)pathcontentencoding)	osr   isfileopenr~   	b64encodereaddecodeappend)r9   retfnamer   fps        r   r:   r:   (  s    
C Nww~~e$GeT" ?b **27795<<>?

Eg8LMN J? ?s   2BB	r   )r+   r~   os.pathr   r!   typingr   cloudinit.reportingr   r   cloudinit.reporting.handlersr   r1   rR   r,   setr   r4   r   r.   rK   r   rP   rS   rU   r:   r   r   r   <module>r      s   
     :  " <s < 
-	.!
 !
H> 8%@ +1..T
E EPr   