params.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.docs.shape import ShapeDocumenter
  14. from kscore.docs.utils import py_type_name
  15. class BaseParamsDocumenter(ShapeDocumenter):
  16. def document_params(self, section, shape, include=None, exclude=None):
  17. """Fills out the documentation for a section given a model shape.
  18. :param section: The section to write the documentation to.
  19. :param shape: The shape of the operation.
  20. :type include: Dictionary where keys are parameter names and
  21. values are the shapes of the parameter names.
  22. :param include: The parameter shapes to include in the documentation.
  23. :type exclude: List of the names of the parameters to exclude.
  24. :param exclude: The names of the parameters to exclude from
  25. documentation.
  26. """
  27. history = []
  28. self.traverse_and_document_shape(
  29. section=section, shape=shape, history=history,
  30. name=None, include=include, exclude=exclude)
  31. def document_recursive_shape(self, section, shape, **kwargs):
  32. self._add_member_documentation(section, shape, **kwargs)
  33. def document_shape_default(self, section, shape, history, include=None,
  34. exclude=None, **kwargs):
  35. self._add_member_documentation(section, shape, **kwargs)
  36. def document_shape_type_list(self, section, shape, history, include=None,
  37. exclude=None, **kwargs):
  38. self._add_member_documentation(section, shape, **kwargs)
  39. param_shape = shape.member
  40. param_section = section.add_new_section(
  41. param_shape.name, context={'shape': shape.member.name})
  42. self._start_nested_param(param_section)
  43. self.traverse_and_document_shape(
  44. section=param_section, shape=param_shape,
  45. history=history, name=None)
  46. section = section.add_new_section('end-list')
  47. self._end_nested_param(section)
  48. def document_shape_type_map(self, section, shape, history, include=None,
  49. exclude=None, **kwargs):
  50. self._add_member_documentation(section, shape, **kwargs)
  51. key_section = section.add_new_section(
  52. 'key', context={'shape': shape.key.name})
  53. self._start_nested_param(key_section)
  54. self._add_member_documentation(key_section, shape.key)
  55. param_section = section.add_new_section(
  56. shape.value.name, context={'shape': shape.value.name})
  57. param_section.style.indent()
  58. self._start_nested_param(param_section)
  59. self.traverse_and_document_shape(
  60. section=param_section, shape=shape.value,
  61. history=history, name=None)
  62. end_section = section.add_new_section('end-map')
  63. self._end_nested_param(end_section)
  64. self._end_nested_param(end_section)
  65. def document_shape_type_structure(self, section, shape, history,
  66. include=None, exclude=None,
  67. name=None, **kwargs):
  68. members = self._add_members_to_shape(shape.members, include)
  69. self._add_member_documentation(section, shape, name=name)
  70. for param in members:
  71. if exclude and param in exclude:
  72. continue
  73. param_shape = members[param]
  74. param_section = section.add_new_section(
  75. param, context={'shape': param_shape.name})
  76. self._start_nested_param(param_section)
  77. self.traverse_and_document_shape(
  78. section=param_section, shape=param_shape,
  79. history=history, name=param)
  80. section = section.add_new_section('end-structure')
  81. self._end_nested_param(section)
  82. def _add_member_documentation(self, section, shape, **kwargs):
  83. pass
  84. def _add_members_to_shape(self, members, include):
  85. if include:
  86. members = members.copy()
  87. for param in include:
  88. members[param.name] = param
  89. return members
  90. def _document_non_top_level_param_type(self, type_section, shape):
  91. special_py_type = self._get_special_py_type_name(shape)
  92. py_type = py_type_name(shape.type_name)
  93. type_format = '(%s) -- '
  94. if special_py_type is not None:
  95. # Special type can reference a linked class.
  96. # Italicizing it blows away the link.
  97. type_section.write(type_format % special_py_type)
  98. else:
  99. type_section.style.italics(type_format % py_type)
  100. def _start_nested_param(self, section):
  101. section.style.indent()
  102. section.style.new_line()
  103. def _end_nested_param(self, section):
  104. section.style.dedent()
  105. section.style.new_line()
  106. class ResponseParamsDocumenter(BaseParamsDocumenter):
  107. """Generates the description for the response parameters"""
  108. EVENT_NAME = 'response-params'
  109. def _add_member_documentation(self, section, shape, name=None, **kwargs):
  110. name_section = section.add_new_section('param-name')
  111. name_section.write('- ')
  112. if name is not None:
  113. name_section.style.bold('%s ' % name)
  114. type_section = section.add_new_section('param-type')
  115. self._document_non_top_level_param_type(type_section, shape)
  116. documentation_section = section.add_new_section('param-documentation')
  117. if shape.documentation:
  118. documentation_section.style.indent()
  119. documentation_section.include_doc_string(shape.documentation)
  120. section.style.new_paragraph()
  121. class RequestParamsDocumenter(BaseParamsDocumenter):
  122. """Generates the description for the request parameters"""
  123. EVENT_NAME = 'request-params'
  124. def document_shape_type_structure(self, section, shape, history,
  125. include=None, exclude=None, **kwargs):
  126. if len(history) > 1:
  127. self._add_member_documentation(section, shape, **kwargs)
  128. section.style.indent()
  129. members = self._add_members_to_shape(shape.members, include)
  130. for i, param in enumerate(members):
  131. if exclude and param in exclude:
  132. continue
  133. param_shape = members[param]
  134. param_section = section.add_new_section(
  135. param, context={'shape': param_shape.name})
  136. param_section.style.new_line()
  137. is_required = param in shape.required_members
  138. self.traverse_and_document_shape(
  139. section=param_section, shape=param_shape,
  140. history=history, name=param, is_required=is_required)
  141. section = section.add_new_section('end-structure')
  142. if len(history) > 1:
  143. section.style.dedent()
  144. section.style.new_line()
  145. def _add_member_documentation(self, section, shape, name=None,
  146. is_top_level_param=False, is_required=False,
  147. **kwargs):
  148. py_type = self._get_special_py_type_name(shape)
  149. if py_type is None:
  150. py_type = py_type_name(shape.type_name)
  151. if is_top_level_param:
  152. type_section = section.add_new_section('param-type')
  153. type_section.write(':type %s: %s' % (name, py_type))
  154. end_type_section = type_section.add_new_section('end-param-type')
  155. end_type_section.style.new_line()
  156. name_section = section.add_new_section('param-name')
  157. name_section.write(':param %s: ' % name)
  158. else:
  159. name_section = section.add_new_section('param-name')
  160. name_section.write('- ')
  161. if name is not None:
  162. name_section.style.bold('%s ' % name)
  163. type_section = section.add_new_section('param-type')
  164. self._document_non_top_level_param_type(type_section, shape)
  165. if is_required:
  166. is_required_section = section.add_new_section('is-required')
  167. is_required_section.style.indent()
  168. is_required_section.style.bold('[REQUIRED] ')
  169. if shape.documentation:
  170. documentation_section = section.add_new_section(
  171. 'param-documentation')
  172. documentation_section.style.indent()
  173. documentation_section.include_doc_string(shape.documentation)
  174. end_param_section = section.add_new_section('end-param')
  175. end_param_section.style.new_paragraph()