o
    f}&                     @   s  d dl Z d dlZd dlZd dlmZmZmZ d dlmZm	Z	m
Z
 eeZdZdZdZdZeegZdgZd	Zd
ZdddddddddddddZeee dd dZG dd de jdZdd Zdd  Zd!d" Zd#d$ Z d%d& Z!d'd( Z"d)d* Z#d+d, Z$e
fd-d.Z%d1d/d0Z&dS )2    N)importer
type_utilsutil)FREQUENCIES
PER_ALWAYSPER_INSTANCEztext/x-not-multipartzapplication/octet-stream__end__	__begin__ztext/part-handlerzpart-handler-%03dz	part-%03dztext/x-include-urlztext/x-include-once-urlztext/x-shellscriptztext/cloud-configztext/cloud-boothookztext/cloud-config-archiveztext/cloud-config-jsonpztext/jinja2text/x-shellscript-per-boottext/x-shellscript-per-instancetext/x-shellscript-per-once)z#includez#include-oncez#!z#cloud-configz#part-handlerz#cloud-boothookz#cloud-config-archivez#cloud-config-jsonpz## template: jinjar
   r   r   c                 C   s   dt |  S )Nr   )len)e r   =/usr/lib/python3/dist-packages/cloudinit/handlers/__init__.py<lambda>>   s    r   )keyc                   @   s4   e Zd ZdddZdd Zdd Zejdd	 Zd
S )Handler   c                 C   s   || _ || _d S N)handler_version	frequency)selfr   versionr   r   r   __init__C   s   
zHandler.__init__c                 C   s   dt | |  f S )Nz%s: [%s])r   obj_name
list_typesr   r   r   r   __repr__G   s   zHandler.__repr__c                 C   s&   t | ds	tddd t| dD S )Nprefixesz#Missing prefixes subclass attributec                 S   s   g | ]}t | qS r   )INCLUSION_TYPES_MAP).0prefixr   r   r   
<listcomp>O   s    z&Handler.list_types.<locals>.<listcomp>)hasattrNotImplementedErrorgetattrr   r   r   r   r   J   s
   
zHandler.list_typesc                 O   s   t  r   )r%   )r   argskwargsr   r   r   handle_partT   s   zHandler.handle_partN)r   )	__name__
__module____qualname__r   r   r   abcabstractmethodr)   r   r   r   r   r   B   s    

r   )	metaclassc           	      C   s  | j }|tks|tkr|tksd S z	| j}t|}W n tttfy(   d}Y nw |d }z@t	d| |||| |dkrI| 
|||||| W d S |dkrY| 
||||| W d S |dkrh| 
|||| W d S td|  ty   ttd| |||| Y d S w )N   Content-Typez1Calling handler %s (%s, %s, %s) with frequency %s   r   zUnknown module version %sz8Failed calling handler %s (%s, %s, %s) with frequency %s)r   r   r   r   int	TypeError
ValueErrorAttributeErrorLOGdebugr)   	Exceptionr   logexc)	moddatafilenamepayloadr   headersmod_freqmod_vercontent_typer   r   r   run_partY   sP   

rC   c                 C      dt i}t| |d d || d S Nr1   )CONTENT_STARTrC   r;   r<   r   r?   r   r   r   
call_begin      rH   c                 C   rD   rE   )CONTENT_ENDrC   rG   r   r   r   call_end   rI   rK   c           
   	   C   s   | d }t | }| d }tj| d d| }|ds d| }t||d | d }ztt	|}	t
|	| d	 | |j|	d
d |d | d< W d S  ty\   ttd|| Y d S w )Nhandlercountr   
handlerdirz%sz.pyz%s.pyi  handlersr<   T)initializedr0   z7Failed at registering python file: %s (part handler %s))PART_HANDLER_FN_TMPLospathjoinendswithr   
write_filefixup_handlerr   import_modulerH   registerr9   r:   r7   )
pdata_ctype	_filenamer>   curcountmodnamer   modfnamerN   r;   r   r   r   walker_handle_handler   s*   

r_   c                 C   s   z2t | tr| ddd }n| d d|  jddddd }t||kr0|d | }W |S W |S  tyB   | d| }Y |S w )N
r0   r      ignore)errors)
isinstancestrsplitdecoder   UnicodeDecodeError)blobsizestartr   r   r   _extract_first_or_bytes   s   
$rl   c                 C   sZ   z|  dW S  ttfy#   z|  dW  Y S  ty"   Y Y | S w  ty,   Y | S w )Nstring_escapeunicode_escape)encodeLookupErrorr4   r6   )textr   r   r   _escape_string   s   rr   c                 C   s   |d }||  dv rtd| d S |tv r t| ||| d S | d }||v r9t|| | d ||| d | d S |r\t|d}dt| }|tkrSt	d	|| d S t	d
|| d S td| d S )Nr1   excludedzcontent_type "%s" is excludedrN   r<   r      z'%s...'z)Unhandled non-multipart (%s) userdata: %sz0Unhandled unknown content-type (%s) userdata: %szEmpty payload of type %s)
getr7   r8   PART_CONTENT_TYPESr_   rC   rl   rr   NOT_MULTIPART_TYPEwarning)r<   r=   r>   r?   rB   rN   rk   detailsr   r   r   walker_callback   s@   
rz   c           	      C   s   d}|   D ]:}| dkrq| }|d u rt}| }|s#t| }t|}t| ||d< t	
|}||||| |d }qd S )Nr   	multipartr1   r0   )walkget_content_maintypeget_content_type
OCTET_TYPEget_filenamePART_FN_TPLdictr7   r8   r   fully_decoded_payload)	msgcallbackr<   partnumpartctyper=   r?   r>   r   r   r   r|      s"   


r|   c                 C   sT   t | dst| dd t | dst| d| | S | j}|r(|tvr(td| | | S )Nr   r0   r   z&Handler %s has an unknown frequency %s)r$   setattrr   r   r7   rx   )r;   def_freqfreqr   r   r   rV     s   

rV   c                 C   sV   z	t |  }W n ty   | Y S w | }tD ]}||r(t|   S q|S r   )r   decode_binarylowerrh   lstripINCLUSION_SRCH
startswithr    )r>   default
payload_lcrq   r   r   r   type_from_starts_with#  s   
r   r   )'r-   loggingrQ   	cloudinitr   r   r   cloudinit.settingsr   r   r   	getLoggerr*   r7   rw   r   rJ   rF   CONTENT_SIGNALSrv   rP   r   r    sortedlistkeysr   ABCMetar   rC   rH   rK   r_   rl   rr   rz   r|   rV   r   r   r   r   r   <module>   sR   

0)