aboutsummaryrefslogtreecommitdiffstats
path: root/app/utils/dict_naming_converter.py
diff options
context:
space:
mode:
authoryayogev <yaronyogev@gmail.com>2018-02-27 17:00:05 +0200
committeryayogev <yaronyogev@gmail.com>2018-02-27 17:00:05 +0200
commit648a394f7a318443dfd82f790f83a79616c26905 (patch)
tree719508e9d99771c3de056a9c2914d461c35fb967 /app/utils/dict_naming_converter.py
parent100add41cfe2b987524b190c1c92771a3c4f1d5f (diff)
US3541 merge various fixes to OPNFV branch
timestamp of last commit tt was merged: 26-Jan-2018 16:25. Change-Id: I7b0bf7885d7d0badb81c794a52c480b905d78459 Signed-off-by: yayogev <yaronyogev@gmail.com>
Diffstat (limited to 'app/utils/dict_naming_converter.py')
-rw-r--r--app/utils/dict_naming_converter.py35
1 files changed, 24 insertions, 11 deletions
diff --git a/app/utils/dict_naming_converter.py b/app/utils/dict_naming_converter.py
index 91fea2e..d0f8d42 100644
--- a/app/utils/dict_naming_converter.py
+++ b/app/utils/dict_naming_converter.py
@@ -8,6 +8,7 @@
# http://www.apache.org/licenses/LICENSE-2.0 #
###############################################################################
from bson.objectid import ObjectId
+from datetime import datetime
class DictNamingConverter:
@@ -20,21 +21,33 @@ class DictNamingConverter:
# Returns:
# Dictionary with the new keys.
@staticmethod
- def change_dict_naming_convention(d, cf):
+ def change_dict_naming_convention(d, cf, level: int=0):
new = {}
+ change_convention = DictNamingConverter.change_dict_naming_convention
if not d:
return d
- if isinstance(d, str):
+ if isinstance(d, str) or isinstance(d, int) or isinstance(d, float) \
+ or isinstance(d, bool) or isinstance(d, datetime):
return d
if isinstance(d, ObjectId):
return d
- for k, v in d.items():
- new_v = v
- if isinstance(v, dict):
- new_v = DictNamingConverter.change_dict_naming_convention(v, cf)
- elif isinstance(v, list):
- new_v = list()
- for x in v:
- new_v.append(DictNamingConverter.change_dict_naming_convention(x, cf))
- new[cf(k)] = new_v
+ if isinstance(d, object) and not isinstance(d, dict):
+ for k in dir(d):
+ if k.startswith('_'):
+ continue
+ v = getattr(d, k)
+ if callable(v):
+ continue
+ new[cf(k)] = change_convention(v, cf, level+1)
+ if isinstance(d, dict):
+ for k, v in d.items():
+ new_v = v
+ if isinstance(v, dict):
+ new_v = change_convention(v, cf, level+1)
+ elif isinstance(v, list):
+ new_v = list()
+ for x in v:
+ list_val = change_convention(x, cf, level+1)
+ new_v.append(list_val)
+ new[cf(k)] = new_v
return new