
    x[h$                     (   S r SSKrSSKrSSKrSSKrSSKJr  SSKJ	r	  SSK
JrJr  SSKJr  SrSrS	r\R$                  " \5      r " S
 S\5      r\" S5      r " S S5      r " S S\5      rSS jr\R6                  S4S jrS r " S S5      rS rg)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                       \ rS rSrS rSrg)_nameset   c                 ,    X;   a  U$ [        SU-  5      e)Nz%s not a valid value)AttributeError)selfnames     </usr/lib/python3/dist-packages/cloudinit/reporting/events.py__getattr___nameset.__getattr__    s    <K3d:;;     N)__name__
__module____qualname____firstlineno__r   __static_attributes__r   r   r   r   r      s    <r   r   )SUCCESSWARNFAILc                   2    \ rS rSrSr\S4S jrS rS rSr	g)ReportingEvent)   z"Encapsulation of event formatting.Nc                 p    Xl         X l        X0l        X@l        Uc  [        R                  " 5       nXPl        g N)
event_typer   descriptionorigintime	timestamp)r   r$   r   r%   r&   r(   s         r   __init__ReportingEvent.__init__,   s/     %	&		I"r   c                 d    SR                  U R                  U R                  U R                  5      $ )z"The event represented as a string.z{0}: {1}: {2})formatr$   r   r%   r   s    r   	as_stringReportingEvent.as_string<   s*    %%OOTYY(8(8
 	
r   c                 v    U R                   U R                  U R                  U R                  U R                  S.$ )z&The event represented as a dictionary.r   r%   r$   r&   r(   r1   r-   s    r   as_dictReportingEvent.as_dictB   s3     II++//kk
 	
r   )r%   r$   r   r&   r(   )
r   r   r   r   __doc__DEFAULT_EVENT_ORIGINr)   r.   r2   r   r   r   r   r    r    )   s    , $# 

r   r    c                   V   ^  \ rS rSr\R
                  S4U 4S jjrS rU 4S jrSr	U =r
