|
| 1 | +import json |
| 2 | +from six import python_2_unicode_compatible |
| 3 | + |
| 4 | +__title__ = 'django_elasticsearch_dsl_drf.wrappers' |
| 5 | +__author__ = 'Artur Barseghyan <[email protected]>' |
| 6 | +__copyright__ = '2017-2018 Artur Barseghyan' |
| 7 | +__license__ = 'GPL 2.0/LGPL 2.1' |
| 8 | +__all__ = ( |
| 9 | + 'dict_to_obj', |
| 10 | + 'obj_to_dict', |
| 11 | + 'Wrapper', |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +@python_2_unicode_compatible |
| 16 | +class Wrapper(object): |
| 17 | + """Wrapper. |
| 18 | +
|
| 19 | + Example: |
| 20 | + >>> from django_elasticsearch_dsl_drf.wrappers import DictProxy |
| 21 | + >>> |
| 22 | + >>> mapping = { |
| 23 | + >>> 'country': { |
| 24 | + >>> 'name': 'Netherlands', |
| 25 | + >>> 'province': { |
| 26 | + >>> 'name': 'North Holland', |
| 27 | + >>> 'city': { |
| 28 | + >>> 'name': 'Amsterdam', |
| 29 | + >>> } |
| 30 | + >>> } |
| 31 | + >>> } |
| 32 | + >>> } |
| 33 | + >>> |
| 34 | + >>> wrapper = dict_to_obj(mapping) |
| 35 | + >>> wrapper.country.name |
| 36 | + >>> "Netherlands" |
| 37 | + >>> wrapper.country.province.name |
| 38 | + >>> "North Holland" |
| 39 | + >>> wrapper.country.province.city.name |
| 40 | + >>> "Amsterdam" |
| 41 | + >>> wrapper.as_dict |
| 42 | + >>> { |
| 43 | + >>> 'country': { |
| 44 | + >>> 'name': 'Netherlands', |
| 45 | + >>> 'province': { |
| 46 | + >>> 'name': 'North Holland', |
| 47 | + >>> 'city': { |
| 48 | + >>> 'name': 'Amsterdam', |
| 49 | + >>> } |
| 50 | + >>> } |
| 51 | + >>> } |
| 52 | + >>> } |
| 53 | + >>> str(wrapper) |
| 54 | + >>> "Netherlands" |
| 55 | + """ |
| 56 | + |
| 57 | + def __str__(self): |
| 58 | + for key, item in self.__dict__.items(): |
| 59 | + if isinstance(item, Wrapper): |
| 60 | + return item.__str__() |
| 61 | + else: |
| 62 | + return item |
| 63 | + |
| 64 | + @property |
| 65 | + def as_dict(self): |
| 66 | + """As dict. |
| 67 | +
|
| 68 | + :return: |
| 69 | + :rtype: dict |
| 70 | + """ |
| 71 | + return obj_to_dict(self) |
| 72 | + |
| 73 | + @property |
| 74 | + def as_json(self): |
| 75 | + """As JSON. |
| 76 | +
|
| 77 | + :return: |
| 78 | + :rtype: str |
| 79 | + """ |
| 80 | + return json.dumps(self.as_dict) |
| 81 | + |
| 82 | + |
| 83 | +def dict_to_obj(mapping): |
| 84 | + """dict to obj mapping. |
| 85 | +
|
| 86 | + :param mapping: |
| 87 | + :type mapping: dict |
| 88 | + :return: |
| 89 | + :rtype: :obj:`Wrapper` |
| 90 | + """ |
| 91 | + wrapper = Wrapper() |
| 92 | + |
| 93 | + for key, item in mapping.items(): |
| 94 | + if isinstance(item, dict): |
| 95 | + setattr(wrapper, key, dict_to_obj(item)) |
| 96 | + else: |
| 97 | + setattr(wrapper, key, item) |
| 98 | + return wrapper |
| 99 | + |
| 100 | + |
| 101 | +def obj_to_dict(obj): |
| 102 | + """Wrapper to dict. |
| 103 | +
|
| 104 | + :param obj: |
| 105 | + :type obj: `obj`:Wrapper: |
| 106 | + :return: |
| 107 | + :rtype: dict |
| 108 | + """ |
| 109 | + mapping = {} |
| 110 | + |
| 111 | + for key, item in obj.__dict__.items(): |
| 112 | + if isinstance(item, Wrapper): |
| 113 | + mapping.update({key: obj_to_dict(item)}) |
| 114 | + else: |
| 115 | + mapping.update({key: item}) |
| 116 | + |
| 117 | + return mapping |
0 commit comments