U
    3do)                     @   s|   d d Z G dd deZG dd deZG dd deZG dd	 d	eZG d
d deZG dd deZG dd deZ	dS )c                 C   s   d|  }d|  }d|||f S )Nz<%s>z</%s>z%s%s%s )keyvaluestartendr   r   3/tmp/pip-unpacked-wheel-d7dsrkjd/boto/s3/website.pytag   s    r   c                   @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )WebsiteConfigurationa  
    Website configuration for a bucket.

    :ivar suffix: Suffix that is appended to a request that is for a
        "directory" on the website endpoint (e.g. if the suffix is
        index.html and you make a request to samplebucket/images/
        the data that is returned will be for the object with the
        key name images/index.html).  The suffix must not be empty
        and must not include a slash character.

    :ivar error_key: The object key name to use when a 4xx class error
        occurs.  This key identifies the page that is returned when
        such an error occurs.

    :ivar redirect_all_requests_to: Describes the redirect behavior for every
        request to this bucket's website endpoint. If this value is non None,
        no other values are considered when configuring the website
        configuration for the bucket. This is an instance of
        ``RedirectLocation``.

    :ivar routing_rules: ``RoutingRules`` object which specifies conditions
        and redirects that apply when the conditions are met.

    Nc                 C   s.   || _ || _|| _|d k	r"|| _nt | _d S N)suffix	error_keyredirect_all_requests_torouting_rulesRoutingRules)selfr
   r   r   r   r   r   r   __init__7   s    zWebsiteConfiguration.__init__c                 C   sF   |dkrt  | _| jS |dkr,tdg| dS |dkrBtdg| dS d S )Nr   IndexDocument)Suffixr
   )	containerErrorDocument)Keyr   )r   r   _XMLKeyValuer   nameattrs
connectionr   r   r   startElementA   s    z!WebsiteConfiguration.startElementc                 C   s   d S r	   r   r   r   r   r   r   r   r   
endElementJ   s    zWebsiteConfiguration.endElementc                 C   s   ddg}| j d k	r*|tdtd| j  | jd k	rL|tdtd| j | jd k	rf|| j  | jr||| j  |d d|S )	Nz&<?xml version="1.0" encoding="UTF-8"?>zF<WebsiteConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">r   r   r   r   z</WebsiteConfiguration> )r
   appendr   r   r   to_xmlr   joinr   partsr   r   r   r    M   s    



zWebsiteConfiguration.to_xml)NNNN)__name__
__module____qualname____doc__r   r   r   r    r   r   r   r   r      s       

	r   c                   @   s.   e Zd Zd
ddZdd Zdd Zdd	 ZdS )r   Nc                 C   s   || _ |r|| _n| | _d S r	   )
translatorr   )r   r(   r   r   r   r   r   ]   s    z_XMLKeyValue.__init__c                 C   s   d S r	   r   r   r   r   r   r   d   s    z_XMLKeyValue.startElementc                 C   s*   | j D ]\}}||krt| j|| qd S r	   )r(   setattrr   )r   r   r   r   xml_key	attr_namer   r   r   r   g   s    z_XMLKeyValue.endElementc                 C   sB   g }| j D ],\}}t| j|}|d k	r
|t|| q
d|S )Nr   )r(   getattrr   r   r   r!   )r   r#   r*   r+   contentr   r   r   r    l   s    z_XMLKeyValue.to_xml)N)r$   r%   r&   r   r   r   r    r   r   r   r   r   \   s   
r   c                       s6   e Zd ZdZddgZd	 fdd	Z fddZ  ZS )
RedirectLocationa*  Specify redirect behavior for every request to a bucket's endpoint.

    :ivar hostname: Name of the host where requests will be redirected.

    :ivar protocol: Protocol to use (http, https) when redirecting requests.
        The default is the protocol that is used in the original request.

    ZHostNamehostnameProtocolprotocolNc                    s"   || _ || _tt| | j d S r	   )r0   r3   superr.   r   
TRANSLATOR)r   r0   r3   	__class__r   r   r      s    zRedirectLocation.__init__c                    s   t dtt|  S )NZRedirectAllRequestsTo)r   r4   r.   r    r   r6   r   r   r       s    zRedirectLocation.to_xml)NNr$   r%   r&   r'   r5   r   r    __classcell__r   r   r6   r   r.   u   s   r.   c                       s<   e Zd Zdd Zdd Zdd Z fddZd	d
 Z  ZS )r   c                 C   s   |  | | S )z

        :type rule: :class:`boto.s3.website.RoutingRule`
        :param rule: A routing rule.

        :return: This ``RoutingRules`` object is returned,
            so that it can chain subsequent calls.

        )r   )r   ruler   r   r   add_rule   s    

