def parse_service_resource( session: Session, service_name: ServiceName) -> Optional[ServiceResource]: try: service_resource = session.resource(service_name.boto3_name) except boto3.exceptions.ResourceNotExistsError: return None methods = list(parse_methods( get_instance_public_methods(service_resource))) attributes: List[Attribute] = [] for attribute in parse_attributes(service_resource): attributes.append(attribute) for attribute in parse_identifiers(service_resource): attributes.append(attribute) collections = list(parse_collections(service_resource)) sub_resources = [] for sub_resource in retrieve_sub_resources(session, service_name, service_resource): sub_resources.append(parse_resource(sub_resource)) return ServiceResource( service_name=service_name, docstring=clean_doc(getdoc(service_resource)), methods=methods, attributes=attributes, collections=collections, sub_resources=sub_resources, )
def parse_client(session: Session, service_name: ServiceName) -> Client: client = session.client(service_name.boto3_name) return Client( service_name=service_name, docstring=clean_doc(getdoc(client)), methods=list(parse_methods(get_instance_public_methods(client))), )
def generate_method( method: Method, method_body: str = "pass", first_arg: str = "self", decorator: str = None, include_doc: bool = False, ) -> Generator[str, None, None]: if decorator: yield decorator yield "# pylint: disable=arguments-differ" yield f"def {method.name}(" formatted_arguments = [first_arg] for formatted_argument in format_arguments(method): formatted_arguments.append(formatted_argument) for argument_index, formatted_argument in enumerate(formatted_arguments): comma = "," if argument_index == len(formatted_arguments) - 1: comma = "" yield f" {formatted_argument}{comma}" if method.return_type: yield f") -> {method.return_type.render()}:" else: yield "):" if include_doc: docstring = clean_doc(method.docstring) if docstring: yield ' """' for line in docstring.split("\n"): yield f" {line}" yield ' """' yield f" {method_body}"
def parse_waiters(client: BaseClient) -> Generator[Waiter, None, None]: for waiter_name in client.waiter_names: waiter = client.get_waiter(waiter_name) yield Waiter( name=waiter.name, docstring=clean_doc(getdoc(waiter)), methods=list(parse_methods(get_instance_public_methods(waiter))), )
def parse_methods(public_methods: Dict) -> Generator[Method, None, None]: for name, method in public_methods.items(): doc = getdoc(method) if doc is None: logger.debug(f"Docless method: {name}") yield manually_set_method(name) else: parsed_doc = parse(doc) yield Method( name=name, arguments=list(parse_arguments(parsed_doc)), docstring=clean_doc(doc), return_type=parse_return_type(parsed_doc.meta), )
def parse_paginators( client: BaseClient, service_paginator_model: PaginatorModel ) -> Generator[Paginator, None, None]: paginator_config = ( service_paginator_model._paginator_config # pylint: disable=protected-access ) for paginator_name in sorted(paginator_config): paginator = client.get_paginator(xform_name(paginator_name)) paginator_model = paginator._model # pylint: disable=protected-access yield Paginator( name=paginator_model.name, docstring=clean_doc(getdoc(paginator)), methods=list(parse_methods( get_instance_public_methods(paginator))), )
def parse_collections( resource: Boto3ServiceResource, ) -> Generator[Collection, None, None]: for collection in resource.meta.resource_model.collections: yield Collection( name=collection.name, docstring=clean_doc(getdoc(collection)), type=InternalImport( name=collection.name, service_name=ServiceName(resource.meta.service_name), ), methods=list( parse_methods( get_instance_public_methods( getattr(resource, collection.name)))), )
def parse_resource(resource: Boto3ServiceResource) -> Resource: name = resource.__class__.__name__.split(".", 1)[-1] methods = list( parse_methods(get_resource_public_actions(resource.__class__))) attributes: List[Attribute] = [] for attribute in parse_attributes(resource): attributes.append(attribute) for attribute in parse_identifiers(resource): attributes.append(attribute) collections = list(parse_collections(resource)) return Resource( name=name, docstring=clean_doc(getdoc(resource)), methods=methods, attributes=attributes, collections=collections, )