waiter.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright 2015 ksyun.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://www.apache.org/licenses/LICENSE-2.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. from kscore import xform_name
  14. from kscore.utils import get_service_module_name
  15. from kscore.docs.method import document_model_driven_method
  16. class WaiterDocumenter(object):
  17. def __init__(self, client, service_waiter_model):
  18. self._client = client
  19. self._service_name = self._client.meta.service_model.service_name
  20. self._service_waiter_model = service_waiter_model
  21. def document_waiters(self, section):
  22. """Documents the various waiters for a service.
  23. :param section: The section to write to.
  24. """
  25. section.style.h2('Waiters')
  26. section.style.new_line()
  27. section.writeln('The available waiters are:')
  28. for waiter_name in self._service_waiter_model.waiter_names:
  29. section.style.li(
  30. ':py:class:`%s.Waiter.%s`' % (
  31. self._client.__class__.__name__, waiter_name))
  32. self._add_single_waiter(section, waiter_name)
  33. def _add_single_waiter(self, section, waiter_name):
  34. section = section.add_new_section(waiter_name)
  35. section.style.start_sphinx_py_class(
  36. class_name='%s.Waiter.%s' % (
  37. self._client.__class__.__name__, waiter_name))
  38. # Add example on how to instantiate waiter.
  39. section.style.start_codeblock()
  40. section.style.new_line()
  41. section.write(
  42. 'waiter = client.get_waiter(\'%s\')' % xform_name(waiter_name)
  43. )
  44. section.style.end_codeblock()
  45. # Add information on the wait() method
  46. section.style.new_line()
  47. document_wait_method(
  48. section=section,
  49. waiter_name=waiter_name,
  50. event_emitter=self._client.meta.events,
  51. service_model=self._client.meta.service_model,
  52. service_waiter_model=self._service_waiter_model
  53. )
  54. def document_wait_method(section, waiter_name, event_emitter,
  55. service_model, service_waiter_model,
  56. include_signature=True):
  57. """Documents a the wait method of a waiter
  58. :param section: The section to write to
  59. :param waiter_name: The name of the waiter
  60. :param event_emitter: The event emitter to use to emit events
  61. :param service_model: The service model
  62. :param service_waiter_model: The waiter model associated to the service
  63. :param include_signature: Whether or not to include the signature.
  64. It is useful for generating docstrings.
  65. """
  66. waiter_model = service_waiter_model.get_waiter(waiter_name)
  67. operation_model = service_model.operation_model(
  68. waiter_model.operation)
  69. wait_description = (
  70. 'Polls :py:meth:`{0}.Client.{1}` every {2} '
  71. 'seconds until a successful state is reached. An error is '
  72. 'returned after {3} failed checks.'.format(
  73. get_service_module_name(service_model),
  74. xform_name(waiter_model.operation),
  75. waiter_model.delay, waiter_model.max_attempts)
  76. )
  77. document_model_driven_method(
  78. section, 'wait', operation_model,
  79. event_emitter=event_emitter,
  80. method_description=wait_description,
  81. example_prefix='waiter.wait',
  82. document_output=False,
  83. include_signature=include_signature
  84. )