Skip to content

Commit f3def5e

Browse files
authored
chore: add configured_snippet module (#1506)
* chore: add configured_snippet module
1 parent bfeced0 commit f3def5e

File tree

10 files changed

+2299
-4
lines changed

10 files changed

+2299
-4
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import dataclasses
16+
17+
import libcst
18+
19+
from gapic.configurable_snippetgen import snippet_config_language_pb2
20+
from gapic.schema import api
21+
22+
23+
def _make_empty_module() -> libcst.Module:
24+
return libcst.Module(body=[])
25+
26+
27+
@dataclasses.dataclass(frozen=True)
28+
class ConfiguredSnippet:
29+
api_schema: api.API
30+
config: snippet_config_language_pb2.SnippetConfig
31+
api_version: str
32+
is_sync: bool
33+
_module: libcst.Module = dataclasses.field(
34+
default_factory=_make_empty_module, init=False
35+
)
36+
37+
@property
38+
def code(self) -> str:
39+
"""The code of the configured snippet."""
40+
return self._module.code
41+
42+
@property
43+
def region_tag(self) -> str:
44+
"""The region tag of the snippet.
45+
46+
For example:
47+
"speech_v1_config_Adaptation_CreateCustomClass_Basic_async"
48+
"""
49+
module_name = self.config.rpc.proto_package.split(".")[-1]
50+
service_name = self.config.rpc.service_name
51+
rpc_name = self.config.rpc.rpc_name
52+
config_id = self.config.metadata.config_id
53+
sync_or_async = "sync" if self.is_sync else "async"
54+
return f"{module_name}_{self.api_version}_config_{service_name}_{rpc_name}_{config_id}_{sync_or_async}"
55+
56+
@property
57+
def sample_function_name(self) -> str:
58+
"""The sample function's name.
59+
60+
For example:
61+
"sample_create_custom_class_basic"
62+
"""
63+
snippet_method_name = self.config.signature.snippet_method_name
64+
config_id = self.config.metadata.config_id
65+
return f"sample_{snippet_method_name}_{config_id}"

0 commit comments

Comments
 (0)