paginator.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.compat import OrderedDict
  15. from kscore.docs.utils import DocumentedShape
  16. from kscore.utils import get_service_module_name
  17. from kscore.docs.method import document_model_driven_method
  18. class PaginatorDocumenter(object):
  19. def __init__(self, client, service_paginator_model):
  20. self._client = client
  21. self._service_name = self._client.meta.service_model.service_name
  22. self._service_paginator_model = service_paginator_model
  23. def document_paginators(self, section):
  24. """Documents the various paginators for a service
  25. param section: The section to write to.
  26. """
  27. section.style.h2('Paginators')
  28. section.style.new_line()
  29. section.writeln('The available paginators are:')
  30. paginator_names = sorted(
  31. self._service_paginator_model._paginator_config)
  32. # List the available paginators and then document each paginator.
  33. for paginator_name in paginator_names:
  34. section.style.li(
  35. ':py:class:`%s.Paginator.%s`' % (
  36. self._client.__class__.__name__, paginator_name))
  37. self._add_paginator(section, paginator_name)
  38. def _add_paginator(self, section, paginator_name):
  39. section = section.add_new_section(paginator_name)
  40. # Docment the paginator class
  41. section.style.start_sphinx_py_class(
  42. class_name='%s.Paginator.%s' % (
  43. self._client.__class__.__name__, paginator_name))
  44. section.style.start_codeblock()
  45. section.style.new_line()
  46. # Document how to instantiate the paginator.
  47. section.write(
  48. 'paginator = client.get_paginator(\'%s\')' % xform_name(
  49. paginator_name)
  50. )
  51. section.style.end_codeblock()
  52. section.style.new_line()
  53. # Get the pagination model for the particular paginator.
  54. paginator_config = self._service_paginator_model.get_paginator(
  55. paginator_name)
  56. document_paginate_method(
  57. section=section,
  58. paginator_name=paginator_name,
  59. event_emitter=self._client.meta.events,
  60. service_model=self._client.meta.service_model,
  61. paginator_config=paginator_config
  62. )
  63. def document_paginate_method(section, paginator_name, event_emitter,
  64. service_model, paginator_config,
  65. include_signature=True):
  66. """Documents the paginate method of a paginator
  67. :param section: The section to write to
  68. :param paginator_name: The name of the paginator. It is snake cased.
  69. :param event_emitter: The event emitter to use to emit events
  70. :param service_model: The service model
  71. :param paginator_config: The paginator config associated to a particular
  72. paginator.
  73. :param include_signature: Whether or not to include the signature.
  74. It is useful for generating docstrings.
  75. """
  76. # Retrieve the operation model of the underlying operation.
  77. operation_model = service_model.operation_model(
  78. paginator_name)
  79. # Add representations of the request and response parameters
  80. # we want to include in the description of the paginate method.
  81. # These are parameters we expose via the kscore interface.
  82. pagination_config_members = OrderedDict()
  83. pagination_config_members['MaxItems'] = DocumentedShape(
  84. name='MaxItems', type_name='integer',
  85. documentation=(
  86. '<p>The total number of items to return. If the total '
  87. 'number of items available is more than the value '
  88. 'specified in max-items then a <code>NextToken</code> '
  89. 'will be provided in the output that you can use to '
  90. 'resume pagination.</p>'))
  91. pagination_config_members['PageSize'] = DocumentedShape(
  92. name='PageSize', type_name='integer',
  93. documentation='<p>The size of each page.<p>')
  94. pagination_config_members['StartingToken'] = DocumentedShape(
  95. name='StartingToken', type_name='string',
  96. documentation=(
  97. '<p>A token to specify where to start paginating. '
  98. 'This is the <code>NextToken</code> from a previous '
  99. 'response.</p>'))
  100. kscore_pagination_params = [
  101. DocumentedShape(
  102. name='PaginationConfig', type_name='structure',
  103. documentation=(
  104. '<p>A dictionary that provides parameters to control '
  105. 'pagination.</p>'),
  106. members=pagination_config_members)
  107. ]
  108. kscore_pagination_response_params = [
  109. DocumentedShape(
  110. name='NextToken', type_name='string',
  111. documentation=(
  112. '<p>A token to resume pagination.</p>'))
  113. ]
  114. service_pagination_params = []
  115. # Add the normal input token of the method to a list
  116. # of input paramters that we wish to hide since we expose our own.
  117. if isinstance(paginator_config['input_token'], list):
  118. service_pagination_params += paginator_config['input_token']
  119. else:
  120. service_pagination_params.append(paginator_config['input_token'])
  121. # Hide the limit key in the documentation.
  122. if paginator_config.get('limit_key', None):
  123. service_pagination_params.append(paginator_config['limit_key'])
  124. # Hide the output tokens in the documentation.
  125. service_pagination_response_params = []
  126. if isinstance(paginator_config['output_token'], list):
  127. service_pagination_response_params += paginator_config[
  128. 'output_token']
  129. else:
  130. service_pagination_response_params.append(paginator_config[
  131. 'output_token'])
  132. paginate_description = (
  133. 'Creates an iterator that will paginate through responses '
  134. 'from :py:meth:`{0}.Client.{1}`.'.format(
  135. get_service_module_name(service_model), xform_name(paginator_name))
  136. )
  137. document_model_driven_method(
  138. section, 'paginate', operation_model,
  139. event_emitter=event_emitter,
  140. method_description=paginate_description,
  141. example_prefix='response_iterator = paginator.paginate',
  142. include_input=kscore_pagination_params,
  143. include_output=kscore_pagination_response_params,
  144. exclude_input=service_pagination_params,
  145. exclude_output=service_pagination_response_params,
  146. include_signature=include_signature
  147. )