f#dZddlZddlZddlZddlmZddlmZm Z ddl m Z dZ dZ dZGd d eZed ZGd d ZGddeZddZej*dfdZdZGddZdZy)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_handlersinstantiated_handler_registry)ReportingHandlerfinishstart cloudinitceZdZdZy)_namesetc*||vr|Std|z)Nz%s not a valid value)AttributeError)selfnames )48:X ??.t?DM r) rrrr4rr#r'r* __classcell__)r6s@rr.r.Js (.4 < rr.c|si}tjjDchc] \}}||vr| }}}ttjj }|D]!}t ||vr|j|#ycc}}w)aReport 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)rregistered_itemsitemslistrvaluestype publish_event)eventexcluded_handler_types hndl_typehndl_clsexcluded_handler_classeshandlershandlers r report_eventrKfs "!#$6#F#F#L#L#N  Ix . .    (,%66==?(H% =4 4 e$% sBc6t||||}t|S)zVReport a "finish" event. See :py:func:`.report_event` for parameter details. r3)r.rK) event_nameevent_descriptionr2r3rDs rreport_finish_eventrPs% !%v* E  rc:tt||}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_TYPErK)rNrOrDs rreport_start_eventrSs +Z9J KE  rceZdZdZdddej dfdZdZdZdZ e dZ e jdZ e d Z e jd Z d Zd Zy) 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. NcF||_||_||_||_||_t j |_|g}||_||r |j}nd}||_ |r*dj|j|f|_ i|_ y|j|_ i|_ y)NT/) parentrrmessageresult_on_exceptionr4rr2r3reporting_enabledjoinfullnamechildren)rrrrYrXr[rZr3s rr#zReportEventStack.__init__s  & #6 nn  J$  $$*$<$<!$(!!2 HHOODM !IIDM rcVd|jd|jd|jdS)NzReportEventStack(z, z, reporting_enabled=))rrr[r&s r__repr__zReportEventStack.__repr__s% II     " "  rctj|_|jr t |j |j |jr#d|jj|j<|S)N)NN) r4rr2r[rSr]rrXr^rr&s r __enter__zReportEventStack.__enter__sNnn  ! ! t}}d.>.> ? ;;.:DKK  + rctjtjfD]>}|jj D]\}\}}||k(s||j fccS@|j |j fSr)r4rrr^r?rYr2)r cand_result_namevalue_msgs r_childrens_finish_infoz'ReportEventStack._childrens_finish_infoso"KK5 1K(, (;(;(= 1$}tK'!4<<00 1 1 T\\**rc|jSr)_resultr&s rr2zReportEventStack.results ||rc>|tvrtd|z||_y)Nz'%s' not a valid result)r4r5rkrrgs rr2zReportEventStack.results"  6>? ? rcJ|j |jS|jSr)_messagerr&s rrYzReportEventStack.messages" == $== rc||_yr)rorms rrYzReportEventStack.messages  rcV|r|j|jfS|jSr)rZrYri)rexcs r _finish_infozReportEventStack._finish_infos) ,,dll; ;**,,rc|j|\}}|jr%||f|jj|j<|jr$t |j |||jyy)NrM)rsrXr^rr[rPr]r3)rexc_type exc_value tracebackr2msgs r__exit__zReportEventStack.__exit__s`)))4  ;;/5smDKK  +  ! !  sFt  "r)rrrr+r4rr#rarcripropertyr2setterrYrsryrrrrUrUs$T"KK%N + ]]    ^^- rrUc0|syg}|D]}tjj|sd}nFt|d5}t j |j j}ddd|j|dd|S#1swY"xYw)Nrbbase64)pathcontentencoding) osrisfileopenr~ b64encodereaddecodeappend)r9retfnamerfps rr:r:(s  CNww~~e$GeT" ?b **27795<<> ? Eg8LM N J ? ?s 2B  B r)r+r~os.pathrr!typingrcloudinit.reportingrrcloudinit.reporting.handlersrr1rRr,setr r4rr.rKrrPrSrUr:rrrrs   :"8%@+1..T  EEP r