You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.0 KiB

4 years ago
  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. # http://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. _ServiceContext = namedtuple(
  16. 'ServiceContext',
  17. ['service_name', 'service_model', 'service_waiter_model',
  18. 'resource_json_definitions']
  19. )
  20. class ServiceContext(_ServiceContext):
  21. """Provides important service-wide, read-only information about a service
  22. :type service_name: str
  23. :param service_name: The name of the service
  24. :type service_model: :py:class:`botocore.model.ServiceModel`
  25. :param service_model: The model of the service.
  26. :type service_waiter_model: :py:class:`botocore.waiter.WaiterModel` or
  27. a waiter model-like object such as
  28. :py:class:`boto3.utils.LazyLoadedWaiterModel`
  29. :param service_waiter_model: The waiter model of the service.
  30. :type resource_json_definitions: dict
  31. :param resource_json_definitions: The loaded json models of all resource
  32. shapes for a service. It is equivalient of loading a
  33. ``resource-1.json`` and retrieving the value at the key "resources".
  34. """
  35. pass
  36. def import_module(name):
  37. """Import module given a name.
  38. Does not support relative imports.
  39. """
  40. __import__(name)
  41. return sys.modules[name]
  42. def lazy_call(full_name, **kwargs):
  43. parent_kwargs = kwargs
  44. def _handler(**kwargs):
  45. module, function_name = full_name.rsplit('.', 1)
  46. module = import_module(module)
  47. kwargs.update(parent_kwargs)
  48. return getattr(module, function_name)(**kwargs)
  49. return _handler
  50. def inject_attribute(class_attributes, name, value):
  51. if name in class_attributes:
  52. raise RuntimeError(
  53. 'Cannot inject class attribute "%s", attribute '
  54. 'already exists in class dict.' % name)
  55. else:
  56. class_attributes[name] = value
  57. class LazyLoadedWaiterModel(object):
  58. """A lazily loaded waiter model
  59. This does not load the service waiter model until an attempt is made
  60. to retrieve the waiter model for a specific waiter. This is helpful
  61. in docstring generation where we do not need to actually need to grab
  62. the waiter-2.json until it is accessed through a ``get_waiter`` call
  63. when the docstring is generated/accessed.
  64. """
  65. def __init__(self, bc_session, service_name, api_version):
  66. self._session = bc_session
  67. self._service_name = service_name
  68. self._api_version = api_version
  69. def get_waiter(self, waiter_name):
  70. return self._session.get_waiter_model(
  71. self._service_name, self._api_version).get_waiter(waiter_name)