Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/boto3/docs/service.py: 19%
95 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
1# Copyright 2015 Amazon.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# https://aws.amazon.com/apache2.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.
13import os
15from botocore.docs.bcdoc.restdoc import DocumentStructure
16from botocore.docs.service import ServiceDocumenter as BaseServiceDocumenter
17from botocore.exceptions import DataNotFoundError
19import boto3
20from boto3.docs.client import Boto3ClientDocumenter
21from boto3.docs.resource import ResourceDocumenter, ServiceResourceDocumenter
22from boto3.utils import ServiceContext
25class ServiceDocumenter(BaseServiceDocumenter):
26 # The path used to find examples
27 EXAMPLE_PATH = os.path.join(os.path.dirname(boto3.__file__), 'examples')
29 def __init__(self, service_name, session, root_docs_path):
30 super().__init__(
31 service_name=service_name,
32 # I know that this is an internal attribute, but the botocore session
33 # is needed to load the paginator and waiter models.
34 session=session._session,
35 root_docs_path=root_docs_path,
36 )
37 self._boto3_session = session
38 self._client = self._boto3_session.client(service_name)
39 self._service_resource = None
40 if self._service_name in self._boto3_session.get_available_resources():
41 self._service_resource = self._boto3_session.resource(service_name)
42 self.sections = [
43 'title',
44 'client',
45 'paginators',
46 'waiters',
47 'resources',
48 'examples',
49 'context-params',
50 ]
51 self._root_docs_path = root_docs_path
52 self._USER_GUIDE_LINK = (
53 'https://boto3.amazonaws.com/'
54 'v1/documentation/api/latest/guide/resources.html'
55 )
57 def document_service(self):
58 """Documents an entire service.
60 :returns: The reStructured text of the documented service.
61 """
62 doc_structure = DocumentStructure(
63 self._service_name, section_names=self.sections, target='html'
64 )
65 self.title(doc_structure.get_section('title'))
67 self.client_api(doc_structure.get_section('client'))
68 self.paginator_api(doc_structure.get_section('paginators'))
69 self.waiter_api(doc_structure.get_section('waiters'))
70 if self._service_resource:
71 self.resource_section(doc_structure.get_section('resources'))
72 self._document_examples(doc_structure.get_section('examples'))
73 context_params_section = doc_structure.get_section('context-params')
74 self.client_context_params(context_params_section)
75 return doc_structure.flush_structure()
77 def client_api(self, section):
78 examples = None
79 try:
80 examples = self.get_examples(self._service_name)
81 except DataNotFoundError:
82 pass
84 Boto3ClientDocumenter(
85 self._client, self._root_docs_path, examples
86 ).document_client(section)
88 def resource_section(self, section):
89 section.style.h2('Resources')
90 section.style.new_line()
91 section.write(
92 'Resources are available in boto3 via the '
93 '``resource`` method. For more detailed instructions '
94 'and examples on the usage of resources, see the '
95 'resources '
96 )
97 section.style.external_link(
98 title='user guide',
99 link=self._USER_GUIDE_LINK,
100 )
101 section.write('.')
102 section.style.new_line()
103 section.style.new_line()
104 section.write('The available resources are:')
105 section.style.new_line()
106 section.style.toctree()
107 self._document_service_resource(section)
108 self._document_resources(section)
110 def _document_service_resource(self, section):
111 # Create a new DocumentStructure for each Service Resource and add contents.
112 service_resource_doc = DocumentStructure(
113 'service-resource', target='html'
114 )
115 breadcrumb_section = service_resource_doc.add_new_section('breadcrumb')
116 breadcrumb_section.style.ref(
117 self._client.__class__.__name__, f'../../{self._service_name}'
118 )
119 breadcrumb_section.write(' / Resource / ServiceResource')
120 ServiceResourceDocumenter(
121 self._service_resource, self._session, self._root_docs_path
122 ).document_resource(service_resource_doc)
123 # Write collections in individual/nested files.
124 # Path: <root>/reference/services/<service>/<resource_name>/<collection_name>.rst
125 resource_name = self._service_resource.meta.resource_model.name
126 if resource_name == self._service_name:
127 resource_name = 'service-resource'
128 service_resource_dir_path = os.path.join(
129 self._root_docs_path,
130 f'{self._service_name}',
131 f'{resource_name.lower()}',
132 )
133 service_resource_doc.write_to_file(service_resource_dir_path, 'index')
134 section.style.tocitem(f'{self._service_name}/{resource_name}/index')
136 def _document_resources(self, section):
137 temp_identifier_value = 'foo'
138 loader = self._session.get_component('data_loader')
139 json_resource_model = loader.load_service_model(
140 self._service_name, 'resources-1'
141 )
142 service_model = self._service_resource.meta.client.meta.service_model
143 for resource_name in json_resource_model['resources']:
144 resource_model = json_resource_model['resources'][resource_name]
145 resource_cls = (
146 self._boto3_session.resource_factory.load_from_definition(
147 resource_name=resource_name,
148 single_resource_json_definition=resource_model,
149 service_context=ServiceContext(
150 service_name=self._service_name,
151 resource_json_definitions=json_resource_model[
152 'resources'
153 ],
154 service_model=service_model,
155 service_waiter_model=None,
156 ),
157 )
158 )
159 identifiers = resource_cls.meta.resource_model.identifiers
160 args = []
161 for _ in identifiers:
162 args.append(temp_identifier_value)
163 resource = resource_cls(*args, client=self._client)
164 # Create a new DocumentStructure for each Resource and add contents.
165 resource_name = resource.meta.resource_model.name.lower()
166 resource_doc = DocumentStructure(resource_name, target='html')
167 breadcrumb_section = resource_doc.add_new_section('breadcrumb')
168 breadcrumb_section.style.ref(
169 self._client.__class__.__name__, f'../../{self._service_name}'
170 )
171 breadcrumb_section.write(
172 f' / Resource / {resource.meta.resource_model.name}'
173 )
174 ResourceDocumenter(
175 resource, self._session, self._root_docs_path
176 ).document_resource(
177 resource_doc.add_new_section(resource.meta.resource_model.name)
178 )
179 # Write collections in individual/nested files.
180 # Path: <root>/reference/services/<service>/<resource_name>/<index>.rst
181 service_resource_dir_path = os.path.join(
182 self._root_docs_path,
183 f'{self._service_name}',
184 f'{resource_name}',
185 )
186 resource_doc.write_to_file(service_resource_dir_path, 'index')
187 section.style.tocitem(
188 f'{self._service_name}/{resource_name}/index'
189 )
191 def _get_example_file(self):
192 return os.path.realpath(
193 os.path.join(self.EXAMPLE_PATH, self._service_name + '.rst')
194 )
196 def _document_examples(self, section):
197 examples_file = self._get_example_file()
198 if os.path.isfile(examples_file):
199 section.style.h2('Examples')
200 section.style.new_line()
201 with open(examples_file) as f:
202 section.write(f.read())