Skip to content

Commit ff43b9d

Browse files
busunkim96tseaver
andauthored
fix(snippetgen): use f-strings in print statements (#975)
Co-authored-by: Tres Seaver <[email protected]>
1 parent 6ac3be6 commit ff43b9d

File tree

123 files changed

+165
-132
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+165
-132
lines changed

packages/gapic-generator/DEVELOPMENT.md

Lines changed: 6 additions & 0 deletions

packages/gapic-generator/gapic/generator/generator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from typing import Any, DefaultDict, Dict, Mapping
2222
from hashlib import sha256
2323
from collections import OrderedDict, defaultdict
24-
from gapic.samplegen_utils.utils import coerce_response_name, is_valid_sample_cfg
24+
from gapic.samplegen_utils.utils import coerce_response_name, is_valid_sample_cfg, render_format_string
2525
from gapic.samplegen_utils.types import DuplicateSample
2626
from gapic.samplegen import manifest, samplegen
2727
from gapic.generator import formatter
@@ -62,6 +62,7 @@ def __init__(self, opts: Options) -> None:
6262
self._env.filters["sort_lines"] = utils.sort_lines
6363
self._env.filters["wrap"] = utils.wrap
6464
self._env.filters["coerce_response_name"] = coerce_response_name
65+
self._env.filters["render_format_string"] = render_format_string
6566

6667
# Add tests to determine type of expressions stored in strings
6768
self._env.tests["str_field_pb"] = utils.is_str_field_pb

packages/gapic-generator/gapic/samplegen_utils/utils.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import os
1919
import yaml
2020

21-
from typing import (Generator, Tuple)
21+
from typing import (Generator, Tuple, List, Union)
2222

2323
from gapic.samplegen_utils import types
2424

@@ -28,6 +28,25 @@
2828
VALID_CONFIG_TYPE = "com.google.api.codegen.samplegen.v1p2.SampleConfigProto"
2929

3030

31+
def render_format_string(s: str, expressions: List[str] = []) -> str:
32+
"""Given string s and a list of expressions, substitute each %s
33+
in the string with {exp}.
34+
35+
Arguments:
36+
s (str): The string literal.
37+
expressions (Optional[List[str]]): A list of expressions.
38+
"""
39+
40+
s = s.replace('\"', '\\\"')
41+
42+
for exp in expressions:
43+
# some expressions will contain references to "$resp"
44+
exp = coerce_response_name(exp)
45+
s = s.replace("%s", f"{{{exp}}}", 1)
46+
47+
return s
48+
49+
3150
def coerce_response_name(s: str) -> str:
3251
# In the sample config, the "$resp" keyword is used to refer to the
3352
# item of interest as received by the corresponding calling form.

packages/gapic-generator/gapic/templates/examples/feature_fragments.j2

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@
2727
{% endmacro %}
2828

2929
{% macro print_string_formatting(string_list) %}
30+
3031
{% if string_list|length == 1 %}
31-
"{{ string_list[0]|replace("%s", "{}")|replace('\"', '\\\"') }}"
32+
"{{ string_list[0] | render_format_string }}"
33+
{% elif string_list|length == 2 and string_list[0] == "%s" and string_list[1] == "$resp" %}
34+
response
3235
{% else %}
33-
"{{ string_list[0]|replace("%s", "{}")|replace('\"', '\\\"') }}".format({{ string_list[1:]|map("coerce_response_name")|join(", ") }})
36+
{# Note: This is the equivalent of render_format_string(string_list[0], string_list[1:] )
37+
# See https://jinja.palletsprojects.com/en/3.0.x/api/#custom-filters #}
38+
f"{{ string_list[0] | render_format_string(string_list[1:]) }}"
3439
{% endif %}
3540
{% endmacro %}
3641

packages/gapic-generator/tests/integration/goldens/asset/samples/generated_samples/cloudasset_generated_asset_v1_asset_service_analyze_iam_policy_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ async def sample_analyze_iam_policy():
4545
response = await client.analyze_iam_policy(request=request)
4646

4747
# Handle response
48-
print("{}".format(response))
48+
print(response)
4949

5050
# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_async]

packages/gapic-generator/tests/integration/goldens/asset/samples/generated_samples/cloudasset_generated_asset_v1_asset_service_analyze_iam_policy_longrunning_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ async def sample_analyze_iam_policy_longrunning():
5151
print("Waiting for operation to complete...")
5252

5353
response = await operation.result()
54-
print("{}".format(response))
54+
print(response)
5555

5656
# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_async]

packages/gapic-generator/tests/integration/goldens/asset/samples/generated_samples/cloudasset_generated_asset_v1_asset_service_analyze_iam_policy_longrunning_sync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ def sample_analyze_iam_policy_longrunning():
5151
print("Waiting for operation to complete...")
5252

5353
response = operation.result()
54-
print("{}".format(response))
54+
print(response)
5555

5656
# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_sync]

packages/gapic-generator/tests/integration/goldens/asset/samples/generated_samples/cloudasset_generated_asset_v1_asset_service_analyze_iam_policy_sync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ def sample_analyze_iam_policy():
4545
response = client.analyze_iam_policy(request=request)
4646

4747
# Handle response
48-
print("{}".format(response))
48+
print(response)
4949

5050
# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_sync]

packages/gapic-generator/tests/integration/goldens/asset/samples/generated_samples/cloudasset_generated_asset_v1_asset_service_batch_get_assets_history_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ async def sample_batch_get_assets_history():
4242
response = await client.batch_get_assets_history(request=request)
4343

4444
# Handle response
45-
print("{}".format(response))
45+
print(response)
4646

4747
# [END cloudasset_generated_asset_v1_AssetService_BatchGetAssetsHistory_async]

packages/gapic-generator/tests/integration/goldens/asset/samples/generated_samples/cloudasset_generated_asset_v1_asset_service_batch_get_assets_history_sync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ def sample_batch_get_assets_history():
4242
response = client.batch_get_assets_history(request=request)
4343

4444
# Handle response
45-
print("{}".format(response))
45+
print(response)
4646

4747
# [END cloudasset_generated_asset_v1_AssetService_BatchGetAssetsHistory_sync]

0 commit comments

Comments
 (0)