From ef8c1124d0c05048dc8b654ad779d17c3be36ee6 Mon Sep 17 00:00:00 2001 From: Rob White Date: Sat, 26 Mar 2016 18:46:50 +1100 Subject: [PATCH] Add function to convert CamelCased key names to snake_names --- lib/ansible/module_utils/ec2.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/ansible/module_utils/ec2.py b/lib/ansible/module_utils/ec2.py index 52b085eb78..18f7d1f590 100644 --- a/lib/ansible/module_utils/ec2.py +++ b/lib/ansible/module_utils/ec2.py @@ -262,3 +262,24 @@ def paging(pause=0): 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