summaryrefslogtreecommitdiff
path: root/dealconvert/formats/bhg.py
blob: 7ff0704827d0311b88083d8351e7246f6b5547a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import sys

from . import DealFormat
from .. import dto

class BHGFormat(DealFormat):
    @property
    def suffix(self):
        return '.bhg'

    def parse_content(self, content):
        board_lines = [line.strip() for line in content.readlines()]
        deals = []
        for board_no, line in enumerate(board_lines):
            if board_no > 0:
                if len(line) != 52:
                    print 'WARNING: malformed .bhg line #%d: %s' % (board_no, line)
                elif not line.isalpha():
                    print 'WARNING: invalid characters in .bhg line #%d: %s' % (board_no, line)
                else:
                    deal = dto.Deal()
                    deal.number = board_no
                    deal.vulnerable = deal.get_vulnerability(board_no)
                    deal.dealer = deal.get_dealer(board_no)
                    hands = [[
                        ord(c) - 65 if ord(c) < 96 else ord(c) - 71
                        for c in list(
                            line[13*i:13*(i+1)]
                        )] for i in range(0, 4)
                    ]
                    for hand, cards in enumerate(hands):
                        for card in cards:
                            suit = card / 13
                            card = self.cards[card % 13]
                            deal.hands[(hand + deal.dealer) % 4][suit].append(card)
                    deals.append(deal)
        return deals

    def output_content(self, out_file, dealset):
        lines = [''] * (max([board.number for board in dealset])+2)
        for deal in dealset:
            line = ''
            for hand in range(0, 4):
                for i, suit in enumerate(deal.hands[(hand + deal.dealer) % 4]):
                    try:
                        cards = [13*i + self.cards.index(card) for card in suit]
                        for card in cards:
                            line += chr((65 if card < 26 else 71)+card)
                    except ValueError:
                        print 'ERROR: invalid suit %s in board #%d' % (''.join(suit), deal.number)
                        sys.exit()
            lines[deal.number] = line
        out_file.write('\r\n'.join(lines))