From cabfda3ed8840fe7dd17d80e3ee922d72f9b7787 Mon Sep 17 00:00:00 2001 From: Abhishek Koneru Date: Mon, 30 Jun 2014 16:04:34 -0400 Subject: Refactoring ProfileClient to remove the property fields. Replaced the usage of python property feature with a dict for attribute name conversion. Fixed an issue caused to traversing the NOTYPES dict in encoder.py to find the instance of an object. The traversal causes an issue in the presence of subclassing. Modified method attr_name_conversion to return a new dictionary with modified attribute names rather than making changes to the object's __dict__. --- base/common/python/pki/encoder.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'base/common/python/pki/encoder.py') diff --git a/base/common/python/pki/encoder.py b/base/common/python/pki/encoder.py index 06a23250e..09e8f5a9f 100644 --- a/base/common/python/pki/encoder.py +++ b/base/common/python/pki/encoder.py @@ -34,21 +34,23 @@ class CustomTypeEncoder(json.JSONEncoder): for k, v in TYPES.items(): if isinstance(obj, v): return {k: obj.__dict__} - for k, v in NOTYPES.items(): - if isinstance(obj, v): - return self.attr_name_conversion(obj.__dict__, v) + if type(obj) in NOTYPES.itervalues(): + return self.attr_name_conversion(obj.__dict__, type(obj)) return json.JSONEncoder.default(self, obj) @staticmethod def attr_name_conversion(attr_dict, object_class): if not hasattr(object_class, 'json_attribute_names'): return attr_dict - for k, v in object_class.json_attribute_names.items(): - if v in attr_dict: - value = attr_dict[v] - del attr_dict[v] - attr_dict[k] = value - return attr_dict + reverse_dict = {v: k for k,v in + object_class.json_attribute_names.iteritems()} + new_dict = dict() + for k, v in attr_dict.items(): + if k in reverse_dict: + new_dict[reverse_dict[k]] = v + else: + new_dict[k] = v + return new_dict def CustomTypeDecoder(dct): -- cgit