Initial stuff
authorBernard Blackham <[email protected]>
Thu, 24 Jun 2004 15:29:52 +0000 (15:29 +0000)
committerBernard Blackham <[email protected]>
Thu, 24 Jun 2004 15:29:52 +0000 (15:29 +0000)
.cvsignore [new file with mode: 0644]
BankAccount.py [new file with mode: 0644]
DispenseServer.py [new file with mode: 0755]

diff --git a/.cvsignore b/.cvsignore
new file mode 100644 (file)
index 0000000..43a3011
--- /dev/null
@@ -0,0 +1,2 @@
+*.pyc
+bank.pck
diff --git a/BankAccount.py b/BankAccount.py
new file mode 100644 (file)
index 0000000..ae33dd9
--- /dev/null
@@ -0,0 +1,64 @@
+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()
diff --git a/DispenseServer.py b/DispenseServer.py
new file mode 100755 (executable)
index 0000000..3c53c55
--- /dev/null
@@ -0,0 +1,16 @@
+#!/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()
+

UCC git Repository :: git.ucc.asn.au