U
    Ó‡g  ã                   @   sr   d Z ddlZddlmZ ddlmZmZ e e¡Z	G dd„ deƒZ
G dd„ deƒZeee
ee f d	œd
d„ZdS )z0Classes and functions related to event handling.é    N)ÚEnum)ÚDictÚSetc                   @   s   e Zd ZdZdd„ ZdS )Ú
EventScopeZnetworkc                 C   s   | j S ©N©Úvalue©Úself© r   ú1/usr/lib/python3/dist-packages/cloudinit/event.pyÚ__str__   s    zEventScope.__str__N)Ú__name__Ú
__module__Ú__qualname__ZNETWORKr   r   r   r   r   r      s   r   c                   @   s(   e Zd ZdZdZdZdZdZdd„ ZdS )	Ú	EventTypezCEvent types which can generate maintenance requests for cloud-init.Zbootzboot-new-instancezboot-legacyZhotplugc                 C   s   | j S r   r   r	   r   r   r   r   "   s    zEventType.__str__N)	r   r   r   Ú__doc__ZBOOTZBOOT_NEW_INSTANCEZBOOT_LEGACYZHOTPLUGr   r   r   r   r   r      s   r   )Úuser_configÚreturnc                 C   sÀ   i }|   ¡ D ]®\}}zt|ƒ}W n< tk
r\ } zt dt|ƒ|¡ W Y ¢qW 5 d}~X Y nX zdd„ |d D ƒ}W n8 tk
r¬ } zt dt|ƒ|¡ g }W 5 d}~X Y nX t|ƒ||< q|S )a  Convert userdata into update config format defined on datasource.

    Userdata is in the form of (e.g):
    {'network': {'when': ['boot']}}

    DataSource config is in the form of:
    {EventScope.Network: {EventType.BOOT}}

    Take the first and return the second
    z.%s! Update data will be ignored for '%s' scopeNc                 S   s   g | ]}t |ƒ‘qS r   )r   )Ú.0Úxr   r   r   Ú
<listcomp>=   s     z&userdata_to_events.<locals>.<listcomp>Zwhen)Úitemsr   Ú
ValueErrorÚLOGZwarningÚstrÚset)r   Zupdate_configZscopeZ
scope_listZ	new_scopeÚeZ
new_valuesr   r   r   Úuserdata_to_events&   s,    ýýr   )r   ZloggingÚenumr   Útypingr   r   Z	getLoggerr   r   r   r   Údictr   r   r   r   r   Ú<module>   s   

