U
    k7g                     @   sf   d dl Z d dlZd dlm  mZ dd Zdd ZG dd deZ	G dd	 d	e
ZG d
d deZdS )    Nc                 C   sB   | rd| kr| S d dd | dD }|d  |dd   S )N_ c                 S   s   g | ]}|  qS  )title).0xr   r   9/tmp/pip-unpacked-wheel-qtbjxrb6/twilio/twiml/__init__.py
<listcomp>
   s     zlower_camel.<locals>.<listcomp>r      )joinsplitlower)stringresultr   r   r   lower_camel   s    r   c                 C   s@   | s| S t d| std| dd  d | dd   S )zj
    Attempt to format language parameter as 'ww-WW'.

    :param string language: language parameter
    z^[a-zA-Z]{2}[_-][a-zA-Z]{2}$z%Invalid value for language parameter.r      -      )rematchTwiMLExceptionr   upper)languager   r   r   format_language   s
    r   c                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__r   r   r   r   r      s   r   c                   @   sh   e Zd ZddddddZdd Zd	d
 Zdd Zdd ZdddZdd Z	dd Z
dd ZdddZdS )TwiMLfromzxml:langzinterpret-asforbreak)from_Zxml_langZinterpret_asZfor_Zbreak_c                 K   sR   | j j| _d | _g | _i | _| D ](\}}|d k	r$|| jt| j	||< q$d S N)
	__class__r   namevalueverbsattrsitemsr   MAPget)selfkwargskvr   r   r   __init__*   s    
zTwiML.__init__c                 C   s   |   S r#   )to_xmlr,   r   r   r   __str__4   s    zTwiML.__str__c                 C   s   | S r#   r   r2   r   r   r   	__enter__7   s    zTwiML.__enter__c                 C   s   dS )NFr   )r,   exc_type	exc_value	tracebackr   r   r   __exit__:   s    zTwiML.__exit__Tc                 C   s*   t j|  ddd}|r&d|S |S )z
        Return the contents of this verb as an XML string

        :param bool xml_declaration: Include the XML declaration. Defaults to True
        zutf-8)encodingz(<?xml version="1.0" encoding="UTF-8"?>{})ETtostringxmldecodeformat)r,   xml_declarationr<   r   r   r   r1   =   s
    zTwiML.to_xmlc                 C   s   |  | | S )z^
        Add a TwiML doc

        :param verb: TwiML Document

        :returns: self
        )nestr,   verbr   r   r   appendJ   s    
zTwiML.appendc                 C   s,   t |tst |tstd| j| |S )z
        Add a TwiML doc. Unlike `append()`, this returns the created verb.

        :param verb: TwiML Document

        :returns: the TwiML verb
        z-Only nesting of TwiML and strings are allowed)
isinstancer   strr   r'   rC   rA   r   r   r   r@   U   s    z
TwiML.nestc                 C   s   t | j}| j }t|}|D ]>}| j| }t|trP||t	|
  q"||t	| q"| jrt| jtrt| j| _| j|_d }| jD ]8}t|t	r|d k	r||_q||_q| }|| q|S r#   )r:   Elementr%   r(   keyssortedrD   boolsetrE   r   r&   dictjsondumpstextr'   tailr<   rC   )r,   elrG   ar&   Z
last_childrB   r   r   r   r<   c   s*    




z	TwiML.xmlNc                 K   s   |  t||f|S r#   )r@   GenericNoder,   r%   r&   r-   r   r   r   	add_child   s    zTwiML.add_child)T)N)r   r   r   r*   r0   r3   r4   r8   r1   rC   r@   r<   rT   r   r   r   r   r   !   s   

!r   c                       s   e Zd Z fddZ  ZS )rR   c                    s"   t t| jf | || _|| _d S r#   )superrR   r0   r%   r&   rS   r$   r   r   r0      s    zGenericNode.__init__)r   r   r   r0   __classcell__r   r   rV   r   rR      s   rR   )rL   r   xml.etree.ElementTreeetreeElementTreer:   r   r   	Exceptionr   objectr   rR   r   r   r   r   <module>   s   g