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

feat: allow warehouse name to be customized #717

Merged
merged 3 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 10 additions & 3 deletions gapic/schema/naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Naming(abc.ABC):
version: str = ''
product_name: str = ''
proto_package: str = ''
_warehouse_package_name: str = ''

def __post_init__(self):
if not self.product_name:
Expand Down Expand Up @@ -141,6 +142,10 @@ def build(
# with ('x.y',) will become a two-tuple: ('x', 'y')
i.capitalize() for i in '.'.join(opts.namespace).split('.')
))
if opts.warehouse_package_name:
package_info = dataclasses.replace(package_info,
_warehouse_package_name=opts.warehouse_package_name
)

# Done; return the naming information.
return package_info
Expand Down Expand Up @@ -186,9 +191,11 @@ def versioned_module_name(self) -> str:
@property
def warehouse_package_name(self) -> str:
"""Return the appropriate Python package name for Warehouse."""

# Piece the name and namespace together to come up with the
# proper package name.
# If a custom name has been set, use it
if self._warehouse_package_name:
return self._warehouse_package_name
# Otherwise piece the name and namespace together to come
# up with the proper package name.
answer = list(self.namespace) + self.name.split(' ')
return '-'.join(answer).lower()

Expand Down
4 changes: 4 additions & 0 deletions gapic/utils/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Options:
"""
name: str = ''
namespace: Tuple[str, ...] = dataclasses.field(default=())
warehouse_package_name: str = ''
retry: Optional[Dict[str, Any]] = None
sample_configs: Tuple[str, ...] = dataclasses.field(default=())
templates: Tuple[str, ...] = dataclasses.field(default=('DEFAULT',))
Expand All @@ -53,6 +54,7 @@ class Options:
'add-iam-methods', # microgenerator implementation for `reroute_to_grpc_interface`
# transport type(s) delineated by '+' (i.e. grpc, rest, custom.[something], etc?)
'transport',
'warehouse-package-name' # change the package name on PyPI
))

@classmethod
Expand Down Expand Up @@ -129,6 +131,8 @@ def tweak_path(p):
answer = Options(
name=opts.pop('name', ['']).pop(),
namespace=tuple(opts.pop('namespace', [])),
warehouse_package_name=opts.pop(
'warehouse-package-name', ['']).pop(),
retry=retry_cfg,
sample_configs=tuple(
cfg_path
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/generator/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,8 @@ def test_options_old_naming():
def test_options_add_iam_methods():
opts = Options.build('add-iam-methods')
assert opts.add_iam_methods


def test_options_warehouse_package_name():
opts = Options.build('warehouse-package-name')
assert opts.warehouse_package_name
10 changes: 10 additions & 0 deletions tests/unit/schema/test_naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ def test_cli_override_name_and_namespace_versionless():
assert not n.version


def test_cli_override_warehouse_package_name():
FileDesc = descriptor_pb2.FileDescriptorProto
proto1 = FileDesc(package='google.translation')
n = naming.Naming.build(
proto1,
opts=Options(warehouse_package_name='google-cloud-foo'),
)
assert n.warehouse_package_name == "google-cloud-foo"


def test_build_factory():
proto = descriptor_pb2.FileDescriptorProto(
package='google.mollusc.v1alpha1'
Expand Down