diff options
author | emkael <emkael@tlen.pl> | 2019-05-21 23:04:58 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2019-05-21 23:04:58 +0200 |
commit | 53e0ca6044da8a2256a1561751273d1a9c8fec01 (patch) | |
tree | a63f946366a12cc947c82aecda6ee69e914a2228 | |
parent | daca39dbb082ef68322bb568a5066dacbb2de945 (diff) |
BRI format
-rw-r--r-- | dealconvert/formats/bri.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/dealconvert/formats/bri.py b/dealconvert/formats/bri.py index 096de34..bb87d9d 100644 --- a/dealconvert/formats/bri.py +++ b/dealconvert/formats/bri.py @@ -1,6 +1,61 @@ from . import DealFormat +from .. import dto class BRIFormat(DealFormat): + number_warning = 'WARNING: .bri file format assumes consequent deal numbers from 1' + @property def suffix(self): return '.bri' + + def parse_content(self, content): + print self.number_warning + dealset = [] + number = 1 + while True: + deal_str = content.read(128).strip() + if len(deal_str) > 0: + if len(deal_str) < 78: + print 'WARNING: truncated .bri input: %s' % (deal_str) + break + else: + try: + deal = [int(deal_str[i*2:(i+1)*2], 10) for i in range(0, 39)] + deal_obj = dto.Deal() + deal_obj.number = number + deal_obj.dealer = deal_obj.get_dealer(number) + deal_obj.vulnerable = deal_obj.get_vulnerability(number) + for hand in range(0, 3): + for card in deal[13*hand:13*(hand+1)]: + card = card - 1 + suit = card / 13 + card = card % 13 + deal_obj.hands[hand][suit].append(self.cards[card]) + deal_obj.fill_west() + dealset.append(deal_obj) + number += 1 + except ValueError: + print 'ERROR: invalid card in .bri file: %s' % (deal_str) + sys.exit() + if max(deal) > 52: + print 'ERROR: invalid card in .bri file: %d' % (max(deal)) + sys.exit() + else: + break + return dealset + + def output_content(self, out_file, dealset): + print self.number_warning + for deal in dealset: + deal_str = '' + for hand in deal.hands[0:3]: + for i, suit in enumerate(hand): + for card in suit: + try: + deal_str += '%02d' % (self.cards.index(card) + 13*i + 1) + except ValueError: + print 'ERROR: invalid card character: %s' % (card) + sys.exit() + deal_str += ' ' * 32 + deal_str += chr(0) * 18 + out_file.write(deal_str) |