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

Add function to convert CamelCased key names to snake_names #15175

Merged
merged 1 commit into from
Mar 27, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/ansible/module_utils/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,24 @@ def page(*args, **kwargs):
return page
return wrapper


def camel_dict_to_snake_dict(camel_dict):

def camel_to_snake(name):

import re

first_cap_re = re.compile('(.)([A-Z][a-z]+)')
all_cap_re = re.compile('([a-z0-9])([A-Z])')
s1 = first_cap_re.sub(r'\1_\2', name)

return all_cap_re.sub(r'\1_\2', s1).lower()


snake_dict = {}
for k, v in camel_dict.iteritems():
if isinstance(v, dict):
v = camel_dict_to_snake_dict(v)
snake_dict[camel_to_snake(k)] = v

return snake_dict