service.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.exceptions import DataNotFoundError
  14. from kscore.docs.utils import get_official_service_name
  15. from kscore.docs.client import ClientDocumenter
  16. from kscore.docs.waiter import WaiterDocumenter
  17. from kscore.docs.paginator import PaginatorDocumenter
  18. from kscore.docs.bcdoc.restdoc import DocumentStructure
  19. class ServiceDocumenter(object):
  20. def __init__(self, service_name, session):
  21. self._session = session
  22. self._service_name = service_name
  23. self._client = self._session.create_client(
  24. service_name, region_name='us-east-1', ks_access_key_id='foo',
  25. ks_secret_access_key='bar')
  26. self.sections = [
  27. 'title',
  28. 'table-of-contents',
  29. 'client-api',
  30. 'paginator-api',
  31. 'waiter-api'
  32. ]
  33. def document_service(self):
  34. """Documents an entire service.
  35. :returns: The reStructured text of the documented service.
  36. """
  37. doc_structure = DocumentStructure(
  38. self._service_name, section_names=self.sections)
  39. self.title(doc_structure.get_section('title'))
  40. self.table_of_contents(doc_structure.get_section('table-of-contents'))
  41. self.client_api(doc_structure.get_section('client-api'))
  42. self.paginator_api(doc_structure.get_section('paginator-api'))
  43. self.waiter_api(doc_structure.get_section('waiter-api'))
  44. return doc_structure.flush_structure()
  45. def title(self, section):
  46. section.style.h1(self._client.__class__.__name__)
  47. def table_of_contents(self, section):
  48. section.style.table_of_contents(title='Table of Contents', depth=2)
  49. def client_api(self, section):
  50. examples = None
  51. try:
  52. examples = self.get_examples(self._service_name)
  53. except DataNotFoundError:
  54. pass
  55. ClientDocumenter(self._client, examples).document_client(section)
  56. def paginator_api(self, section):
  57. try:
  58. service_paginator_model = self._session.get_paginator_model(
  59. self._service_name)
  60. except DataNotFoundError:
  61. return
  62. paginator_documenter = PaginatorDocumenter(
  63. self._client, service_paginator_model)
  64. paginator_documenter.document_paginators(section)
  65. def waiter_api(self, section):
  66. if self._client.waiter_names:
  67. service_waiter_model = self._session.get_waiter_model(
  68. self._service_name)
  69. waiter_documenter = WaiterDocumenter(
  70. self._client, service_waiter_model)
  71. waiter_documenter.document_waiters(section)
  72. def get_examples(self, service_name, api_version=None):
  73. loader = self._session.get_component('data_loader')
  74. examples = loader.load_service_model(
  75. service_name, 'examples-1', api_version)
  76. return examples['examples']