Skip to content

Commit

Permalink
Simplify xform_name() to not separate words by numbers
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
jamesls committed Jun 25, 2018
1 parent 879f844 commit 715ab12
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 18 deletions.
18 changes: 2 additions & 16 deletions botocore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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__))
Expand All @@ -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
Expand All @@ -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]
4 changes: 2 additions & 2 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit 715ab12

Please sign in to comment.