OLD | NEW |
(Empty) | |
| 1 from decimal import Decimal |
| 2 |
| 3 |
| 4 def ResponseFactory(action): |
| 5 class FPSResponse(Response): |
| 6 _action = action |
| 7 _Result = globals().get(action + 'Result', ResponseElement) |
| 8 |
| 9 # due to nodes receiving their closing tags |
| 10 def endElement(self, name, value, connection): |
| 11 if name != action + 'Response': |
| 12 Response.endElement(self, name, value, connection) |
| 13 return FPSResponse |
| 14 |
| 15 |
| 16 class ResponseElement(object): |
| 17 def __init__(self, connection=None, name=None): |
| 18 if connection is not None: |
| 19 self._connection = connection |
| 20 self._name = name or self.__class__.__name__ |
| 21 |
| 22 @property |
| 23 def connection(self): |
| 24 return self._connection |
| 25 |
| 26 def __repr__(self): |
| 27 render = lambda pair: '{!s}: {!r}'.format(*pair) |
| 28 do_show = lambda pair: not pair[0].startswith('_') |
| 29 attrs = filter(do_show, self.__dict__.items()) |
| 30 return '{0}({1})'.format(self.__class__.__name__, |
| 31 ', '.join(map(render, attrs))) |
| 32 |
| 33 def startElement(self, name, attrs, connection): |
| 34 return None |
| 35 |
| 36 # due to nodes receiving their closing tags |
| 37 def endElement(self, name, value, connection): |
| 38 if name != self._name: |
| 39 setattr(self, name, value) |
| 40 |
| 41 |
| 42 class Response(ResponseElement): |
| 43 _action = 'Undefined' |
| 44 |
| 45 def startElement(self, name, attrs, connection): |
| 46 if name == 'ResponseMetadata': |
| 47 setattr(self, name, ResponseElement(name=name)) |
| 48 elif name == self._action + 'Result': |
| 49 setattr(self, name, self._Result(name=name)) |
| 50 else: |
| 51 return ResponseElement.startElement(self, name, attrs, connection) |
| 52 return getattr(self, name) |
| 53 |
| 54 |
| 55 class ComplexAmount(ResponseElement): |
| 56 def __repr__(self): |
| 57 return '{0} {1}'.format(self.CurrencyCode, self.Value) |
| 58 |
| 59 def __float__(self): |
| 60 return float(self.Value) |
| 61 |
| 62 def __str__(self): |
| 63 return str(self.Value) |
| 64 |
| 65 def startElement(self, name, attrs, connection): |
| 66 if name not in ('CurrencyCode', 'Value'): |
| 67 message = 'Unrecognized tag {0} in ComplexAmount'.format(name) |
| 68 raise AssertionError(message) |
| 69 return ResponseElement.startElement(self, name, attrs, connection) |
| 70 |
| 71 def endElement(self, name, value, connection): |
| 72 if name == 'Value': |
| 73 value = Decimal(value) |
| 74 ResponseElement.endElement(self, name, value, connection) |
| 75 |
| 76 |
| 77 class AmountCollection(ResponseElement): |
| 78 def startElement(self, name, attrs, connection): |
| 79 setattr(self, name, ComplexAmount(name=name)) |
| 80 return getattr(self, name) |
| 81 |
| 82 |
| 83 class AccountBalance(AmountCollection): |
| 84 def startElement(self, name, attrs, connection): |
| 85 if name == 'AvailableBalances': |
| 86 setattr(self, name, AmountCollection(name=name)) |
| 87 return getattr(self, name) |
| 88 return AmountCollection.startElement(self, name, attrs, connection) |
| 89 |
| 90 |
| 91 class GetAccountBalanceResult(ResponseElement): |
| 92 def startElement(self, name, attrs, connection): |
| 93 if name == 'AccountBalance': |
| 94 setattr(self, name, AccountBalance(name=name)) |
| 95 return getattr(self, name) |
| 96 return Response.startElement(self, name, attrs, connection) |
| 97 |
| 98 |
| 99 class GetTotalPrepaidLiabilityResult(ResponseElement): |
| 100 def startElement(self, name, attrs, connection): |
| 101 if name == 'OutstandingPrepaidLiability': |
| 102 setattr(self, name, AmountCollection(name=name)) |
| 103 return getattr(self, name) |
| 104 return Response.startElement(self, name, attrs, connection) |
| 105 |
| 106 |
| 107 class GetPrepaidBalanceResult(ResponseElement): |
| 108 def startElement(self, name, attrs, connection): |
| 109 if name == 'PrepaidBalance': |
| 110 setattr(self, name, AmountCollection(name=name)) |
| 111 return getattr(self, name) |
| 112 return Response.startElement(self, name, attrs, connection) |
| 113 |
| 114 |
| 115 class GetOutstandingDebtBalanceResult(ResponseElement): |
| 116 def startElement(self, name, attrs, connection): |
| 117 if name == 'OutstandingDebt': |
| 118 setattr(self, name, AmountCollection(name=name)) |
| 119 return getattr(self, name) |
| 120 return Response.startElement(self, name, attrs, connection) |
| 121 |
| 122 |
| 123 class TransactionPart(ResponseElement): |
| 124 def startElement(self, name, attrs, connection): |
| 125 if name == 'FeesPaid': |
| 126 setattr(self, name, ComplexAmount(name=name)) |
| 127 return getattr(self, name) |
| 128 return ResponseElement.startElement(self, name, attrs, connection) |
| 129 |
| 130 |
| 131 class Transaction(ResponseElement): |
| 132 def __init__(self, *args, **kw): |
| 133 self.TransactionPart = [] |
| 134 ResponseElement.__init__(self, *args, **kw) |
| 135 |
| 136 def startElement(self, name, attrs, connection): |
| 137 if name == 'TransactionPart': |
| 138 getattr(self, name).append(TransactionPart(name=name)) |
| 139 return getattr(self, name)[-1] |
| 140 if name in ('TransactionAmount', 'FPSFees', 'Balance'): |
| 141 setattr(self, name, ComplexAmount(name=name)) |
| 142 return getattr(self, name) |
| 143 return ResponseElement.startElement(self, name, attrs, connection) |
| 144 |
| 145 |
| 146 class GetAccountActivityResult(ResponseElement): |
| 147 def __init__(self, *args, **kw): |
| 148 self.Transaction = [] |
| 149 ResponseElement.__init__(self, *args, **kw) |
| 150 |
| 151 def startElement(self, name, attrs, connection): |
| 152 if name == 'Transaction': |
| 153 getattr(self, name).append(Transaction(name=name)) |
| 154 return getattr(self, name)[-1] |
| 155 return ResponseElement.startElement(self, name, attrs, connection) |
| 156 |
| 157 |
| 158 class GetTransactionResult(ResponseElement): |
| 159 def startElement(self, name, attrs, connection): |
| 160 if name == 'Transaction': |
| 161 setattr(self, name, Transaction(name=name)) |
| 162 return getattr(self, name) |
| 163 return ResponseElement.startElement(self, name, attrs, connection) |
| 164 |
| 165 |
| 166 class GetTokensResult(ResponseElement): |
| 167 def __init__(self, *args, **kw): |
| 168 self.Token = [] |
| 169 ResponseElement.__init__(self, *args, **kw) |
| 170 |
| 171 def startElement(self, name, attrs, connection): |
| 172 if name == 'Token': |
| 173 getattr(self, name).append(ResponseElement(name=name)) |
| 174 return getattr(self, name)[-1] |
| 175 return ResponseElement.startElement(self, name, attrs, connection) |
OLD | NEW |