Skip to content

Commit 51b6861

Browse files
authored
Add enums to the default __init__.py (#271)
Fix for #265
1 parent 96eba77 commit 51b6861

2 files changed

Lines changed: 39 additions & 20 deletions

File tree

packages/gapic-generator/gapic/templates/%namespace/%name/__init__.py.j2

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
{% block content %}
44
{# Import subpackages. -#}
5+
{% filter sort_lines -%}
56
{% for subpackage in api.subpackages.keys() -%}
67
from {% if api.naming.module_namespace %}{{ api.naming.module_namespace|join('.') }}.{% endif -%}
78
{{ api.naming.versioned_module_name }} import {{ subpackage }}
@@ -14,34 +15,51 @@ from {% if api.naming.module_namespace %}{{ api.naming.module_namespace|join('.'
1415
{{ api.naming.versioned_module_name }}.services.{{ service.name|snake_case }}.client import {{ service.client_name }}
1516
{% endfor -%}
1617

17-
{# Import messages from each proto.
18+
{# Import messages and enums from each proto.
1819
It is safe to import all of the messages into the same namespace here,
1920
because protocol buffers itself enforces selector uniqueness within
2021
a proto package.
2122
-#}
23+
{# Import messages from each proto.
24+
It is safe to import all of the messages into the same namespace here,
25+
because protocol buffers itself enforces selector uniqueness within
26+
a proto package.
27+
-#}
2228
{% for proto in api.protos.values()|sort(attribute='module_name')
2329
if proto.meta.address.subpackage == api.subpackage_view -%}
24-
{% for message in proto.messages.values()|sort(attribute='name') -%}
30+
{% for message in proto.messages.values()|sort(attribute='name') -%}
2531
from {% if api.naming.module_namespace %}{{ api.naming.module_namespace|join('.') }}.{% endif -%}
2632
{{ api.naming.versioned_module_name }}.types.{{ proto.module_name }} import {{ message.name }}
27-
{% endfor %}{% endfor %}
28-
33+
{% endfor -%}
34+
{% for enum in proto.enums.values()|sort(attribute='name') -%}
35+
from {% if api.naming.module_namespace %}{{ api.naming.module_namespace|join('.') }}.{% endif -%}
36+
{{ api.naming.versioned_module_name }}.types.{{ proto.module_name }} import {{ enum.name }}
37+
{% endfor %}{% endfor -%}
38+
{% endfilter %}
2939
{# Define __all__.
3040
This requires the full set of imported names, so we iterate over
3141
them again.
3242
-#}
3343
__all__ = (
34-
{%- for subpackage in api.subpackages.keys() %}
35-
'{{ subpackage }}',
36-
{%- endfor %}
37-
{%- for service in api.services.values()|sort(attribute='name')
38-
if service.meta.address.subpackage == api.subpackage_view %}
39-
'{{ service.client_name }}',
40-
{%- endfor %}
41-
{%- for proto in api.protos.values()|sort(attribute='module_name')
42-
if proto.meta.address.subpackage == api.subpackage_view %}
43-
{%- for message in proto.messages.values()|sort(attribute='name') %}
44-
'{{ message.name }}',
45-
{%- endfor %}{% endfor %}
44+
{%- filter indent %}
45+
{% filter sort_lines -%}
46+
{% for subpackage in api.subpackages.keys() -%}
47+
'{{ subpackage }}',
48+
{% endfor -%}
49+
{% for service in api.services.values()|sort(attribute='name')
50+
if service.meta.address.subpackage == api.subpackage_view -%}
51+
'{{ service.client_name }}',
52+
{% endfor -%}
53+
{% for proto in api.protos.values()|sort(attribute='module_name')
54+
if proto.meta.address.subpackage == api.subpackage_view -%}
55+
{% for message in proto.messages.values()|sort(attribute='name') -%}
56+
'{{ message.name }}',
57+
{% endfor -%}
58+
{% for enum in proto.enums.values()|sort(attribute='name')
59+
if proto.meta.address.subpackage == api.subpackage_view -%}
60+
'{{ enum.name }}',
61+
{% endfor -%}{% endfor -%}
62+
{% endfilter -%}
63+
{% endfilter -%}
4664
)
4765
{% endblock %}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import textwrap
16+
from typing import Iterable
1617

1718

1819
def sort_lines(text: str, dedupe: bool = True) -> str:
@@ -27,11 +28,11 @@ def sort_lines(text: str, dedupe: bool = True) -> str:
2728
trailing = '\n' if text.endswith('\n') else ''
2829

2930
# Split the text into individual lines, throwing away any empty lines.
30-
lines = [i for i in text.strip().split('\n') if i.strip()]
31+
lines: Iterable[str] = (i for i in text.strip().split('\n') if i.strip())
3132

3233
# De-duplicate the lines if requested.
3334
if dedupe:
34-
lines = list(set(lines))
35+
lines = set(lines)
3536

3637
# Return the final string.
3738
answer = '\n'.join(sorted(lines))
@@ -78,8 +79,8 @@ def wrap(text: str, width: int, *, offset: int = None, indent: int = 0) -> str:
7879
first = text.split('\n')[0] + '\n'
7980
if len(first) > width - offset:
8081
initial = textwrap.wrap(first,
81-
break_long_words=False,
82-
width=width - offset,
82+
break_long_words=False,
83+
width=width - offset,
8384
)
8485
# Strip the first \n from the text so it is not misidentified as an
8586
# intentionally short line below.

0 commit comments

Comments
 (0)