shape.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. # NOTE: This class should not be instantiated and its
  14. # ``traverse_and_document_shape`` method called directly. It should be
  15. # inherited from a Documenter class with the appropriate methods
  16. # and attributes.
  17. class ShapeDocumenter(object):
  18. EVENT_NAME = ''
  19. def __init__(self, service_name, operation_name, event_emitter,
  20. context=None):
  21. self._service_name = service_name
  22. self._operation_name = operation_name
  23. self._event_emitter = event_emitter
  24. self._context = context
  25. if context is None:
  26. self._context = {
  27. 'special_shape_types': {}
  28. }
  29. def traverse_and_document_shape(self, section, shape, history,
  30. include=None, exclude=None, name=None,
  31. is_required=False):
  32. """Traverses and documents a shape
  33. Will take a self class and call its appropriate methods as a shape
  34. is traversed.
  35. :param section: The section to document.
  36. :param history: A list of the names of the shapes that have been
  37. traversed.
  38. :type include: Dictionary where keys are parameter names and
  39. values are the shapes of the parameter names.
  40. :param include: The parameter shapes to include in the documentation.
  41. :type exclude: List of the names of the parameters to exclude.
  42. :param exclude: The names of the parameters to exclude from
  43. documentation.
  44. :param name: The name of the shape.
  45. :param is_required: If the shape is a required member.
  46. """
  47. param_type = shape.type_name
  48. if shape.name in history:
  49. self.document_recursive_shape(section, shape, name=name)
  50. else:
  51. history.append(shape.name)
  52. is_top_level_param = (len(history) == 2)
  53. getattr(self, 'document_shape_type_%s' % param_type,
  54. self.document_shape_default)(
  55. section, shape, history=history, name=name,
  56. include=include, exclude=exclude,
  57. is_top_level_param=is_top_level_param,
  58. is_required=is_required)
  59. if is_top_level_param:
  60. self._event_emitter.emit(
  61. 'docs.%s.%s.%s.%s' % (self.EVENT_NAME,
  62. self._service_name,
  63. self._operation_name,
  64. name),
  65. section=section)
  66. at_overlying_method_section = (len(history) == 1)
  67. if at_overlying_method_section:
  68. self._event_emitter.emit(
  69. 'docs.%s.%s.%s.complete-section' % (self.EVENT_NAME,
  70. self._service_name,
  71. self._operation_name),
  72. section=section)
  73. history.pop()
  74. def _get_special_py_default(self, shape):
  75. special_defaults = {
  76. 'streaming_input_shape': 'b\'bytes\'|file',
  77. 'streaming_output_shape': 'StreamingBody()'
  78. }
  79. return self._get_value_for_special_type(shape, special_defaults)
  80. def _get_special_py_type_name(self, shape):
  81. special_type_names = {
  82. 'streaming_input_shape': 'bytes or seekable file-like object',
  83. 'streaming_output_shape': ':class:`.StreamingBody`'
  84. }
  85. return self._get_value_for_special_type(shape, special_type_names)
  86. def _get_value_for_special_type(self, shape, special_type_map):
  87. for special_type, marked_shape in self._context[
  88. 'special_shape_types'].items():
  89. if special_type in special_type_map:
  90. if shape == marked_shape:
  91. return special_type_map[special_type]
  92. return None