client-barcode: Split Status.commit() up into sub-functions

This commit is contained in:
Sebastian Pipping 2011-10-26 17:50:57 +02:00
parent 27962ad107
commit 156a0df88d
1 changed files with 51 additions and 46 deletions

View File

@ -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: