summaryrefslogtreecommitdiff
path: root/dealconvert/formats/csv.py
blob: d0d8b28098ada3af8a02727a70fe6e5b965987f0 (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
from __future__ import absolute_import
import csv

from . import DealFormat
from .. import dto

class CSVFormat(DealFormat):
    @property
    def suffix(self):
        return '.csv'

    def parse_content(self, content):
        dealset = []
        for line in csv.reader(content):
            deal = dto.Deal()
            deal.number = int(line[16])
            dealers = {'N': dto.POSITION_NORTH,
                       'E': dto.POSITION_EAST,
                       'S': dto.POSITION_SOUTH,
                       'W': dto.POSITION_WEST}
            deal.dealer = dealers[line[17].split('/')[0]]
            for pair in ['NS', 'EW']:
                deal.vulnerable[pair] = line[17].split('/')[1] in [pair, 'All']
            for hand in range(0, 4):
                for suit in range(0, 4):
                    deal.hands[hand][suit] = list(line[hand*4+suit])
            dealset.append(deal)
        return dealset

    def output_content(self, out_file, dealset):
        writer = csv.writer(out_file, quoting=csv.QUOTE_ALL)
        for deal in dealset:
            line = []
            for hand in deal.hands:
                line += [''.join(suit) for suit in hand]
            line += [str(deal.number), '']
            line[17] = 'NESW'[deal.dealer] + '/'
            line[17] += ('All' if deal.vulnerable['EW'] else 'NS') \
                if deal.vulnerable['NS'] \
                   else ('EW' if deal.vulnerable['EW'] else '-')
            writer.writerow(line)