summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2019-05-22 01:10:17 +0200
committeremkael <emkael@tlen.pl>2019-05-22 01:10:17 +0200
commitaad1c523b501ad965aef8811f2f1c45dbe295983 (patch)
treef0f7eb0fd208e0f3fb6bc6a890961500f3e8d5a6
parente227c0167081b62fd9689e3f965fcb8dcfa3bc9d (diff)
DUP format
-rw-r--r--dealconvert/formats/dup.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/dealconvert/formats/dup.py b/dealconvert/formats/dup.py
index 081599a..5b78bfc 100644
--- a/dealconvert/formats/dup.py
+++ b/dealconvert/formats/dup.py
@@ -1,6 +1,60 @@
+import sys
+
from . import DealFormat
+from .bri import BRIFormat
+from .dge import DGEFormat
+from .. import dto
class DUPFormat(DealFormat):
@property
def suffix(self):
return '.dup'
+
+ def __init__(self):
+ self.bri = BRIFormat()
+ self.dge = DGEFormat()
+
+ def parse_content(self, content):
+ boards = []
+ while True:
+ boards.append(content.read(156))
+ if len(boards[-1]) < 156:
+ if len(boards[-1]) > 0:
+ print 'WARNING: truncated .dup content: %s' % (boards[-1])
+ boards = boards[0:-1]
+ break
+ boards = [(board[0:78], board[78:146], board[146:]) for board in boards]
+ if boards[0][2][0] == chr(0):
+ print 'ERROR: .dup file header not found'
+ sys.exit()
+ start_board = int(boards[0][2][2:4].strip())
+ board_count = int(boards[0][2][7:9].strip())
+ board_numbers = range(start_board, start_board+board_count)
+ if boards[0][2][1].upper() != 'N':
+ board_numbers.reverse()
+ dealset = []
+ for idx, board in enumerate(boards):
+ deal = dto.Deal()
+ deal.number = board_numbers[idx]
+ deal.dealer = deal.get_dealer(deal.number)
+ deal.vulnerable = deal.get_vulnerability(deal.number)
+ deal.hands = self.bri.parse_hands(board[0])
+ dealset.append(deal)
+ return dealset
+
+ def output_content(self, out_file, dealset):
+ board_numbers = [deal.number for deal in dealset]
+ first_board = min(board_numbers)
+ board_count = len(dealset)
+ for board in range(first_board, first_board+board_count):
+ if board not in board_numbers:
+ print 'ERROR: .dup format requires consequent board numbers'
+ sys.exit()
+ header = 'YN%s 0 %02d ' % (str(first_board).ljust(2, ' '), board_count)
+ for deal in sorted(dealset, key=lambda d:d.number):
+ out_file.write(self.bri.single_deal_output(deal))
+ out_file.write(self.dge.single_deal_output(deal))
+ if deal.number == first_board:
+ out_file.write(header)
+ else:
+ out_file.write(chr(0) * 10)