o ckF[%@sddlZddlZddlmZddlmZddlmZddlmZiZ ddZ dd Z d d Z d d Z e fddZdddZ  dddZ  dddZ  dddZddZGdddeZdS)N)json)BotoClientError)BotoEndpointResolver)StaticEndpointBuildercCst|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)pathr 1/usr/lib/python3/dist-packages/boto/regioninfo.pyload_endpoint_json#s r cCs8t|d }t|WdS1swYdS)rrN)openrload)rendpoints_filer r r r/s $rcCs0|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 setdefaultupdate)defaults additionsservice region_infor r r merge_endpoints<s rcCsXt}d}tjdrtjd}ntjddrtjdd}|r*t|}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 NBOTO_ENDPOINTSBotoendpoints_path)_load_builtin_endpointsosenvirongetbotoconfigr r) endpointsadditional_path additionalr r r load_regionsUs   r$cCs8|r|Sttj}t|}t|}|}|||S)z1Loads the builtin endpoints in the legacy format.)rrENDPOINTS_PATHrrbuild_static_endpointsr)_cacher!resolverbuilderr r r rvs  rcCs\t}||vr td||durt}g}||iD]\}}|||||dq|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.Nnameendpointconnection_cls)r$r RegionInforrappend) service_name region_clsr-r! region_objs region_namer,r r r get_regionss"r4cKsR|durt}t||||}|durtrt||||}|dur!dS|jdi|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. Nr )r. _get_region_use_endpoint_heuristics_get_region_with_heuristicsconnect)r0r3r1r- kw_paramsregionr r r r8sr8cCs(t|||D] }|j|kr|SqdS)z8Finds the region by searching through the known regions.N)r4r+)r0r3r1r-r:r r r r5s  r5cCs,ttj}t|}|||}||||dS)z4Finds the region using known regions and heuristics.r*)r rr%rresolve_hostname)r0r3r1r-r!r(hostnamer r r r7s  r7cCs.tjdd}tjddd}|dkp|S)NBOTO_USE_ENDPOINT_HEURISTICSfalseruse_endpoint_heuristicsFtrue)rrrlowerrr getbool)env_var config_varr r r r6s r6c@s>eZdZdZ  d ddZddZddZd d Zd d ZdS)r.z" Represents an AWS Region NcCs||_||_||_||_dSN) connectionr+r,r-)selfrFr+r,r-r r r __init__s zRegionInfo.__init__cCs d|jS)Nz RegionInfo:%s)r+)rGr r r __repr__s zRegionInfo.__repr__cCsdSrEr )rGr+attrsrFr r r startElement szRegionInfo.startElementcCs4|dkr ||_dS|dkr||_dSt|||dS)N regionNameregionEndpoint)r+r,setattr)rGr+valuerFr r r endElements   zRegionInfo.endElementcKs|jr |jdd|i|SdS)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 r:Nr )r-)rGr9r r r r8s zRegionInfo.connect)NNNN) __name__ __module__ __qualname____doc__rHrIrKrPr8r r r r r.s  r.)NN)rr boto.compatrboto.exceptionrboto.endpointsrr_endpoints_cacher rrr$rr4r8r5r7r6objectr.r r r r s0       ! 0 &