| 1 |
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 |
# may not use this file except in compliance with the License. A copy of |
| 5 |
# the License is located at |
| 6 |
# |
| 7 |
# https://aws.amazon.com/apache2.0/ |
| 8 |
# |
| 9 |
# or in the "license" file accompanying this file. This file is |
| 10 |
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 |
# ANY KIND, either express or implied. See the License for the specific |
| 12 |
# language governing permissions and limitations under the License. |
| 13 |
import sys |
| 14 |
from collections import namedtuple |
| 15 |
|
| 16 |
_ServiceContext = namedtuple( |
| 17 |
'ServiceContext', |
| 18 |
[ |
| 19 |
'service_name', |
| 20 |
'service_model', |
| 21 |
'service_waiter_model', |
| 22 |
'resource_json_definitions', |
| 23 |
], |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
class ServiceContext(_ServiceContext): |
| 28 |
"""Provides important service-wide, read-only information about a service |
| 29 |
|
| 30 |
:type service_name: str |
| 31 |
:param service_name: The name of the service |
| 32 |
|
| 33 |
:type service_model: :py:class:`botocore.model.ServiceModel` |
| 34 |
:param service_model: The model of the service. |
| 35 |
|
| 36 |
:type service_waiter_model: :py:class:`botocore.waiter.WaiterModel` or |
| 37 |
a waiter model-like object such as |
| 38 |
:py:class:`boto3.utils.LazyLoadedWaiterModel` |
| 39 |
:param service_waiter_model: The waiter model of the service. |
| 40 |
|
| 41 |
:type resource_json_definitions: dict |
| 42 |
:param resource_json_definitions: The loaded json models of all resource |
| 43 |
shapes for a service. It is equivalient of loading a |
| 44 |
``resource-1.json`` and retrieving the value at the key "resources". |
| 45 |
""" |
| 46 |
|
| 47 |
pass |
| 48 |
|
| 49 |
|
| 50 |
def import_module(name): |
| 51 |
"""Import module given a name. |
| 52 |
|
| 53 |
Does not support relative imports. |
| 54 |
|
| 55 |
""" |
| 56 |
__import__(name) |
| 57 |
return sys.modules[name] |
| 58 |
|
| 59 |
|
| 60 |
def lazy_call(full_name, **kwargs): |
| 61 |
parent_kwargs = kwargs |
| 62 |
|
| 63 |
def _handler(**kwargs): |
| 64 |
module, function_name = full_name.rsplit('.', 1) |
| 65 |
module = import_module(module) |
| 66 |
kwargs.update(parent_kwargs) |
| 67 |
return getattr(module, function_name)(**kwargs) |
| 68 |
|
| 69 |
return _handler |
| 70 |
|
| 71 |
|
| 72 |
def inject_attribute(class_attributes, name, value): |
| 73 |
if name in class_attributes: |
| 74 |
raise RuntimeError( |
| 75 |
f'Cannot inject class attribute "{name}", attribute ' |
| 76 |
f'already exists in class dict.' |
| 77 |
) |
| 78 |
else: |
| 79 |
class_attributes[name] = value |
| 80 |
|
| 81 |
|
| 82 |
class LazyLoadedWaiterModel: |
| 83 |
"""A lazily loaded waiter model |
| 84 |
|
| 85 |
This does not load the service waiter model until an attempt is made |
| 86 |
to retrieve the waiter model for a specific waiter. This is helpful |
| 87 |
in docstring generation where we do not need to actually need to grab |
| 88 |
the waiter-2.json until it is accessed through a ``get_waiter`` call |
| 89 |
when the docstring is generated/accessed. |
| 90 |
""" |
| 91 |
|
| 92 |
def __init__(self, bc_session, service_name, api_version): |
| 93 |
self._session = bc_session |
| 94 |
self._service_name = service_name |
| 95 |
self._api_version = api_version |
| 96 |
|
| 97 |
def get_waiter(self, waiter_name): |
| 98 |
return self._session.get_waiter_model( |
| 99 |
self._service_name, self._api_version |
| 100 |
).get_waiter(waiter_name) |