--- /dev/null
+import pickle
+
+ACCOUNT_FILE = 'bank.pck'
+
+class BankAccount:
+ def __init__(self):
+ f = None
+ self.bank = {}
+ try:
+ f = open(ACCOUNT_FILE)
+ except IOError:
+ pass
+ if f != None:
+ self.bank = pickle.load(f)
+ self.sanity_check(self.bank)
+ f.close()
+ self.save()
+
+ def sanity_check_user(self, user):
+ wanted_fields = ['balance']
+ for f in user:
+ if wanted_fields.has_key(f):
+ if f == 'balance':
+ if not isinstance(user['balance'], tuple):
+ return False
+ if len(user['balance']) != 2:
+ return False
+ if not isinstance(user[0], int) or \
+ not isinstance(user[1], int):
+ return False
+ del wanted_fields[f]
+ else:
+ return False
+ if len(wanted_fields) != 0:
+ return False
+ return True
+
+ def sanity_check(self, bank):
+ for u in bank:
+ if not self.sanity_check_user(u):
+ return False
+ return True
+
+ def save(self):
+ f = open(ACCOUNT_FILE, 'w')
+ pickle.dump(self.bank, f)
+ f.close()
+
+ def ensure_user_exists(self, username):
+ if self.bank.has_key(username):
+ return
+ self.bank[username] = {}
+ self.bank[username]['balance'] = (0,0)
+
+ def get_balance(self, username):
+ if self.bank.has_key(username):
+ return self.bank['username']['balance']
+ return (0,0)
+
+ def add_amount(self, username, amount):
+ ensure_user_exists(username)
+ self.bank['username']['balance'][0] += amount[0]
+ self.bank['username']['balance'][1] += amount[1]
+ self.save()
--- /dev/null
+#!/usr/bin/python
+
+from SOAPpy import *
+from BankAccount import BankAccount
+
+namespace = 'Dispense'
+bind_port = 9900
+
+if __name__ == '__main__':
+ server = SOAPServer(('0.0.0.0', 9900))
+ bank = BankAccount()
+ for f in []:
+ server.registerFunction(f, namespace)
+ print "Serving ..."
+ server.serve_forever()
+