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

Source Code for Module simplify.domain

  1   
  2  # 
  3  # Copyright (c) 2013, 2014 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 values.iteritems(): 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 to_dict(self):
47 48 d = dict() 49 for key, value in self.__dict__.iteritems(): 50 if isinstance(value, Domain): 51 d[key] = value.to_dict() 52 else: 53 d[key] = value 54 return d
55
56 - def class_name(self):
57 return self.__class__.__name__.lower()
58
59 - def __str__(self):
60 return json.dumps(self.to_dict(), sort_keys = True, indent = 2, cls = PaymentObjectEncoder)
61
62 -class PaymentObjectEncoder(json.JSONEncoder):
63 - def default(self, obj):
64 if isinstance(obj, Domain): 65 return obj.to_dict() 66 else: 67 return json.JSONEncoder.default(self, obj)
68
69 -def build_payment_object(typ, value):
70 if isinstance(value, dict): 71 return DomainFactory.factory(typ, value) 72 else: 73 return value
74
75 -class DomainFactory(object):
76 77 cache = {} 78 79 @classmethod
80 - def factory(cls, module_name, values = None, fail_on_error = False):
81 82 class_name = module_name[0].upper() + module_name[1:] 83 try: 84 if class_name in cls.cache: 85 class_ = cls.cache[class_name] 86 else: 87 module_ = __import__('simplify') 88 class_ = getattr(module_, class_name) 89 cls.cache[class_name] = class_ 90 if isinstance(values, dict): 91 return class_(values) 92 else: 93 return class_() 94 except AttributeError as e: 95 if fail_on_error: 96 raise e 97 else: 98 return DomainFactory.factory("domain", values, True)
99