zRoutingRules.add_rulec                 C   s(   |dkr$t t t }| | |S d S )NRoutingRule)r=   	ConditionRedirectr<   )r   r   r   r   r;   r   r   r   r      s    
zRoutingRules.startElementc                 C   s   d S r	   r   r   r   r   r   r      s    zRoutingRules.endElementc                    s   dt t|   S )NzRoutingRules(%s))r4   r   __repr__r8   r6   r   r   r@      s    zRoutingRules.__repr__c                 C   s,   g }| D ]}| |  qtdd|S )Nr   
)r   r    r   r!   )r   Z
inner_textr;   r   r   r   r       s    zRoutingRules.to_xml)	r$   r%   r&   r<   r   r   r@   r    r:   r   r   r6   r   r      s
   r   c                   @   sJ   e Zd ZdZdddZdd Zdd Zd	d
 ZedddZ	dddZ
dS )r=   a  Represents a single routing rule.

    There are convenience methods to making creating rules
    more concise::

        rule = RoutingRule.when(key_prefix='foo/').then_redirect('example.com')

    :ivar condition: Describes condition that must be met for the
        specified redirect to apply.

    :ivar redirect: Specifies redirect behavior.  You can redirect requests to
        another host, to another page, or with another protocol. In the event
        of an error, you can can specify a different error code to return.

    Nc                 C   s   || _ || _d S r	   	conditionredirect)r   rC   rD   r   r   r   r      s    zRoutingRule.__init__c                 C   s    |dkr| j S |dkr| jS d S )Nr>   r?   rB   r   r   r   r   r      s    zRoutingRule.startElementc                 C   s   d S r	   r   r   r   r   r   r      s    zRoutingRule.endElementc                 C   s@   g }| j r|| j   | jr0|| j  tdd|S )Nr=   rA   )rC   r   r    rD   r   r!   r"   r   r   r   r       s    zRoutingRule.to_xmlc                 C   s   | t ||dd S )N)
key_prefixhttp_error_code)r>   )clsrE   rF   r   r   r   when   s
    zRoutingRule.whenc                 C   s   t |||||d| _| S )N)r0   r3   replace_keyreplace_key_prefixhttp_redirect_code)r?   rD   r   r0   r3   rI   rJ   rK   r   r   r   then_redirect   s     zRoutingRule.then_redirect)NN)NN)NNNNN)r$   r%   r&   r'   r   r   r   r    classmethodrH   rM   r   r   r   r   r=      s   
    r=   c                       s6   e Zd ZdZddgZd	 fdd	Z fddZ  ZS )
r>   a$  
    :ivar key_prefix: The object key name prefix when the redirect is applied.
        For example, to redirect requests for ExamplePage.html, the key prefix
        will be ExamplePage.html. To redirect request for all pages with the
        prefix docs/, the key prefix will be /docs, which identifies all
        objects in the docs/ folder.

    :ivar http_error_code: The HTTP error code when the redirect is applied. In
        the event of an error, if the error code equals this value, then the
        specified redirect is applied.

    )ZKeyPrefixEqualsrE   )ZHttpErrorCodeReturnedEqualsrF   Nc                    s"   || _ || _tt| | j d S r	   )rE   rF   r4   r>   r   r5   )r   rE   rF   r6   r   r   r      s    zCondition.__init__c                    s   t dtt|  S )Nr>   )r   r4   r>   r    r8   r6   r   r   r       s    zCondition.to_xml)NNr9   r   r   r6   r   r>      s   r>   c                       s<   e Zd ZdZdddddgZd fdd		Z fd
dZ  ZS )r?   a  
    :ivar hostname: The host name to use in the redirect request.

    :ivar protocol: The protocol to use in the redirect request.  Can be either
    'http' or 'https'.

    :ivar replace_key: The specific object key to use in the redirect request.
        For example, redirect request to error.html.

    :ivar replace_key_prefix: The object key prefix to use in the redirect
        request. For example, to redirect requests for all pages with prefix
        docs/ (objects in the docs/ folder) to documents/, you can set a
        condition block with KeyPrefixEquals set to docs/ and in the Redirect
        set ReplaceKeyPrefixWith to /documents.

    :ivar http_redirect_code: The HTTP redirect code to use on the response.

    r1   r/   )ZReplaceKeyWithrI   )ZReplaceKeyPrefixWithrJ   )ZHttpRedirectCoderK   Nc                    s4   || _ || _|| _|| _|| _tt| | j d S r	   )	r0   r3   rI   rJ   rK   r4   r?   r   r5   rL   r6   r   r   r     s    zRedirect.__init__c                    s   t dtt|  S )Nr?   )r   r4   r?   r    r8   r6   r   r   r    "  s    zRedirect.to_xml)NNNNNr9   r   r   r6   r   r?      s       	r?   N)
r   objectr   r   r.   listr   r=   r>   r?   r   r   r   r   <module>   s   ?"4