Skip to content

Commit 49a8671

Browse files
authored
chore(generator): add compatability layer templates (#17886)
This PR consolidates the common code into a temporary compatibility layer as a fallback implementation as we move this code to google-api-core. Once, the minimum supported version of google-api-core has these functions we will clean up this layer. Towards: #17883
1 parent eceea95 commit 49a8671

19 files changed

Lines changed: 292 additions & 3 deletions

File tree

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
from google.protobuf.compiler.plugin_pb2 import CodeGeneratorResponse
3838

3939

40+
ALLOWED_PRIVATE_TEMPLATES = (
41+
"__init__.py.j2",
42+
"_compat.py.j2",
43+
)
44+
45+
4046
class Generator:
4147
"""A protoc code generator for client libraries.
4248
@@ -118,9 +124,9 @@ def get_response(self, api_schema: api.API, opts: Options) -> CodeGeneratorRespo
118124
# and instead of iterating over it/them, we iterate over samples
119125
# and plug those into the template.
120126
for template_name in client_templates:
121-
# Quick check: Skip "private" templates.
127+
# Quick check: Skip "private" templates except explicitly allowed ones.
122128
filename = template_name.split("/")[-1]
123-
if filename.startswith("_") and filename != "__init__.py.j2":
129+
if filename.startswith("_") and filename not in ALLOWED_PRIVATE_TEMPLATES:
124130
continue
125131

126132
# Append to the output files dictionary.
@@ -413,7 +419,7 @@ def _get_file(
413419

414420
# Quick check: Do not render empty files.
415421
if utils.empty(cgr_file.content) and not fn.endswith(
416-
("py.typed", "__init__.py")
422+
("py.typed", "__init__.py", "_compat.py")
417423
):
418424
return {}
419425

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{% extends "_base.py.j2" %}
2+
3+
{% block content %}
4+
5+
"""A compatibility module for older versions of google-api-core."""
6+
{# TODO(https://github.com/googleapis/google-cloud-python/issues/17813):
7+
Clean up this file/functions when the minimum supported version of
8+
google-api-core has the functions in `_compat.py.j2`. #}
9+
{# TODO(https://github.com/googleapis/google-cloud-python/issues/17884):
10+
Add conditional logic to check if static code exists in google-api-core and use it from there,
11+
falling back to the local implementation if not present. #}
12+
{# TODO(https://github.com/googleapis/google-cloud-python/issues/17883):
13+
Backfill compatibility functions being removed from the client layer. #}
14+
15+
{% endblock %}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% extends "_base.py.j2" %}
2+
3+
{% block content %}
4+
5+
"""Tests for the compatibility module for older versions of google-api-core."""
6+
{# TODO(https://github.com/googleapis/google-cloud-python/issues/17813):
7+
Clean up this file/tests when the minimum supported version of
8+
google-api-core has the functions in `_compat.py.j2`. #}
9+
{# TODO(https://github.com/googleapis/google-cloud-python/issues/17883):
10+
Backfill compatibility functions tests being removed from the client layer. #}
11+
12+
{% endblock %}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2026 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
"""A compatibility module for older versions of google-api-core."""
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2026 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
"""Tests for the compatibility module for older versions of google-api-core."""
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2026 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
"""A compatibility module for older versions of google-api-core."""
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2026 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
"""Tests for the compatibility module for older versions of google-api-core."""
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2026 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
"""A compatibility module for older versions of google-api-core."""
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2026 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
"""Tests for the compatibility module for older versions of google-api-core."""
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2026 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
"""A compatibility module for older versions of google-api-core."""

0 commit comments

Comments
 (0)