Пример #1
0
    def get_response(self, api_schema: api.API,
                     opts: Options) -> CodeGeneratorResponse:
        """Return a :class:`~.CodeGeneratorResponse` for this library.

        This is a complete response to be written to (usually) stdout, and
        thus read by ``protoc``.

        Args:
            api_schema (~api.API): An API schema object.
            opts (~.options.Options): An options instance.

        Returns:
            ~.CodeGeneratorResponse: A response describing appropriate
            files and contents. See ``plugin.proto``.
        """
        output_files: Dict[str, CodeGeneratorResponse.File] = OrderedDict()
        sample_templates, client_templates = utils.partition(
            lambda fname: os.path.basename(fname) == samplegen.
            DEFAULT_TEMPLATE_NAME,
            self._env.loader.list_templates(),  # type: ignore
        )

        # We generate code snippets *before* the library code so snippets
        # can be inserted into method docstrings.
        snippet_idx = snippet_index.SnippetIndex(api_schema)
        if sample_templates:
            sample_output, snippet_idx = self._generate_samples_and_manifest(
                api_schema,
                snippet_idx,
                self._env.get_template(sample_templates[0]),
                opts=opts,
            )
            output_files.update(sample_output)

        # Iterate over each template and add the appropriate output files
        # based on that template.
        # Sample templates work differently: there's (usually) only one,
        # and instead of iterating over it/them, we iterate over samples
        # and plug those into the template.
        for template_name in client_templates:
            # Quick check: Skip "private" templates.
            filename = template_name.split("/")[-1]
            if filename.startswith("_") and filename != "__init__.py.j2":
                continue

            # Append to the output files dictionary.
            output_files.update(
                self._render_template(template_name,
                                      api_schema=api_schema,
                                      opts=opts,
                                      snippet_index=snippet_idx))

        # Return the CodeGeneratorResponse output.
        res = CodeGeneratorResponse(file=[i for i in output_files.values()
                                          ])  # type: ignore
        res.supported_features |= CodeGeneratorResponse.Feature.FEATURE_PROTO3_OPTIONAL  # type: ignore
        return res
Пример #2
0
def test_get_snippet_no_matching_rpc():
    index = snippet_index.SnippetIndex(api_schema=DummyApiSchema(
        services={
            "Squid":
            DummyService(name="Squid", methods={"classify": DummyMethod()})
        }))

    # No 'list' RPC in 'Squid' service
    with pytest.raises(types.RpcMethodNotFound):
        index.get_snippet(service_name="Squid", rpc_name="list")
Пример #3
0
def test_get_snippet_no_matching_service():
    index = snippet_index.SnippetIndex(api_schema=DummyApiSchema(
        services={
            "Squid":
            DummyService(name="Squid", methods={"classify": DummyMethod()})
        }))

    # No 'Clam' service in API Schema
    with pytest.raises(types.UnknownService):
        index.get_snippet(service_name="Clam", rpc_name="classify")
Пример #4
0
def test_add_snippet_no_matching_service(sample_str):
    snippet_metadata = snippet_metadata_pb2.Snippet()
    snippet_metadata.client_method.method.service.short_name = "Clam"
    snippet = snippet_index.Snippet(sample_str, snippet_metadata)

    # No 'Clam' service in API Schema
    index = snippet_index.SnippetIndex(api_schema=DummyApiSchema(
        services={"Squid": DummyService(name="Squid", methods={})}))
    with pytest.raises(types.UnknownService):
        index.add_snippet(snippet)
Пример #5
0
def test_add_snippet_no_matching_rpc(sample_str):
    snippet_metadata = snippet_metadata_pb2.Snippet()
    snippet_metadata.client_method.method.service.short_name = "Squid"
    snippet_metadata.client_method.short_name = "classify"
    snippet = snippet_index.Snippet(sample_str, snippet_metadata)

    # No 'classify' method in 'Squid' service
    index = snippet_index.SnippetIndex(api_schema=DummyApiSchema(
        services={"Squid": DummyService(name="Squid", methods={"list": None})
                  }))
    with pytest.raises(types.RpcMethodNotFound):
        index.add_snippet(snippet)
Пример #6
0
def test_get_snippet_no_matching_service():
    index = snippet_index.SnippetIndex(api_schema=DummyApiSchema(
        naming=DummyNaming(proto_package="google.mollusca",
                           warehouse_package_name="google-mollusca",
                           version="v1"),
        services={
            "Squid":
            DummyService(name="Squid", methods={"classify": DummyMethod()})
        }))

    # No 'Clam' service in API Schema
    with pytest.raises(types.UnknownService):
        index.get_snippet(service_name="Clam", rpc_name="classify")
Пример #7
0
def test_get_metadata_json(sample_str):
    snippet_metadata = snippet_metadata_pb2.Snippet()
    snippet_metadata.client_method.method.service.short_name = "Squid"
    snippet_metadata.client_method.method.short_name = "classify"
    snippet = snippet_index.Snippet(sample_str, snippet_metadata)

    index = snippet_index.SnippetIndex(api_schema=DummyApiSchema(
        services={
            "Squid":
            DummyService(name="Squid", methods={"classify": DummyMethod()})
        }))

    index.add_snippet(snippet)

    assert json.loads(index.get_metadata_json()) == {
        'snippets': [{
            'clientMethod': {
                'method': {
                    'shortName': 'classify',
                    'service': {
                        'shortName': 'Squid'
                    }
                }
            },
            'segments': [{
                'end': 28,
                'start': 2,
                'type': 'FULL'
            }, {
                'end': 28,
                'start': 2,
                'type': 'SHORT'
            }, {
                'end': 8,
                'start': 6,
                'type': 'CLIENT_INITIALIZATION'
            }, {
                'end': 22,
                'start': 9,
                'type': 'REQUEST_INITIALIZATION'
            }, {
                'end': 25,
                'start': 23,
                'type': 'REQUEST_EXECUTION'
            }, {
                'end': 29,
                'start': 26,
                'type': 'RESPONSE_HANDLING'
            }]
        }]
    }
