o
    w7e&                     @   s   d dl Z d dlZd dlmZ d dlmZ d dl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/d0Z&d3d1d2Z'dS )4    N)importer)log)
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__D   s   
zHandler.__init__c                 C   s   dt | |  f S )Nz%s: [%s])r   obj_name
list_typesr   r   r   r   __repr__H   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>P   s    z&Handler.list_types.<locals>.<listcomp>)hasattrNotImplementedErrorgetattrr   r   r   r   r   K   s
   
zHandler.list_typesc                 O   s   t  r   )r&   )r   argskwargsr   r   r   handle_partU   s   zHandler.handle_partN)r   )	__name__
__module____qualname__r   r   r   abcabstractmethodr*   r   r   r   r   r   C   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_partZ   sP   

rD   c                 C      dt i}t| |d d || d S Nr2   )CONTENT_STARTrD   r<   r=   r   r@   r   r   r   
call_begin      rI   c                 C   rE   rF   )CONTENT_ENDrD   rH   r   r   r   call_end   rJ   rL   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)initializedr1   z7Failed at registering python file: %s (part handler %s))PART_HANDLER_FN_TMPLospathjoinendswithr   
write_filefixup_handlerr   import_modulerI   registerr:   r;   r8   )
pdata_ctype	_filenamer?   curcountmodnamer   modfnamerO   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
r1   r      ignore)errors)
isinstancestrsplitdecoder   UnicodeDecodeError)blobsizestartr   r   r   _extract_first_or_bytes   s   
$rm   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LookupErrorr5   r7   )textr   r   r   _escape_string   s   rs   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 )Nr2   excludedzcontent_type "%s" is excludedrO   r=   r      z'%s...'z)Unhandled non-multipart (%s) userdata: %sz0Unhandled unknown content-type (%s) userdata: %szEmpty payload of type %s)
getr8   r9   PART_CONTENT_TYPESr`   rD   rm   rs   NOT_MULTIPART_TYPEwarning)r=   r>   r?   r@   rC   rO   rl   detailsr   r   r   walker_callback   s@   
r{   c           	      C   s   d}|   D ]:}| dkrq| }|d u rt}| }|s#t| }t|}t| ||d< t	
|}||||| |d }qd S )Nr   	multipartr2   r1   )walkget_content_maintypeget_content_type
OCTET_TYPEget_filenamePART_FN_TPLdictr8   r9   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   r1   r   z&Handler %s has an unknown frequency %s)r%   setattrr   r   r8   ry   )r<   def_freqfreqr   r   r   rW     s   

rW   c                 C   sV   z	t |  }W n ty   | Y S w | }tD ]}||r(t|   S q|S r   )r   decode_binarylowerri   lstripINCLUSION_SRCH
startswithr!   )r?   default
payload_lcrr   r   r   r   type_from_starts_with$  s   
r   r   )(r.   rR   	cloudinitr   r   loggingr   r   cloudinit.settingsr   r   r   	getLoggerr+   r8   rx   r   rK   rG   CONTENT_SIGNALSrw   rQ   r   r!   sortedlistkeysr   ABCMetar   rD   rI   rL   r`   rm   rs   r{   r}   rW   r   r   r   r   r   <module>   sT   

0)