diff options
Diffstat (limited to 'dealconvert/formats/dlm.py')
-rw-r--r-- | dealconvert/formats/dlm.py | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/dealconvert/formats/dlm.py b/dealconvert/formats/dlm.py index 5dc385d..db9ac79 100644 --- a/dealconvert/formats/dlm.py +++ b/dealconvert/formats/dlm.py @@ -1,4 +1,4 @@ -import sys +import warnings from . import DealFormat from .. import dto @@ -11,31 +11,31 @@ class DLMFormat(DealFormat): def parse_content(self, content): lines = [line.strip() for line in content.readlines()] if lines[0] != '[Document]': - print 'ERROR: .dlm header not detected: %s' % (lines[0]) + raise RuntimeError('.dlm header not detected: %s' % (lines[0])) sys.exit() fields = {} for line in lines[1:]: try: fields[line.split('=')[0]] = line.split('=')[1] except IndexError: - print 'WARNING: unable to parse .dlm line: %s' % (line) + warnings.warn('unable to parse .dlm line: %s' % (line)) try: boards = range(int(fields['From board']), int(fields['To board'])+1) except (ValueError, IndexError): - print 'ERROR: unable to parse .dlm board number data' - sys.exit() + raise RuntimeError('unable to parse .dlm board number data') checksum = len(boards) if fields['Status'] == 'Show': checksum ^= 1 if checksum != int(fields['Checksum']): - print 'WARNING: .dlm checksum does not match: %d/%s' % ( - checksum, fields['Checksum']) + warnings.warn( + '.dlm checksum does not match: %d/%s' % ( + checksum, fields['Checksum'])) dealset = [] for board in boards: try: board_str = fields['Board %02d' % (board)] except IndexError: - print 'WARNING: board %d not found in .dlm' % (board) + warnings.warn('board %d not found in .dlm' % (board)) continue try: checksum = board @@ -47,8 +47,9 @@ class DLMFormat(DealFormat): values.append(value / 4) values.append(value % 4) if checksum != str_checksum: - print 'WARNING: .dlm board checksum mismatch: %s (%d)' % ( - board_str, checksum) + warnings.warn( + '.dlm board checksum mismatch: %s (%d)' % ( + board_str, checksum)) deal = dto.Deal() deal.number = board deal.dealer = deal.get_dealer(board) @@ -59,7 +60,7 @@ class DLMFormat(DealFormat): self.cards[card]) dealset.append(deal) except: - print 'WARNING: malformed .dlm data: %s' % (board_str) + warnings.warn('malformed .dlm data: %s' % (board_str)) return dealset def output_content(self, out_file, dealset): @@ -69,8 +70,8 @@ class DLMFormat(DealFormat): board_count = len(dealset) for board in range(first_board, first_board+board_count): if board not in board_numbers: - print 'ERROR: .dlm format requires consequent board numbers' - sys.exit() + raise RuntimeError( + '.dlm format requires consequent board numbers') header = [] header.append('[Document]') header.append('Headline=Generated by deal-converter.py') @@ -97,7 +98,7 @@ class DLMFormat(DealFormat): try: values[suit*13+self.cards.index(card)] = i except ValueError: - print 'ERROR: invalid card: %s' % (card) + raise RuntimeError('invalid card: %s' % (card)) line = 'Board %02d=' % (board) checksum = board for i in range(0, 26): |