Skip to content

Commit

Permalink
autopep8
Browse files Browse the repository at this point in the history
  • Loading branch information
gkevinzheng committed Dec 11, 2024
1 parent e3e7dbb commit 4cc2cd9
Show file tree
Hide file tree
Showing 3 changed files with 854 additions and 32 deletions.
50 changes: 35 additions & 15 deletions gapic/schema/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,18 +262,19 @@ def build_address_allowlist_for_selective_gapic(self, *,
def prune_messages_for_selective_gapic(self, *,
address_allowlist: Set['metadata.Address']) -> Optional['Proto']:
"""Returns a truncated version of this Proto.
Only the services, messages, and enums contained in the allowlist
of visited addresses are included in the returned object. If there
are no services, messages, or enums left, and no file level resources,
return None.
"""
services = {
k: v.prune_messages_for_selective_gapic(address_allowlist=address_allowlist)
k: v.prune_messages_for_selective_gapic(
address_allowlist=address_allowlist)
for k, v in self.services.items()
if v.meta.address in address_allowlist
}

# For messages and enums we should only be pruning them from all_messages if they
# are proto plus types. This should apply to the Protos we are pruning from, but might
# not in the future.
Expand All @@ -288,10 +289,10 @@ def prune_messages_for_selective_gapic(self, *,
for k, v in self.all_enums.items()
if (v.ident in address_allowlist or not v.ident.is_proto_plus_type)
}

if not services and not all_messages and not all_enums:
return None

return dataclasses.replace(
self,
services=services,
Expand Down Expand Up @@ -427,19 +428,22 @@ def disambiguate_keyword_sanitize_fname(
ignore_unknown_fields=True
)

# Done; return the API.
return cls(naming=naming,
all_protos=protos,
service_yaml_config=service_yaml_config)

# Third pass for various selective GAPIC settings; these require
# settings in the service.yaml and so we build the API object
# before doing another pass.
api = cls(naming=naming,
all_protos=protos,
service_yaml_config=service_yaml_config)

if package in api.all_library_settings:
selective_gapic_methods = api.all_library_settings[package].python_settings.common.selective_gapic_generation.methods
selective_gapic_methods = api.all_library_settings[
package].python_settings.common.selective_gapic_generation.methods
if selective_gapic_methods:

all_resource_messages = collections.ChainMap(
*(proto.resource_messages for proto in protos.values())
)

# Prepare a list of addresses to include in selective GAPIC,
# then prune each Proto object. We look at metadata.Addresses, not objects, because
# objects that refer to the same thing in the proto are different Python objects
Expand All @@ -457,14 +461,15 @@ def disambiguate_keyword_sanitize_fname(
if name not in api.protos:
new_protos[name] = proto
else:
pruned_proto = proto.prune_messages_for_selective_gapic(address_allowlist=address_allowlist)
pruned_proto = proto.prune_messages_for_selective_gapic(
address_allowlist=address_allowlist)
if pruned_proto:
new_protos[name] = pruned_proto

api = cls(naming=naming,
all_protos=new_protos,
service_yaml_config=service_yaml_config)

return api

@cached_property
Expand Down Expand Up @@ -840,6 +845,21 @@ def enforce_valid_library_settings(
continue
versions_seen.add(library_settings.version)

# Check to see if selective gapic generation methods are valid.
selective_gapic_errors = {}
for method_name in library_settings.python_settings.common.selective_gapic_generation.methods:
if method_name not in self.all_methods:
selective_gapic_errors[method_name] = "Method does not exist."
elif not method_name.startswith(library_settings.version):
selective_gapic_errors[method_name] = "Mismatched version for method."

if selective_gapic_errors:
all_errors[library_settings.version] = [
{
"selective_gapic_generation": selective_gapic_errors,
}
]

if all_errors:
raise ClientLibrarySettingsError(yaml.dump(all_errors))

Expand Down
24 changes: 12 additions & 12 deletions gapic/schema/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def build_address_allowlist_for_selective_gapic(self, *,
self.enum.build_address_allowlist_for_selective_gapic(
address_allowlist=address_allowlist,
)

if self.resource_reference and self.resource_reference in resource_messages:
# The message types in resource_message are different objects, but should be
# defined the same as the MessageTypes we're traversing here.
Expand Down Expand Up @@ -786,7 +786,7 @@ def with_context(self, *,
},
meta=self.meta.with_context(collisions=collisions),
)

def build_address_allowlist_for_selective_gapic(self, *,
address_allowlist: Set['metadata.Address'],
resource_messages: Dict[str, 'MessageType']):
Expand All @@ -803,12 +803,12 @@ def build_address_allowlist_for_selective_gapic(self, *,
address_allowlist=address_allowlist,
resource_messages=resource_messages
)

for enum in self.nested_enums.values():
enum.build_address_allowlist_for_selective_gapic(
address_allowlist=address_allowlist,
)

for message in self.nested_messages.values():
message.build_address_allowlist_for_selective_gapic(
address_allowlist=address_allowlist,
Expand Down Expand Up @@ -980,7 +980,7 @@ def with_context(self, *,
visited_messages=visited_messages,
),
)

def build_address_allowlist_for_selective_gapic(self, *,
address_allowlist: Set['metadata.Address'],
resource_messages: Dict[str, 'MessageType']) -> None:
Expand All @@ -989,7 +989,7 @@ def build_address_allowlist_for_selective_gapic(self, *,
This method is used to create an allowlist of addresses to be used to filter out unneeded
services, methods, messages, and enums at a later step.
"""

self.request_type.build_address_allowlist_for_selective_gapic(
address_allowlist=address_allowlist,
resource_messages=resource_messages,
Expand Down Expand Up @@ -1795,12 +1795,13 @@ def build_address_allowlist_for_selective_gapic(self, *,
if self.lro:
self.lro.build_address_allowlist_for_selective_gapic(address_allowlist=address_allowlist,
resource_messages=resource_messages)

if self.extended_lro:
# We need to add the service/method pointed to by self.operation_service to
# We need to add the service/method pointed to by self.operation_service to
# the allowlist, as it might not have been specified by selective_gapic_generation.
# We assume that the operation service lives in the same proto as this one.
operation_service = services_in_proto[self.operation_service] # type: ignore
# type: ignore
operation_service = services_in_proto[self.operation_service]
address_allowlist.add(operation_service.meta.address)
operation_service.operation_polling_method.build_address_allowlist_for_selective_gapic(
address_allowlist=address_allowlist,
Expand All @@ -1812,7 +1813,7 @@ def build_address_allowlist_for_selective_gapic(self, *,
address_allowlist=address_allowlist,
resource_messages=resource_messages,
)

self.input.build_address_allowlist_for_selective_gapic(
address_allowlist=address_allowlist,
resource_messages=resource_messages,
Expand Down Expand Up @@ -2161,11 +2162,10 @@ def build_address_allowlist_for_selective_gapic(self, *,
services_in_proto=services_in_proto,
)


def prune_messages_for_selective_gapic(self, *,
address_allowlist: Set['metadata.Address']) -> 'Service':
"""Returns a truncated version of this Service.
Only the methods, messages, and enums contained in the address allowlist
are included in the returned object.
"""
Expand Down
Loading

0 comments on commit 4cc2cd9

Please sign in to comment.