From 715ab124fd9474e6b2d3be76431b72f1161b4e28 Mon Sep 17 00:00:00 2001 From: James Saryerwinnie Date: Mon, 25 Jun 2018 16:16:46 -0700 Subject: [PATCH] Simplify xform_name() to not separate words by numbers The _number_cap regex has proven to not be the "right thing" to do in practice. In practice we end up having to put customizations in to fix what the _number_cap regex is doing. The most common cases are (left being what it does, right being what we actually want): * `s_3 -> s3` * `ec_2 -> ec2` * `ipv_6 -> ipv6` This change removes the _number_cap regex, which in turn removes our partial renames hack and simplifies the code. This ended up having no impact for botocore (aside from one unit test I had to fix), I verified that all the names we `xform_name()` on in botocore are identical between this change and the latest develop branch. Most of these changes affect the parameters of operations, and in botocore you pass the parameter exactly as its cased in the service model. I will have to make changes in the CLI though. Because the CLI transforms top level parameters, I'll have to go through and set aliases for all the parameters that are changed because of this (about 10 parameters). --- botocore/__init__.py | 18 ++---------------- tests/unit/test_client.py | 4 ++-- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/botocore/__init__.py b/botocore/__init__.py index cf06dd0f0f..3ed2aa761a 100644 --- a/botocore/__init__.py +++ b/botocore/__init__.py @@ -29,7 +29,6 @@ def emit(self, record): _first_cap_regex = re.compile('(.)([A-Z][a-z]+)') -_number_cap_regex = re.compile('([a-z])([0-9]+)') _end_cap_regex = re.compile('([a-z0-9])([A-Z])') # The regex below handles the special case where some acryonym # name is pluralized, e.g GatewayARNs, ListWebACLs, SomeCNAMEs. @@ -52,12 +51,6 @@ def emit(self, record): # services which might have a matching argument or operation. This way a # common mis-translation can be fixed without having to call out each # individual case. -_partial_renames = { - 'ipv-6': 'ipv6', - 'ipv_6': 'ipv6', - 's_3_resources': 's3_resources', - 's-3-resources': 's3-resources', -} ScalarTypes = ('string', 'integer', 'boolean', 'timestamp', 'float', 'double') BOTOCORE_ROOT = os.path.dirname(os.path.abspath(__file__)) @@ -75,8 +68,7 @@ def __deepcopy__(self, memodict): UNSIGNED = UNSIGNED() -def xform_name(name, sep='_', _xform_cache=_xform_cache, - partial_renames=_partial_renames): +def xform_name(name, sep='_', _xform_cache=_xform_cache): """Convert camel case to a "pythonic" name. If the name contains the ``sep`` character, then it is @@ -95,12 +87,6 @@ def xform_name(name, sep='_', _xform_cache=_xform_cache, # Replace something like ARNs, ACLs with _arns, _acls. name = name[:-len(matched)] + sep + matched.lower() s1 = _first_cap_regex.sub(r'\1' + sep + r'\2', name) - s2 = _number_cap_regex.sub(r'\1' + sep + r'\2', s1) - transformed = _end_cap_regex.sub(r'\1' + sep + r'\2', s2).lower() - - # Do partial renames - for old, new in partial_renames.items(): - if old in transformed: - transformed = transformed.replace(old, new) + transformed = _end_cap_regex.sub(r'\1' + sep + r'\2', s1).lower() _xform_cache[key] = transformed return _xform_cache[key] diff --git a/tests/unit/test_client.py b/tests/unit/test_client.py index dd7ef6947d..b3bb6e9ad6 100644 --- a/tests/unit/test_client.py +++ b/tests/unit/test_client.py @@ -758,8 +758,8 @@ def test_service_has_waiter_configs(self): creator = self.create_client_creator() service_client = creator.create_client('myservice', 'us-west-2') self.assertEqual(sorted(service_client.waiter_names), - sorted(['waiter_1', 'waiter_2'])) - self.assertTrue(hasattr(service_client.get_waiter('waiter_1'), 'wait')) + sorted(['waiter1', 'waiter2'])) + self.assertTrue(hasattr(service_client.get_waiter('waiter1'), 'wait')) def test_service_has_no_waiter_configs(self): self.loader.load_service_model.side_effect = [