o x[h$@sdZddlZddlZddlZddlZddlmZddlm Z ddl m Z m Z ddl mZdZdZd ZeeZGd d d eZed ZGd ddZGdddeZdddZejdfddZddZGdddZddZdS)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_handlersinstantiated_handler_registry)ReportingHandlerfinishstart cloudinitc@seZdZddZdS)_namesetcCs||vr|Std|)Nz%s not a valid value)AttributeError)selfnamerys zreport_event..N)rregistered_itemsitemslistrvaluestype publish_event)eventr7excluded_handler_classeshandlershandlerrr6r report_eventis   rCcCst||||d}t|S)zVReport a "finish" event. See :py:func:`.report_event` for parameter details. r*)r&rC) event_nameevent_descriptionr)r*r?rrrreport_finish_eventsrGcCstt||}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. )rSTART_EVENT_TYPErC)rErFr?rrrreport_start_events rIc@seZdZdZdddejdfddZddZddZd d Z e d d Z e j d d Z e ddZ e j ddZ ddZddZdS)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|_|durg}||_|dur(|r&|j }nd}||_ |r7d |j |f|_ n|j|_ i|_ dS)NT/) parentr rmessageresult_on_exceptionr+rr)r*reporting_enabledjoinfullnamechildren)r r rrMrLrOrNr*rrrrs,  zReportEventStack.__init__cCsd|j|j|jfS)Nz.ReportEventStack(%s, %s, reporting_enabled=%s))r rrOr rrr__repr__s zReportEventStack.__repr__cCs4tj|_|jrt|j|j|jrd|jj|j <|S)N)NN) r+rr)rOrIrQrrLrRr r rrr __enter__s zReportEventStack.__enter__cCsPtjtjfD]}|jD]\}\}}||kr ||jfSq q|j|jfSr)r+rrrRr:rMr))r cand_result_namevalue_msgrrr_childrens_finish_infos z'ReportEventStack._childrens_finish_infocCs|jSr)_resultr rrrr)szReportEventStack.resultcCs|tvr td|||_dS)Nz'%s' not a valid result)r+r,rZr rWrrrr) s  cCs|jdur|jS|jSr)_messagerr rrrrMs zReportEventStack.messagecCs ||_dSr)r\r[rrrrMs cCs|r|j|jfS|Sr)rNrMrY)r excrrr _finish_infos zReportEventStack._finish_infocCsH||\}}|jr||f|jj|j<|jr"t|j|||jddSdS)NrD)r^rLrRr rOrGrQr*)r exc_type exc_value tracebackr)msgrrr__exit__!s  zReportEventStack.__exit__)rrrr$r+rrrSrTrYpropertyr)setterrMr^rcrrrrrJs** '     rJc Cs|sdSg}|D]J}tj|sd}n6td|&t|d}t| }Wdn1s5wYWdn1sDwY| ||ddq|S)NzReading rbbase64)pathcontentencoding) osrhisfilerTimedopenrg b64encodereaddecodeappend)r/retfnamerifprrrr0+s  r0r) r$rgloggingos.pathrkrtypingrr rcloudinit.reportingrrcloudinit.reporting.handlersrr(rHr% getLoggerrLOGsetr r+rr&rCrrGrIrJr0rrrrs0    $