summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2019-05-21 23:24:28 +0200
committeremkael <emkael@tlen.pl>2019-05-21 23:24:28 +0200
commitcf467ce1245ac47f8bea4691c9574ed9a7fe8ff1 (patch)
treec2bc5738b00efb25c7cd556c367b4a8936dcb5ed
parent53e0ca6044da8a2256a1561751273d1a9c8fec01 (diff)
CSV format support
-rw-r--r--dealconvert/formats/csv.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/dealconvert/formats/csv.py b/dealconvert/formats/csv.py
index 928d62e..d0d8b28 100644
--- a/dealconvert/formats/csv.py
+++ b/dealconvert/formats/csv.py
@@ -1,6 +1,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)