a '¡Dg$ã@sàdZddlZddlZddlZddlZddlmZddlm Z ddl m Z m Z ddl mZdZdZd Ze e¡ZGd d „d eƒZed ƒZGd d„dƒZGdd„deƒZddd„Zejdfdd„Zdd„ZGdd„dƒZdd„ZdS)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)ÚReportingHandlerZfinishÚstartÚ cloudinitc@seZdZdd„ZdS)Ú_namesetcCs||vr |Std|ƒ‚dS)Nz%s not a valid value)ÚAttributeError)ÚselfÚname©r ú>/usr/lib/python3.9/site-packages/cloudinit/reporting/events.pyÚ __getattr__ sz_nameset.__getattr__N)Ú__name__Ú __module__Ú __qualname__rr r r rr sr )ÚSUCCESSÚWARNÚFAILc@s.eZdZdZedfdd„Zdd„Zdd„ZdS) ÚReportingEventz"Encapsulation of event formatting.NcCs2||_||_||_||_|dur(t ¡}||_dS©N)Ú event_typer Ú descriptionÚoriginÚtimeÚ timestamp)r rr rrrr r rÚ__init__,szReportingEvent.__init__cCsd |j|j|j¡S)z"The event represented as a string.z {0}: {1}: {2})Úformatrr r©r r r rÚ as_string<s ÿzReportingEvent.as_stringcCs|j|j|j|j|jdœS)z&The event represented as a dictionary.©r rrrrr!rr r rÚas_dictBs ûzReportingEvent.as_dict)rrrÚ__doc__ÚDEFAULT_EVENT_ORIGINrr r"r r r rr)s ú rcs8eZdZejdf‡fdd„ Zdd„Z‡fdd„Z‡ZS)ÚFinishReportingEventNcsDtt|ƒ t||¡||_|dur&g}||_|tvr@td|ƒ‚dS)NzInvalid result: %s)Úsuperr%rÚFINISH_EVENT_TYPEÚresultÚ post_filesÚstatusÚ ValueError)r r rr(r)©Ú __class__r rrNs ÿzFinishReportingEvent.__init__cCsd |j|j|j|j¡S)Nz{0}: {1}: {2}: {3})rrr r(rrr r rr [sÿzFinishReportingEvent.as_stringcs0tt|ƒ ¡}|j|d<|jr,t|jƒ|d<|S)z'The event represented as json friendly.r(Úfiles)r&r%r"r(r)Ú_collect_file_info)r Údatar,r rr"`s  zFinishReportingEvent.as_dict) rrrr*rrr r"Ú __classcell__r r r,rr%Msÿ r%csTˆsi‰‡fdd„tj ¡Dƒ}ttj ¡ƒ}|D]}t|ƒ|vrDq2| |¡q2dS)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. csh|]\}}|ˆvr|’qSr r )Ú.0Z hndl_typeZhndl_cls©Úexcluded_handler_typesr rÚ ysþzreport_event..N)rZregistered_itemsÚitemsÚlistrÚvaluesÚtypeZ publish_event)Úeventr4Zexcluded_handler_classesÚhandlersZhandlerr r3rÚ report_eventis þÿ r<cCst||||d�}t|ƒS)zVReport a "finish" event. See :py:func:`.report_event` for parameter details. ©r))r%r<)Ú event_nameÚevent_descriptionr(r)r:r r rÚreport_finish_eventˆsÿr@cCstt||ƒ}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_TYPEr<)r>r?r:r r rÚreport_start_event•s rBc@s‚eZdZdZdddejdfdd„Zdd„Zdd„Zd d „Z e d d „ƒZ e j d d „ƒZ e dd„ƒZ e j dd„ƒZ dd„Zdd„ZdS)ÚReportEventStackaKContext 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 / :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. NcCs€||_||_||_||_||_tj|_|dur2g}||_|durP|rL|j }nd}||_ |rnd  |j |f¡|_ n|j|_ i|_ dS)NTú/) Úparentr rÚmessageÚresult_on_exceptionr*rr(r)Úreporting_enabledÚjoinÚfullnameÚchildren)r r rrFrErHrGr)r r rrÊs, þÿzReportEventStack.__init__cCsd|j|j|jfS)Nz.ReportEventStack(%s, %s, reporting_enabled=%s))r rrHrr r rÚ__repr__ñs ýzReportEventStack.__repr__cCs4tj|_|jrt|j|jƒ|jr0d|jj|j <|S)N)NN) r*rr(rHrBrJrrErKr rr r rÚ __enter__øs zReportEventStack.__enter__cCsPtjtjfD]6}|j ¡D]&\}\}}||kr||jfSqq |j|jfSr)r*rrrKr6rFr()r Z cand_resultÚ_nameÚvalueZ_msgr r rÚ_childrens_finish_infos z'ReportEventStack._childrens_finish_infocCs|jSr)Ú_resultrr r rr(szReportEventStack.resultcCs|tvrtd|ƒ‚||_dS)Nz'%s' not a valid result)r*r+rQ©r rOr r rr( s cCs|jdur|jS|jSr)Ú_messagerrr r rrFs zReportEventStack.messagecCs ||_dSr)rSrRr r rrFscCs|r|j|jfS| ¡Sr)rGrFrP)r Úexcr r rÚ _finish_infos zReportEventStack._finish_infocCsD| |¡\}}|jr&||f|jj|j<|jr@t|j|||jd�dS)Nr=)rUrErKr rHr@rJr))r Úexc_typeÚ exc_valueÚ tracebackr(Úmsgr r rÚ__exit__!s ÿzReportEventStack.__exit__)rrrr#r*rrrLrMrPÚpropertyr(ÚsetterrFrUrZr r r rrC£s(*ø '    rCc Csª|sdSg}|D]”}tj |¡s&d}nlt d|›�¡�Lt|dƒ�"}t | ¡¡  ¡}Wdƒn1sj0YWdƒn1sˆ0Y|  ||ddœ¡q|S)NzReading ÚrbÚbase64)ÚpathÚcontentÚencoding) Úosr_ÚisfilerZTimedÚopenr^Z b64encodeÚreadÚdecodeÚappend)r.ÚretÚfnamer`Úfpr r rr/+s ÿNr/)N) r#r^ZloggingZos.pathrbrÚtypingrrrZcloudinit.reportingrrZcloudinit.reporting.handlersrr'rAr$Z getLoggerrZLOGÚsetr r*rr%r<rr@rBrCr/r r r rÚs.    $ ÿ