From 156a0df88d03096238f4f0e38144ca8e4612e349 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Wed, 26 Oct 2011 17:50:57 +0200 Subject: [PATCH] client-barcode: Split Status.commit() up into sub-functions --- client-barcode/freitagskasse.py | 97 +++++++++++++++++---------------- 1 file changed, 51 insertions(+), 46 deletions(-) diff --git a/client-barcode/freitagskasse.py b/client-barcode/freitagskasse.py index a723e42..8391be6 100644 --- a/client-barcode/freitagskasse.py +++ b/client-barcode/freitagskasse.py @@ -267,63 +267,68 @@ class Status: def commit(self): assert(self.logged_in()) - # Compress DepositCommands - dummy, initial_balance = self.transfers[0] - balance_before = initial_balance - compressed_deposit = DepositCommand(Decimal('0')) - others = list() - for (command, dummy) in list(self.transfers): - if isinstance(command, DepositCommand): - compressed_deposit.add(command) - else: - others.append((command, balance_before)) - balance_before += command.difference() - if compressed_deposit.difference() != 0: - others.append((compressed_deposit, balance_before)) - self.transfers = others + def compress_deposit_commands(): + dummy, initial_balance = self.transfers[0] + balance_before = initial_balance + compressed_deposit = DepositCommand(Decimal('0')) + others = list() + for (command, dummy) in list(self.transfers): + if isinstance(command, DepositCommand): + compressed_deposit.add(command) + else: + others.append((command, balance_before)) + balance_before += command.difference() + if compressed_deposit.difference() != 0: + others.append((compressed_deposit, balance_before)) + self.transfers = others - # Compress BuyCommands, use a single bulkbuy - dummy, initial_balance = self.transfers[0] - balance_before = initial_balance + def process_buy_commands_combined(): + # Compress BuyCommands, use a single bulkbuy + dummy, initial_balance = self.transfers[0] + balance_before = initial_balance + buy_commands = list() + non_buy_commands = list() + total_buy_diff = 0 - buy_commands = list() - non_buy_commands = list() - total_buy_diff = 0 + for command, dummy in self.transfers: + if isinstance(command, BuyCommand): + buy_commands.append(command) + else: + balance_before += command.difference() + non_buy_commands.append((command, balance_before)) - for command, dummy in self.transfers: - if isinstance(command, BuyCommand): - buy_commands.append(command) - else: - balance_before += command.difference() - non_buy_commands.append((command, balance_before)) - - try: - net.bulk_buy(buy_commands, self.login_name) - except urllib2.HTTPError as e: - myDisplay.display_screen("Server error",'Server Error: %s' % str(e)) - error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e)) - else: - self.transfers = non_buy_commands - - # Process remaining command queue - for (command, balance_backup) in list(self.transfers): try: - command.run(self.login_name) + net.bulk_buy(buy_commands, self.login_name) except urllib2.HTTPError as e: myDisplay.display_screen("Server error",'Server Error: %s' % str(e)) error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e)) - break else: - self.transfers.pop(0) + self.transfers = non_buy_commands - if not self.transfers: - # Show final balance for some time - clear() - self.dump() - delay('Logout', 3) + def process_commands(): + for (command, balance_backup) in list(self.transfers): + try: + command.run(self.login_name) + except urllib2.HTTPError as e: + myDisplay.display_screen("Server error",'Server Error: %s' % str(e)) + error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e)) + break + else: + self.transfers.pop(0) - self.logout() + def finish(): + if not self.transfers: + # Show final balance for some time + clear() + self.dump() + delay('Logout', 3) + self.logout() + + compress_deposit_commands() + process_buy_commands_combined() + process_commands() + finish() def find(self, barcode): try: