def parse_fake_service_package(
        session: Session, service_name: ServiceName,
        package_data: type[BasePackageData]) -> ServicePackage:
    """
    Create fake boto3 service module structure.

    Used by stubs and master package.

    Arguments:
        session -- boto3 session.
        service_name -- Target service name.
        package_data -- Package data.

    Returns:
        ServiceModule structure.
    """
    shape_parser = ShapeParser(session, service_name)
    boto3_client = get_boto3_client(session, service_name)
    boto3_resource = get_boto3_resource(session, service_name)

    result = ServicePackage(
        data=package_data,
        service_name=service_name,
        client=Client(
            name=Client.get_class_name(service_name),
            service_name=service_name,
            boto3_client=boto3_client,
        ),
    )

    if boto3_resource is not None:
        result.service_resource = ServiceResource(
            name=ServiceResource.get_class_name(service_name),
            service_name=service_name,
            boto3_service_resource=boto3_resource,
        )

    waiter_names: list[str] = boto3_client.waiter_names
    for waiter_name in waiter_names:
        real_class_name = get_class_prefix(waiter_name)
        waiter_class_name = f"{real_class_name}Waiter"
        result.waiters.append(
            Waiter(
                waiter_class_name,
                waiter_name=waiter_name,
                service_name=service_name,
            ))

    for paginator_name in shape_parser.get_paginator_names():
        operation_name = xform_name(paginator_name)
        result.paginators.append(
            Paginator(
                f"{paginator_name}Paginator",
                operation_name=operation_name,
                service_name=service_name,
                paginator_name=paginator_name,
            ))

    return result
Пример #2
0
def parse_client(session: Session, service_name: ServiceName, shape_parser: ShapeParser) -> Client:
    """
    Parse boto3 client to a structure.

    Arguments:
        session -- boto3 session.
        service_name -- Target service name.

    Returns:
        Client structure.
    """
    client = get_boto3_client(session, service_name)
    public_methods = get_public_methods(client)

    # remove methods that will be overriden
    if "get_paginator" in public_methods:
        del public_methods["get_paginator"]
    if "get_waiter" in public_methods:
        del public_methods["get_waiter"]

    result = Client(
        name=Client.get_class_name(service_name),
        service_name=service_name,
        boto3_client=client,
    )

    shape_method_map = shape_parser.get_client_method_map()
    result.methods.append(result.get_exceptions_property())
    for method_name, public_method in public_methods.items():
        if method_name in shape_method_map:
            method = shape_method_map[method_name]
        else:
            method = parse_method("Client", method_name, public_method, service_name)
        docstring = get_short_docstring(inspect.getdoc(public_method) or "")
        method.docstring = docstring
        result.methods.append(method)

    service_model = client.meta.service_model
    client_exceptions = ClientExceptionsFactory().create_client_exceptions(service_model)
    for exception_class_name in dir(client_exceptions):
        if exception_class_name.startswith("_"):
            continue
        if not exception_class_name[0].isupper():
            continue
        result.exceptions_class.attributes.append(
            Attribute(
                exception_class_name,
                TypeSubscript(
                    Type.Type,
                    [InternalImport("BotocoreClientError", stringify=False)],
                ),
            )
        )

    result.attributes.append(Attribute("meta", TypeClass(ClientMeta)))

    return result
Пример #3
0
 def _get_client_import(self, service_name: ServiceName) -> ExternalImport:
     client_import = ExternalImport(
         source=ImportString.parent() +
         ImportString(ServiceModuleName.client.value),
         name=Client.get_class_name(service_name),
     )
     client_import.import_record = InternalImportRecord(
         ServiceModuleName.client, client_import.name)
     return client_import
 def service_package(self) -> ServicePackage:
     service_name = ServiceName("service", "Service")
     return ServicePackage(
         Boto3StubsPackageData,
         service_name=service_name,
         client=Client("Client", service_name, "base"),
         service_resource=ServiceResource("ServiceResource", service_name,
                                          "base"),
         waiters=[Waiter("waiter", "waiter", service_name)],
         paginators=[
             Paginator("Paginator", "Paginator", "paginate", service_name)
         ],
         typed_dicts=[TypeTypedDict("MyTypedDict", [])],
         literals=[TypeLiteral("MyLiteral", ["value"])],
         helper_functions=["helper_function"],
     )
Пример #5
0
def parse_fake_service_package(session: Session,
                               service_name: ServiceName) -> ServicePackage:
    """
    Create fake boto3 service module structure.

    Used by stubs and master package.

    Arguments:
        session -- boto3 session.
        service_name -- Target service name.

    Returns:
        ServiceModule structure.
    """
    shape_parser = ShapeParser(session, service_name)

    result = ServicePackage(
        name=service_name.module_name,
        pypi_name=service_name.pypi_name,
        service_name=service_name,
        client=Client(),
    )

    boto3_client = get_boto3_client(session, service_name)
    boto3_resource = get_boto3_resource(session, service_name)

    if boto3_resource is not None:
        result.service_resource = ServiceResource()

    for waiter_name in boto3_client.waiter_names:
        real_class_name = get_class_prefix(waiter_name)
        waiter_class_name = f"{real_class_name}Waiter"
        result.waiters.append(
            Waiter(waiter_class_name, waiter_name=waiter_name))

    for paginator_name in shape_parser.get_paginator_names():
        operation_name = xform_name(paginator_name)
        result.paginators.append(
            Paginator(f"{paginator_name}Paginator",
                      operation_name=operation_name))

    return result
Пример #6
0
def parse_client(session: Session, service_name: ServiceName,
                 shape_parser: ShapeParser) -> Client:
    """
    Parse boto3 client to a structure.

    Arguments:
        session -- boto3 session.
        service_name -- Target service name.

    Returns:
        Client structure.
    """
    client = get_boto3_client(session, service_name)
    public_methods = get_public_methods(client)

    # remove methods that will be overriden
    if "get_paginator" in public_methods:
        del public_methods["get_paginator"]
    if "get_waiter" in public_methods:
        del public_methods["get_waiter"]

    result = Client(
        name=f"{service_name.class_name}Client",
        service_name=service_name,
        boto3_client=client,
        docstring=(f"[{service_name.class_name}.Client documentation]"
                   f"({service_name.doc_link}.Client)"),
    )

    shape_method_map = shape_parser.get_client_method_map()
    for method_name, public_method in public_methods.items():
        if method_name in shape_method_map:
            method = shape_method_map[method_name]
        else:
            method = parse_method("Client", method_name, public_method,
                                  service_name)
        method.docstring = (f"[Client.{method_name} documentation]"
                            f"({service_name.doc_link}.Client.{method_name})")
        result.methods.append(method)

    service_model = client.meta.service_model
    client_exceptions = ClientExceptionsFactory().create_client_exceptions(
        service_model)
    for exception_class_name in dir(client_exceptions):
        if exception_class_name.startswith("_"):
            continue
        if not exception_class_name[0].isupper():
            continue
        result.exceptions_class.attributes.append(
            Attribute(
                exception_class_name,
                TypeSubscript(
                    Type.Type,
                    [TypeClass(ClientError, alias="Boto3ClientError")]),
            ))

    result.attributes.append(
        Attribute(
            "exceptions",
            InternalImport(
                name=result.exceptions_class.name,
                module_name=ServiceModuleName.client,
                service_name=service_name,
                stringify=False,
            ),
        ))
    return result