name : events.cpython-39.pyc
a

'�Dg$�@s�dZddlZddlZddlZddlZddlmZddlm	Z	ddl
mZmZddl
mZdZdZd	Ze�e�ZGd
d�de�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)rrrrrrr
r
r�__init__,szReportingEvent.__init__cCsd�|j|j|j�S)z"The event represented as a string.z
{0}: {1}: {2})�formatrrr�rr
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)rr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�	<setcomp>ys�zreport_event.<locals>.<setcomp>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
dd��Zejd
d��Ze
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 <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.
    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)rr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()rZcand_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�rrOr
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�<module>s.
$
 �

	

© 2025 Cubjrnet7