Пример #8
0
def test_get_snippet_no_matching_rpc():
    index = snippet_index.SnippetIndex(api_schema=DummyApiSchema(
        services={
            "Squid":
            DummyService(name="Squid", methods={"classify": DummyMethod()})
        },
        naming=DummyNaming(proto_package="google.mollusca",
                           warehouse_package_name="google-mollusca",
                           version="v1"),
    ))

    # No 'list' RPC in 'Squid' service
    with pytest.raises(types.RpcMethodNotFound):
        index.get_snippet(service_name="Squid", rpc_name="list")
Пример #9
0
def test_add_snippet_no_matching_service(sample_str):
    snippet_metadata = snippet_metadata_pb2.Snippet()
    snippet_metadata.client_method.method.service.short_name = "Clam"
    snippet = snippet_index.Snippet(sample_str, snippet_metadata)

    # No 'Clam' service in API Schema
    index = snippet_index.SnippetIndex(api_schema=DummyApiSchema(
        services={"Squid": DummyService(name="Squid", methods={})},
        naming=DummyNaming(proto_package="google.mollusca",
                           warehouse_package_name="google-mollusca",
                           version="v1"),
    ))
    with pytest.raises(types.UnknownService):
        index.add_snippet(snippet)
Пример #10
0
def test_add_snippet_no_matching_rpc(sample_str):
    snippet_metadata = snippet_metadata_pb2.Snippet()
    snippet_metadata.client_method.method.service.short_name = "Squid"
    snippet_metadata.client_method.short_name = "classify"
    snippet = snippet_index.Snippet(sample_str, snippet_metadata)

    # No 'classify' method in 'Squid' service
    index = snippet_index.SnippetIndex(api_schema=DummyApiSchema(
        services={"Squid": DummyService(name="Squid", methods={"list": None})},
        naming=DummyNaming(proto_package="google.mollusca",
                           warehouse_package_name="google-mollusca",
                           version="v1"),
    ))
    with pytest.raises(types.RpcMethodNotFound):
        index.add_snippet(snippet)
Пример #11
0
def test_add_and_get_snippet_sync(sample_str):
    snippet_metadata = snippet_metadata_pb2.Snippet()
    snippet_metadata.client_method.method.service.short_name = "Squid"
    snippet_metadata.client_method.method.short_name = "classify"
    snippet = snippet_index.Snippet(sample_str, snippet_metadata)

    index = snippet_index.SnippetIndex(api_schema=DummyApiSchema(
        services={
            "Squid":
            DummyService(name="Squid", methods={"classify": DummyMethod()})
        }))

    index.add_snippet(snippet)

    index.get_snippet(service_name="Squid", rpc_name="classify")
Пример #12
0
def test_add_and_get_snippet_sync(sample_str):
    snippet_metadata = snippet_metadata_pb2.Snippet()
    snippet_metadata.client_method.method.service.short_name = "Squid"
    snippet_metadata.client_method.method.short_name = "classify"
    snippet = snippet_index.Snippet(sample_str, snippet_metadata)

    index = snippet_index.SnippetIndex(api_schema=DummyApiSchema(
        services={
            "Squid":
            DummyService(name="Squid", methods={"classify": DummyMethod()})
        },
        naming=DummyNaming(proto_package="google.mollusca",
                           warehouse_package_name="google-mollusca",
                           version="v1"),
    ))

    index.add_snippet(snippet)

    index.get_snippet(service_name="Squid", rpc_name="classify")
Пример #13
0
def test_get_metadata_json(sample_str):
    snippet_metadata = snippet_metadata_pb2.Snippet()
    snippet_metadata.client_method.method.service.short_name = "Squid"
    snippet_metadata.client_method.method.short_name = "classify"
    snippet = snippet_index.Snippet(sample_str, snippet_metadata)

    index = snippet_index.SnippetIndex(api_schema=DummyApiSchema(
        services={
            "Squid":
            DummyService(name="Squid", methods={"classify": DummyMethod()})
        },
        naming=DummyNaming(proto_package="google.mollusca",
                           warehouse_package_name="google-mollusca",
                           version="v1"),
    ))

    index.add_snippet(snippet)

    print(index.get_metadata_json())
    assert json.loads(index.get_metadata_json()) == {
        "clientLibrary": {
            "apis": [{
                "id": "google.mollusca",
                "version": "v1"
            }],
            "language": "PYTHON",
            "name": "google-mollusca"
        },
        "snippets": [{
            "clientMethod": {
                "method": {
                    "service": {
                        "shortName": "Squid"
                    },
                    "shortName": "classify"
                }
            },
            "segments": [{
                "end": 28,
                "start": 2,
                "type": "FULL"
            }, {
                "end": 28,
                "start": 2,
                "type": "SHORT"
            }, {
                "end": 8,
                "start": 6,
                "type": "CLIENT_INITIALIZATION"
            }, {
                "end": 22,
                "start": 9,
                "type": "REQUEST_INITIALIZATION"
            }, {
                "end": 25,
                "start": 23,
                "type": "REQUEST_EXECUTION"
            }, {
                "end": 29,
                "start": 26,
                "type": "RESPONSE_HANDLING"
            }]
        }]
    }