From f05c577752de80a1bfed292951d1b7d7003e329c Mon Sep 17 00:00:00 2001 From: Bernard Blackham Date: Thu, 24 Jun 2004 15:29:52 +0000 Subject: [PATCH] Initial stuff --- .cvsignore | 2 ++ BankAccount.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++ DispenseServer.py | 16 ++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 .cvsignore create mode 100644 BankAccount.py create mode 100755 DispenseServer.py diff --git a/.cvsignore b/.cvsignore new file mode 100644 index 0000000..43a3011 --- /dev/null +++ b/.cvsignore @@ -0,0 +1,2 @@ +*.pyc +bank.pck diff --git a/BankAccount.py b/BankAccount.py new file mode 100644 index 0000000..ae33dd9 --- /dev/null +++ b/BankAccount.py @@ -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 index 0000000..3c53c55 --- /dev/null +++ b/DispenseServer.py @@ -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() + -- 2.20.1