$ )FinishReportingEventM   Nc                    > [         [        U ]  [        X5        X0l        Uc  / nX@l        U[        ;  a  [        SU-  5      eg )NzInvalid result: %s)superr7   r)   FINISH_EVENT_TYPEresult
post_filesstatus
ValueError)r   r   r%   r<   r=   	__class__s        r   r)   FinishReportingEvent.__init__N   sO     	"D2t	
 J$1F:;;  r   c                 z    SR                  U R                  U R                  U R                  U R                  5      $ )Nz{0}: {1}: {2}: {3})r,   r$   r   r<   r%   r-   s    r   r.   FinishReportingEvent.as_string[   s0    #**OOTYYT5E5E
 	
r   c                    > [         [        U ]  5       nU R                  US'   U R                  (       a  [        U R                  5      US'   U$ )z'The event represented as json friendly.r<   files)r:   r7   r2   r<   r=   _collect_file_info)r   datar@   s     r   r2   FinishReportingEvent.as_dict`   s>    )48:X??.t?DMr   )r=   r<   )r   r   r   r   r>   r   r)   r.   r2   r   __classcell__)r@   s   @r   r7   r7   M   s     (.4<

 r   r7   c                 8   U(       d  0 n[         R                  R                  5        VVs1 s H  u  p#X!;   d  M  UiM     nnn[        [        R                  R                  5       5      nU H%  n[        U5      U;   a  M  UR                  U 5        M'     gs  snnf )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_eventrX   i   s     "!# $6#F#F#L#L#N #NI. 	#N    (,%66==?(H =44e$  s
   BBc                 ,    [        XX#S9n[        U5      $ )zNReport a "finish" event.

See :py:func:`.report_event` for parameter details.
r=   )r7   rX   )
event_nameevent_descriptionr<   r=   rQ   s        r   report_finish_eventr]      s      !vE r   c                 8    [        [        X5      n[        U5      $ )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_TYPErX   )r[   r\   rQ   s      r   report_start_eventr`      s     +ZKEr   c                       \ rS rSrSrSSS\R                  S4S jrS rS r	S r
\S 5       r\R                  S	 5       r\S
 5       r\R                  S 5       rS rS rSrg)ReportEventStack   a  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                 B   X@l         Xl        X l        X0l        X`l        [
        R                  U l        Uc  / nXpl        Uc  U(       a  UR                  nOSnXPl	        U(       a#  SR                  UR                  U45      U l        OU R                  U l        0 U l        g )NT/)parentr   r%   messageresult_on_exceptionr>   r   r<   r=   reporting_enabledjoinfullnamechildren)r   r   r%   rg   rf   ri   rh   r=   s           r   r)   ReportEventStack.__init__   s     	&#6 nnJ$ $$*$<$<!$(!!2HHOODM !IIDMr   c                 \    SU R                   < SU R                  < SU R                  < S3$ )NzReportEventStack(z, z, reporting_enabled=))r   r%   ri   r-   s    r   __repr__ReportEventStack.__repr__   s%    II""
 	
r   c                     [         R                  U l        U R                  (       a   [	        U R
                  U R                  5        U R                  (       a#  SU R                  R                  U R                  '   U $ )N)NN)
r>   r   r<   ri   r`   rk   r%   rf   rl   r   r-   s    r   	__enter__ReportEventStack.__enter__   sN    nn!!t}}d.>.>?;;.:DKK  +r   c                     [         R                  [         R                  4 H?  nU R                  R	                  5        H  u  nu  p4X1:X  d  M  X0R
                  4s  s  $    MA     U R                  U R
                  4$ r#   )r>   r   r   rl   rL   rg   r<   )r   cand_result_namevalue_msgs        r   _childrens_finish_info'ReportEventStack._childrens_finish_info   s_    "KK5K(,(;(;(=$}'!<<00 )> 6 T\\**r   c                     U R                   $ r#   )_resultr-   s    r   r<   ReportEventStack.result  s    ||r   c                 @    U[         ;  a  [        SU-  5      eXl        g )Nz'%s' not a valid result)r>   r?   r}   r   rx   s     r   r<   r~     s     6>??r   c                 L    U R                   b  U R                   $ U R                  $ r#   )_messager%   r-   s    r   rg   ReportEventStack.message  s"    ==$== r   c                     Xl         g r#   )r   r   s     r   rg   r     s    r   c                 `    U(       a  U R                   U R                  4$ U R                  5       $ r#   )rh   rg   rz   )r   excs     r   _finish_infoReportEventStack._finish_info  s)    ,,dll;;**,,r   c                     U R                  U5      u  pEU R                  (       a$  XE4U R                  R                  U R                  '   U R                  (       a   [        U R                  XTU R                  S9  g g )NrZ   )r   rf   rl   r   ri   r]   rk   r=   )r   exc_type	exc_value	tracebackr<   msgs         r   __exit__ReportEventStack.__exit__!  sZ    )))4;;/5mDKK  +!!st "r   )r   r}   rl   r%   rk   rg   r   rf   r=   ri   r<   rh   )r   r   r   r   r4   r>   r   r)   rp   rs   rz   propertyr<   setterrg   r   r   r   r   r   r   rb   rb      s    $T "KK%N
+   ]] 
    
 ^^ -r   rb   c                    U (       d  g / nU  H  n[         R                  R                  U5      (       d  S nOi[        R                  " SU 35         [        US5       n[        R                  " UR                  5       5      R                  5       nS S S 5        S S S 5        UR                  UWSS.5        M     U$ ! , (       d  f       N1= f! , (       d  f       N:= f)NzReading rbbase64)pathcontentencoding)osr   isfiler   Timedopenr   	b64encodereaddecodeappend)rE   retfnamer   fps        r   rF   rF   +  s    
Cww~~e$$G""XeW#56t9 **27795<<>96 	

Eg8LM  J9 966s$   C3B;C;
C	C
C	r#   ) r4   r   loggingos.pathr   r'   typingr   r
   r   cloudinit.reportingr   r   cloudinit.reporting.handlersr   r;   r_   r5   	getLoggerr   LOGsetr   r>   r    r7   rX   r   r]   r`   rb   rF   r   r   r   <module>r      s   
      ! :  " !<s < 
-	.!
 !
H> 8%@ +1..T
E EPr   