summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2019-05-21 21:34:10 +0200
committeremkael <emkael@tlen.pl>2019-05-21 21:34:45 +0200
commit167b4cbc50e28209b18b04d738565590715a92ff (patch)
tree58c4343209f989a379d5eba4fa6bddc6f02c8518
parentd10d3dd8967c76ce34d61bf2ea980487549de4d8 (diff)
Error handling in BER format
-rw-r--r--dealconvert/formats/ber.py19
1 files changed, 15 insertions, 4 deletions
diff --git a/dealconvert/formats/ber.py b/dealconvert/formats/ber.py
index 66c289e..d20a0ff 100644
--- a/dealconvert/formats/ber.py
+++ b/dealconvert/formats/ber.py
@@ -1,3 +1,5 @@
+import sys
+
from . import DealFormat
from .. import dto
@@ -14,7 +16,7 @@ class BERFormat(DealFormat):
dealset = []
number = 1
while True:
- deal_str = content.read(52)
+ deal_str = content.read(52).strip()
if len(deal_str) > 0:
if len(deal_str) < 52:
print 'WARNING: truncated .ber input: %s' % (deal_str)
@@ -25,7 +27,11 @@ class BERFormat(DealFormat):
deal.vulnerable = deal.get_vulnerability(number)
for suit in range(0, 4):
for card in range(0, 13):
- deal.hands[int(deal_str[suit*13 + card])-1][suit].append(self.cards[card])
+ try:
+ deal.hands[int(deal_str[suit*13 + card])-1][suit].append(self.cards[card])
+ except (IndexError, ValueError):
+ print 'ERROR: invalid character in .ber file: %s' % (deal_str[suit*13 + card])
+ sys.exit()
dealset.append(deal)
number += 1
else:
@@ -36,9 +42,14 @@ class BERFormat(DealFormat):
def output_content(self, out_file, dealset):
print self.number_warning
for board in dealset:
- deal_str = [''] * 52
+ deal_str = [' '] * 52
for i, hand in enumerate(board.hands):
for j, suit in enumerate(hand):
for card in suit:
- deal_str[j*13 + self.cards.index(card)] = str(i + 1)
+ try:
+ deal_str[j*13 + self.cards.index(card)] = str(i + 1)
+ except ValueError:
+ print 'ERROR: invalid card character: %s' % (card)
+ if ' ' in deal_str:
+ print 'WARNING: not all cards present in board %d' % (board.number)
out_file.write(''.join(deal_str))