Browse Source

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

Sebastian Pipping 9 years ago
parent
commit
156a0df88d
1 changed files with 52 additions and 47 deletions
  1. 52
    47
      client-barcode/freitagskasse.py

+ 52
- 47
client-barcode/freitagskasse.py View File

@@ -267,63 +267,68 @@ class Status:
267 267
 	def commit(self):
268 268
 		assert(self.logged_in())
269 269
 
270
-		# Compress DepositCommands
271
-		dummy, initial_balance = self.transfers[0]
272
-		balance_before = initial_balance
273
-		compressed_deposit = DepositCommand(Decimal('0'))
274
-		others = list()
275
-		for (command, dummy) in list(self.transfers):
276
-			if isinstance(command, DepositCommand):
277
-				compressed_deposit.add(command)
278
-			else:
279
-				others.append((command, balance_before))
280
-				balance_before += command.difference()
281
-		if compressed_deposit.difference() != 0:
282
-			others.append((compressed_deposit, balance_before))
283
-		self.transfers = others
284
-
285
-		# Compress BuyCommands, use a single bulkbuy
286
-		dummy, initial_balance = self.transfers[0]
287
-		balance_before = initial_balance
288
-
289
-		buy_commands = list()
290
-		non_buy_commands = list()
291
-		total_buy_diff = 0
292
-
293
-		for command, dummy in self.transfers:
294
-			if isinstance(command, BuyCommand):
295
-				buy_commands.append(command)
296
-			else:
297
-				balance_before += command.difference()
298
-				non_buy_commands.append((command, balance_before))
270
+		def compress_deposit_commands():
271
+			dummy, initial_balance = self.transfers[0]
272
+			balance_before = initial_balance
273
+			compressed_deposit = DepositCommand(Decimal('0'))
274
+			others = list()
275
+			for (command, dummy) in list(self.transfers):
276
+				if isinstance(command, DepositCommand):
277
+					compressed_deposit.add(command)
278
+				else:
279
+					others.append((command, balance_before))
280
+					balance_before += command.difference()
281
+			if compressed_deposit.difference() != 0:
282
+				others.append((compressed_deposit, balance_before))
283
+			self.transfers = others
284
+
285
+		def process_buy_commands_combined():
286
+			# Compress BuyCommands, use a single bulkbuy
287
+			dummy, initial_balance = self.transfers[0]
288
+			balance_before = initial_balance
289
+			buy_commands = list()
290
+			non_buy_commands = list()
291
+			total_buy_diff = 0
299 292
 
300
-		try:
301
-			net.bulk_buy(buy_commands, self.login_name)
302
-		except urllib2.HTTPError as e:
303
-			myDisplay.display_screen("Server error",'Server Error: %s' % str(e))
304
-			error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e))
305
-		else:
306
-			self.transfers = non_buy_commands
293
+			for command, dummy in self.transfers:
294
+				if isinstance(command, BuyCommand):
295
+					buy_commands.append(command)
296
+				else:
297
+					balance_before += command.difference()
298
+					non_buy_commands.append((command, balance_before))
307 299
 
308
-		# Process remaining command queue
309
-		for (command, balance_backup) in list(self.transfers):
310 300
 			try:
311
-				command.run(self.login_name)
301
+				net.bulk_buy(buy_commands, self.login_name)
312 302
 			except urllib2.HTTPError as e:
313 303
 				myDisplay.display_screen("Server error",'Server Error: %s' % str(e))
314 304
 				error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e))
315
-				break
316 305
 			else:
317
-				self.transfers.pop(0)
306
+				self.transfers = non_buy_commands
318 307
 
319
-		if not self.transfers:
320
-			# Show final balance for some time
321
-			clear()
322
-			self.dump()
323
-			delay('Logout', 3)
308
+		def process_commands():
309
+			for (command, balance_backup) in list(self.transfers):
310
+				try:
311
+					command.run(self.login_name)
312
+				except urllib2.HTTPError as e:
313
+					myDisplay.display_screen("Server error",'Server Error: %s' % str(e))
314
+					error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e))
315
+					break
316
+				else:
317
+					self.transfers.pop(0)
318
+
319
+		def finish():
320
+			if not self.transfers:
321
+				# Show final balance for some time
322
+				clear()
323
+				self.dump()
324
+				delay('Logout', 3)
324 325
 
325
-			self.logout()
326
+				self.logout()
326 327
 
328
+		compress_deposit_commands()
329
+		process_buy_commands_combined()
330
+		process_commands()
331
+		finish()
327 332
 
328 333
 	def find(self, barcode):
329 334
 		try:

Loading…
Cancel
Save