python-boto3/boto3/utils.py

101 lines
3.1 KiB
Python
Raw Normal View History

2015-11-27 23:25:33 +01:00
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
2021-09-22 18:34:33 +02:00
# https://aws.amazon.com/apache2.0/
2015-11-27 23:25:33 +01:00
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import sys
from collections import namedtuple
_ServiceContext = namedtuple(
'ServiceContext',
2022-05-26 01:13:54 +02:00
[
'service_name',
'service_model',
'service_waiter_model',
'resource_json_definitions',
],
2015-11-27 23:25:33 +01:00
)
class ServiceContext(_ServiceContext):
"""Provides important service-wide, read-only information about a service
:type service_name: str
:param service_name: The name of the service
:type service_model: :py:class:`botocore.model.ServiceModel`
:param service_model: The model of the service.
:type service_waiter_model: :py:class:`botocore.waiter.WaiterModel` or
a waiter model-like object such as
:py:class:`boto3.utils.LazyLoadedWaiterModel`
:param service_waiter_model: The waiter model of the service.
:type resource_json_definitions: dict
:param resource_json_definitions: The loaded json models of all resource
shapes for a service. It is equivalient of loading a
``resource-1.json`` and retrieving the value at the key "resources".
"""
2022-05-26 01:13:54 +02:00
2015-11-27 23:25:33 +01:00
pass
def import_module(name):
"""Import module given a name.
Does not support relative imports.
"""
__import__(name)
return sys.modules[name]
2016-05-22 04:03:29 +02:00
def lazy_call(full_name, **kwargs):
parent_kwargs = kwargs
2015-11-27 23:25:33 +01:00
def _handler(**kwargs):
module, function_name = full_name.rsplit('.', 1)
module = import_module(module)
2016-05-22 04:03:29 +02:00
kwargs.update(parent_kwargs)
2015-11-27 23:25:33 +01:00
return getattr(module, function_name)(**kwargs)
2016-05-22 04:03:29 +02:00
2015-11-27 23:25:33 +01:00
return _handler
def inject_attribute(class_attributes, name, value):
if name in class_attributes:
raise RuntimeError(
2022-05-26 01:13:54 +02:00
f'Cannot inject class attribute "{name}", attribute '
f'already exists in class dict.'
)
2015-11-27 23:25:33 +01:00
else:
class_attributes[name] = value
2022-05-26 01:13:54 +02:00
class LazyLoadedWaiterModel:
2015-11-27 23:25:33 +01:00
"""A lazily loaded waiter model
This does not load the service waiter model until an attempt is made
to retrieve the waiter model for a specific waiter. This is helpful
in docstring generation where we do not need to actually need to grab
the waiter-2.json until it is accessed through a ``get_waiter`` call
when the docstring is generated/accessed.
"""
2022-05-26 01:13:54 +02:00
2015-11-27 23:25:33 +01:00
def __init__(self, bc_session, service_name, api_version):
self._session = bc_session
self._service_name = service_name
self._api_version = api_version
def get_waiter(self, waiter_name):
return self._session.get_waiter_model(
2022-05-26 01:13:54 +02:00
self._service_name, self._api_version
).get_waiter(waiter_name)