U
    3d%                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlmZ i Z	dd Z
dd	 Zd
d Zdd Ze	fddZdddZdddZdddZdddZdd ZG dd deZdS )     N)json)BotoClientError)BotoEndpointResolver)StaticEndpointBuilderc                 C   s   t | S )
    Loads a given JSON file & returns it.

    :param path: The path to the JSON file
    :type path: string

    :returns: The loaded data
    )_load_json_file)path r	   3/tmp/pip-unpacked-wheel-d7dsrkjd/boto/regioninfo.pyload_endpoint_json#   s    	r   c              
   C   s,   t | d}t|W  5 Q R  S Q R X dS )r   rN)openr   load)r   Zendpoints_filer	   r	   r
   r   /   s    	r   c                 C   s0   |  D ]"\}}| |i  | | | q| S )aW  
    Given an existing set of endpoint data, this will deep-update it with
    any similarly structured data in the additions.

    :param defaults: The existing endpoints data
    :type defaults: dict

    :param defaults: The additional endpoints data
    :type defaults: dict

    :returns: The modified endpoints data
    :rtype: dict
    )items
setdefaultupdate)defaultsZ	additionsZserviceZregion_infor	   r	   r
   merge_endpoints<   s    r   c                  C   sX   t  } d}tjdr"tjd }ntjddr>tjdd}|rTt|}t| |} | S )a  
    Actually load the region/endpoint information from the JSON files.

    By default, this loads from the default included ``boto/endpoints.json``
    file.

    Users can override/extend this by supplying either a ``BOTO_ENDPOINTS``
    environment variable or a ``endpoints_path`` config variable, either of
    which should be an absolute path to the user's JSON file.

    :returns: The endpoints data
    :rtype: dict
    NZBOTO_ENDPOINTSBotoZendpoints_path)_load_builtin_endpointsosenvirongetbotoconfigr   r   )	endpointsZadditional_path
additionalr	   r	   r
   load_regionsU   s    
r   c                 C   s8   | r| S t tj}t|}t|}| }| | | S )z1Loads the builtin endpoints in the legacy format.)r   r   ENDPOINTS_PATHr   r   Zbuild_static_endpointsr   )_cacher   resolverZbuilderr	   r	   r
   r   v   s    

r   c                 C   s\   t  }| |krtd|  |dkr&t}g }|| i  D ]\}}|||||d q:|S )ae  
    Given a service name (like ``ec2``), returns a list of ``RegionInfo``
    objects for that service.

    This leverages the ``endpoints.json`` file (+ optional user overrides) to
    configure/construct all the objects.

    :param service_name: The name of the service to construct the ``RegionInfo``
        objects for. Ex: ``ec2``, ``s3``, ``sns``, etc.
    :type service_name: string

    :param region_cls: (Optional) The class to use when constructing. By
        default, this is ``RegionInfo``.
    :type region_cls: class

    :param connection_cls: (Optional) The connection class for the
        ``RegionInfo`` object. Providing this allows the ``connect`` method on
        the ``RegionInfo`` to work. Default is ``None`` (no connection).
    :type connection_cls: class

    :returns: A list of configured ``RegionInfo`` objects
    :rtype: list
    z$Service '%s' not found in endpoints.Nnameendpointconnection_cls)r   r   
RegionInfor   r   append)service_name
region_clsr$   r   Zregion_objsregion_namer#   r	   r	   r
   get_regions   s"    r*   c                 K   sN   |dkrt }t| |||}|dkr6t r6t| |||}|dkrBdS |jf |S )a(  Create a connection class for a given service in a given region.

    :param service_name: The name of the service to construct the
        ``RegionInfo`` object for, e.g. ``ec2``, ``s3``, etc.
    :type service_name: str

    :param region_name: The name of the region to connect to, e.g.
        ``us-west-2``, ``eu-central-1``, etc.
    :type region_name: str

    :param region_cls: (Optional) The class to use when constructing. By
        default, this is ``RegionInfo``.
    :type region_cls: class

    :param connection_cls: (Optional) The connection class for the
        ``RegionInfo`` object. Providing this allows the ``connect`` method on
        the ``RegionInfo`` to work. Default is ``None`` (no connection).
    :type connection_cls: class

    :returns: A configured connection class.
    N)r%   _get_region_use_endpoint_heuristics_get_region_with_heuristicsconnect)r'   r)   r(   r$   	kw_paramsregionr	   r	   r
   r.      s       r.   c                 C   s(   t | ||D ]}|j|kr|  S qdS )z8Finds the region by searching through the known regions.N)r*   r"   )r'   r)   r(   r$   r0   r	   r	   r
   r+      s    

r+   c                 C   s,   t tj}t|}|| |}||||dS )z4Finds the region using known regions and heuristics.r!   )r   r   r   r   Zresolve_hostname)r'   r)   r(   r$   r   r    hostnamer	   r	   r
   r-      s    
r-   c                  C   s.   t jdd } tjddd}| dkp,|S )NZBOTO_USE_ENDPOINT_HEURISTICSfalser   Zuse_endpoint_heuristicsFtrue)r   r   r   lowerr   r   Zgetbool)env_varZ
config_varr	   r	   r
   r,      s    r,   c                   @   s:   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdS )r%   z"
    Represents an AWS Region
    Nc                 C   s   || _ || _|| _|| _d S N)
connectionr"   r#   r$   )selfr7   r"   r#   r$   r	   r	   r
   __init__  s    zRegionInfo.__init__c                 C   s
   d| j  S )NzRegionInfo:%s)r"   )r8   r	   r	   r
   __repr__  s    zRegionInfo.__repr__c                 C   s   d S r6   r	   )r8   r"   attrsr7   r	   r	   r
   startElement  s    zRegionInfo.startElementc                 C   s0   |dkr|| _ n|dkr || _nt| || d S )NZ
regionNameZregionEndpoint)r"   r#   setattr)r8   r"   valuer7   r	   r	   r
   
endElement  s
    zRegionInfo.endElementc                 K   s   | j r| j f d| i|S dS )a  
        Connect to this Region's endpoint. Returns an connection
        object pointing to the endpoint associated with this region.
        You may pass any of the arguments accepted by the connection
        class's constructor as keyword arguments and they will be
        passed along to the connection object.

        :rtype: Connection object
        :return: The connection to this regions endpoint
        r0   N)r$   )r8   r/   r	   r	   r
   r.     s    zRegionInfo.connect)NNNN)	__name__
__module____qualname____doc__r9   r:   r<   r?   r.   r	   r	   r	   r
   r%      s     
r%   )NN)NN)NN)NN)r   r   Zboto.compatr   Zboto.exceptionr   Zboto.endpointsr   r   Z_endpoints_cacher   r   r   r   r   r*   r.   r+   r-   r,   objectr%   r	   r	   r	   r
   <module>   s.   !
0  
&  
	  
