update lower_dict_keys, avoid mistakes in OrderDict

This commit is contained in:
debugtalk
2017-11-03 12:55:23 +08:00
parent 895478719d
commit 36e7c7e4cf
2 changed files with 37 additions and 17 deletions

View File

@@ -287,31 +287,28 @@ def search_conf_item(start_path, item_type, item_name):
return search_conf_item(dir_path, item_type, item_name)
def lower_dict_key(origin_dict, depth=1):
""" convert dict key to lower case, with depth control supported.
def lower_dict_keys(origin_dict):
""" convert keys in dict to lower case
e.g.
Name => name, Request => request
URL => url, METHOD => method, Headers => headers, Data => data
"""
new_dict = {}
if not origin_dict or not isinstance(origin_dict, dict):
return origin_dict
for key, value in origin_dict.items():
if depth >= 2:
new_dict[key] = value
continue
if isinstance(value, dict):
value = lower_dict_key(value, depth+1)
new_dict[key.lower()] = value
return new_dict
return {
key.lower(): value
for key, value in origin_dict.items()
}
def lower_config_dict_key(config_dict):
""" convert key in config dict to lower case, convertion will occur in two places:
1, all keys in config dict;
2, all keys in config["request"]
"""
config_dict = lower_dict_key(config_dict)
if "request" in config_dict and isinstance(config_dict["request"], dict):
config_dict["request"] = lower_dict_key(config_dict["request"])
config_dict = lower_dict_keys(config_dict)
if "request" in config_dict:
config_dict["request"] = lower_dict_keys(config_dict["request"])
return config_dict