3 \me#@sdZddlZddlZddlZddlmZddlmZm Z ddl m Z dZ dZ dZGd d d eZedZGdddZGdddeZdddZejdfddZddZGdddZddZdS)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)ReportingHandlerZfinishstartZ cloudinitc@seZdZddZdS)_namesetcCs||kr |Std|dS)Nz%s not a valid value)AttributeError)selfnamer /usr/lib/python3.6/events.py __getattr__sz_nameset.__getattr__N)__name__ __module__ __qualname__r r r r r rsrSUCCESSWARNFAILc@s.eZdZdZedfddZddZddZdS) ReportingEventz"Encapsulation of event formatting.NcCs2||_||_||_||_|dkr(tj}||_dS)N) event_typer descriptionorigintime timestamp)r rr rrrr r r __init__)szReportingEvent.__init__cCsdj|j|j|jS)z"The event represented as a string.z {0}: {1}: {2})formatrr r)r r r r as_string9szReportingEvent.as_stringcCs|j|j|j|j|jdS)z&The event represented as a dictionary.)r rrrr)r rrrr)r r r r as_dict?s zReportingEvent.as_dict)rrr__doc__DEFAULT_EVENT_ORIGINrrrr r r r r&s  rcs8eZdZejdffdd ZddZfddZZS)FinishReportingEventNcsDtt|jt||||_|dkr&g}||_|tkr@td|dS)NzInvalid result: %s)superr rFINISH_EVENT_TYPEresult post_filesstatus ValueError)r r rr#r$) __class__r r rKs  zFinishReportingEvent.__init__cCsdj|j|j|j|jS)Nz{0}: {1}: {2}: {3})rrr r#r)r r r r rXszFinishReportingEvent.as_stringcs0tt|j}|j|d<|jr,t|j|d<|S)z'The event represented as json friendly.r#files)r!r rr#r$_collect_file_info)r data)r'r r r]s  zFinishReportingEvent.as_dict) rrrr%rrrr __classcell__r r )r'r r Js r csXsifddtjjD}ttjj}x$|D]}t||krFq4|j|q4WdS)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. csh|]\}}|kr|qSr r ).0Z hndl_typeZhndl_cls)excluded_handler_typesr r vszreport_event..N)rZregistered_itemsitemslistrvaluestypeZ publish_event)eventr-Zexcluded_handler_classesZhandlersZhandlerr )r-r report_eventfs    r4cCst||||d}t|S)zVReport a "finish" event. See :py:func:`.report_event` for parameter details. )r$)r r4) event_nameevent_descriptionr#r$r3r r r report_finish_eventsr7cCstt||}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_TYPEr4)r5r6r3r r r report_start_events r9c@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|_|dkr2g}||_|dkrP|rL|j }nd}||_ |rndj |j |f|_ n|j|_ i|_ dS)NT/) parentr rmessageresult_on_exceptionr%rr#r$reporting_enabledjoinfullnamechildren)r r rr=r<r?r>r$r r r rs(  zReportEventStack.__init__cCsd|j|j|jfS)Nz.ReportEventStack(%s, %s, reporting_enabled=%s))r rr?)r r r r __repr__szReportEventStack.__repr__cCs4tj|_|jrt|j|j|jr0d|jj|j <|S)N)NN) r%rr#r?r9rArr<rBr )r r r r __enter__s zReportEventStack.__enter__cCsPxBtjtjfD]2}x,|jjD]\}\}}||kr||jfSqWqW|j|jfS)N)r%rrrBr/r=r#)r Z cand_result_namevalueZ_msgr r r _childrens_finish_infos z'ReportEventStack._childrens_finish_infocCs|jS)N)_result)r r r r r#szReportEventStack.resultcCs|tkrtd|||_dS)Nz'%s' not a valid result)r%r&rH)r rFr r r r#s cCs|jdk r|jS|jS)N)_messager)r r r r r=s zReportEventStack.messagecCs ||_dS)N)rI)r rFr r r r=scCs|r|j|jfS|jS)N)r>r=rG)r excr r r _finish_infos zReportEventStack._finish_infocCsD|j|\}}|jr&||f|jj|j<|jr@t|j|||jddS)N)r$)rKr<rBr r?r7rAr$)r exc_type exc_value tracebackr#msgr r r __exit__s zReportEventStack.__exit__)rrrrr%rrrCrDrGpropertyr#setterr=rKrPr r r r r:s%   r:c Csj|sdSg}xX|D]P}tjj|s(d}n(t|d}tj|jj}WdQRX|j||ddqW|S)Nrbbase64)pathcontentencoding) osrUisfileopenrTZ b64encodereaddecodeappend)r(retZfnamerVfpr r r r)(s   r))rrr)N)rrTZos.pathrXrZtypingrZcloudinit.reportingrrZcloudinit.reporting.handlersrr"r8rsetrr%rr r4rr7r9r:r)r r r r  s&  $