Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(snippetgen): use f-strings in print statements #975

Merged
merged 4 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ Execute unit tests by running one of the sessions prefixed with `unit-`.
find gapic tests -name "*.py" -not -path 'tests/integration/goldens/*' | xargs autopep8 --diff --exit-code
```

- Format sources in place:

```
find gapic tests -name "*.py" -not -path 'tests/integration/goldens/*' | xargs autopep8 --in-place
```

## Integration Tests

- Run a single integration test for one API. This generates Python source code
Expand Down
3 changes: 2 additions & 1 deletion gapic/generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from typing import Any, DefaultDict, Dict, Mapping
from hashlib import sha256
from collections import OrderedDict, defaultdict
from gapic.samplegen_utils.utils import coerce_response_name, is_valid_sample_cfg
from gapic.samplegen_utils.utils import coerce_response_name, is_valid_sample_cfg, render_format_string
from gapic.samplegen_utils.types import DuplicateSample
from gapic.samplegen import manifest, samplegen
from gapic.generator import formatter
Expand Down Expand Up @@ -62,6 +62,7 @@ def __init__(self, opts: Options) -> None:
self._env.filters["sort_lines"] = utils.sort_lines
self._env.filters["wrap"] = utils.wrap
self._env.filters["coerce_response_name"] = coerce_response_name
self._env.filters["render_format_string"] = render_format_string

# Add tests to determine type of expressions stored in strings
self._env.tests["str_field_pb"] = utils.is_str_field_pb
Expand Down
21 changes: 20 additions & 1 deletion gapic/samplegen_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import os
import yaml

from typing import (Generator, Tuple)
from typing import (Generator, Tuple, List, Union)

from gapic.samplegen_utils import types

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


def render_format_string(s: str, expressions: List[str] = []) -> str:
"""Given string s and a list of expressions, substitute each %s
in the string with {exp}.

Arguments:
s (str): The string literal.
expressions (Optional[List[str]]): A list of expressions.
"""

s = s.replace('\"', '\\\"')

for exp in expressions:
# some expressions will contain references to "$resp"
exp = coerce_response_name(exp)
s = s.replace("%s", f"{{{exp}}}", 1)

return s


def coerce_response_name(s: str) -> str:
# In the sample config, the "$resp" keyword is used to refer to the
# item of interest as received by the corresponding calling form.
Expand Down
9 changes: 7 additions & 2 deletions gapic/templates/examples/feature_fragments.j2
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@
{% endmacro %}

{% macro print_string_formatting(string_list) %}

{% if string_list|length == 1 %}
"{{ string_list[0]|replace("%s", "{}")|replace('\"', '\\\"') }}"
"{{ string_list[0] | render_format_string }}"
{% elif string_list|length == 2 and string_list[0] == "%s" and string_list[1] == "$resp" %}
response
{% else %}
"{{ string_list[0]|replace("%s", "{}")|replace('\"', '\\\"') }}".format({{ string_list[1:]|map("coerce_response_name")|join(", ") }})
{# Note: This is the equivalent of render_format_string(string_list[0], string_list[1:] )
# See https://jinja.palletsprojects.com/en/3.0.x/api/#custom-filters #}
f"{{ string_list[0] | render_format_string(string_list[1:]) }}"
{% endif %}
{% endmacro %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ async def sample_analyze_iam_policy():
response = await client.analyze_iam_policy(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_async]
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ async def sample_analyze_iam_policy_longrunning():
print("Waiting for operation to complete...")

response = await operation.result()
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_async]
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ def sample_analyze_iam_policy_longrunning():
print("Waiting for operation to complete...")

response = operation.result()
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ def sample_analyze_iam_policy():
response = client.analyze_iam_policy(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ async def sample_batch_get_assets_history():
response = await client.batch_get_assets_history(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_BatchGetAssetsHistory_async]
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ def sample_batch_get_assets_history():
response = client.batch_get_assets_history(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_BatchGetAssetsHistory_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ async def sample_create_feed():
response = await client.create_feed(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_CreateFeed_async]
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ def sample_create_feed():
response = client.create_feed(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_CreateFeed_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ async def sample_export_assets():
print("Waiting for operation to complete...")

response = await operation.result()
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_ExportAssets_async]
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ def sample_export_assets():
print("Waiting for operation to complete...")

response = operation.result()
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_ExportAssets_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ async def sample_get_feed():
response = await client.get_feed(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_GetFeed_async]
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ def sample_get_feed():
response = client.get_feed(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_GetFeed_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ async def sample_list_assets():
# Make the request
page_result = client.list_assets(request=request)
async for response in page_result:
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_ListAssets_async]
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ def sample_list_assets():
# Make the request
page_result = client.list_assets(request=request)
for response in page_result:
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_ListAssets_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ async def sample_list_feeds():
response = await client.list_feeds(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_ListFeeds_async]
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ def sample_list_feeds():
response = client.list_feeds(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_ListFeeds_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ async def sample_search_all_iam_policies():
# Make the request
page_result = client.search_all_iam_policies(request=request)
async for response in page_result:
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_SearchAllIamPolicies_async]
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ def sample_search_all_iam_policies():
# Make the request
page_result = client.search_all_iam_policies(request=request)
for response in page_result:
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_SearchAllIamPolicies_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ async def sample_search_all_resources():
# Make the request
page_result = client.search_all_resources(request=request)
async for response in page_result:
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_SearchAllResources_async]
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ def sample_search_all_resources():
# Make the request
page_result = client.search_all_resources(request=request)
for response in page_result:
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_SearchAllResources_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ async def sample_update_feed():
response = await client.update_feed(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_UpdateFeed_async]
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ def sample_update_feed():
response = client.update_feed(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END cloudasset_generated_asset_v1_AssetService_UpdateFeed_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ async def sample_generate_access_token():
response = await client.generate_access_token(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END iamcredentials_generated_credentials_v1_IAMCredentials_GenerateAccessToken_async]
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ def sample_generate_access_token():
response = client.generate_access_token(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END iamcredentials_generated_credentials_v1_IAMCredentials_GenerateAccessToken_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ async def sample_generate_id_token():
response = await client.generate_id_token(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END iamcredentials_generated_credentials_v1_IAMCredentials_GenerateIdToken_async]
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ def sample_generate_id_token():
response = client.generate_id_token(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END iamcredentials_generated_credentials_v1_IAMCredentials_GenerateIdToken_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ async def sample_sign_blob():
response = await client.sign_blob(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END iamcredentials_generated_credentials_v1_IAMCredentials_SignBlob_async]
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ def sample_sign_blob():
response = client.sign_blob(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END iamcredentials_generated_credentials_v1_IAMCredentials_SignBlob_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ async def sample_sign_jwt():
response = await client.sign_jwt(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END iamcredentials_generated_credentials_v1_IAMCredentials_SignJwt_async]
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ def sample_sign_jwt():
response = client.sign_jwt(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END iamcredentials_generated_credentials_v1_IAMCredentials_SignJwt_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ async def sample_create_bucket():
response = await client.create_bucket(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END logging_generated_logging_v2_ConfigServiceV2_CreateBucket_async]
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ def sample_create_bucket():
response = client.create_bucket(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END logging_generated_logging_v2_ConfigServiceV2_CreateBucket_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ async def sample_create_exclusion():
response = await client.create_exclusion(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END logging_generated_logging_v2_ConfigServiceV2_CreateExclusion_async]
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ def sample_create_exclusion():
response = client.create_exclusion(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END logging_generated_logging_v2_ConfigServiceV2_CreateExclusion_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ async def sample_create_sink():
response = await client.create_sink(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END logging_generated_logging_v2_ConfigServiceV2_CreateSink_async]
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ def sample_create_sink():
response = client.create_sink(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END logging_generated_logging_v2_ConfigServiceV2_CreateSink_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ async def sample_create_view():
response = await client.create_view(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END logging_generated_logging_v2_ConfigServiceV2_CreateView_async]
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ def sample_create_view():
response = client.create_view(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END logging_generated_logging_v2_ConfigServiceV2_CreateView_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ async def sample_get_bucket():
response = await client.get_bucket(request=request)

# Handle response
print("{}".format(response))
print(response)

# [END logging_generated_logging_v2_ConfigServiceV2_GetBucket_async]
Loading