Package simplify :: Module domain
[hide private]
[frames] | no frames]

Source Code for Module simplify.domain

  1   
  2  # 
  3  # Copyright (c) 2013 - 2024 MasterCard International Incorporated 
  4  # All rights reserved. 
  5  #  
  6  # Redistribution and use in source and binary forms, with or without modification, are  
  7  # permitted provided that the following conditions are met: 
  8  #  
  9  # Redistributions of source code must retain the above copyright notice, this list of  
 10  # conditions and the following disclaimer. 
 11  # Redistributions in binary form must reproduce the above copyright notice, this list of  
 12  # conditions and the following disclaimer in the documentation and/or other materials  
 13  # provided with the distribution. 
 14  # Neither the name of the MasterCard International Incorporated nor the names of its  
 15  # contributors may be used to endorse or promote products derived from this software  
 16  # without specific prior written permission. 
 17  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY  
 18  # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES  
 19  # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT  
 20  # SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,  
 21  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
 22  # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
 23  # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER  
 24  # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING  
 25  # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF  
 26  # SUCH DAMAGE. 
 27  # 
 28   
 29  import json 
30 31 -class Domain:
32
33 - def __init__(self, values = None):
34 if values is None: 35 values = {} 36 for key, value in list(values.items()): 37 self.__dict__[key] = build_payment_object(key, value) 38 39 if "id" in self.__dict__: 40 self.object_id = self.__dict__["id"]
41
42 - def __getitem__(self, key):
43 if key in self.__dict__: 44 return self.__dict__[key]
45
46 - def __setitem__(self, key, value):
47 self.__dict__[key] = value
48
49 - def to_dict(self):
50 51 d = dict() 52 for key, value in list(self.__dict__.items()): 53 if isinstance(value, Domain): 54 d[key] = value.to_dict() 55 else: 56 d[key] = value 57 return d
58
59 - def class_name(self):
60 return self.__class__.__name__.lower()
61
62 - def __str__(self):
63 return json.dumps(self.to_dict(), sort_keys = True, indent = 2, cls = PaymentObjectEncoder)
64
65 -class PaymentObjectEncoder(json.JSONEncoder):
66 - def default(self, obj):
67 if isinstance(obj, Domain): 68 return obj.to_dict() 69 else: 70 return json.JSONEncoder.default(self, obj)
71
72 -def build_payment_object(typ, value):
73 if isinstance(value, dict): 74 return DomainFactory.factory(typ, value) 75 else: 76 return value
77
78 -class DomainFactory(object):
79 80 cache = {} 81 82 @classmethod
83 - def factory(cls, module_name, values = None, fail_on_error = False):
84 85 class_name = module_name[0].upper() + module_name[1:] 86 try: 87 if class_name in cls.cache: 88 class_ = cls.cache[class_name] 89 else: 90 module_ = __import__('simplify') 91 class_ = getattr(module_, class_name) 92 cls.cache[class_name] = class_ 93 if isinstance(values, dict): 94 return class_(values) 95 else: 96 return class_() 97 except AttributeError as e: 98 if fail_on_error: 99 raise e 100 else: 101 return DomainFactory.factory("domain", values, True)
102