diff options
author | emkael <emkael@tlen.pl> | 2019-05-23 01:42:08 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2019-05-23 01:42:08 +0200 |
commit | c5608c4cdb67aca4d154541a09d5ebb88bf232ec (patch) | |
tree | a3f9ecba1206c3bfe6aa936ce43f238b34d4c315 | |
parent | 3e2102f3038be32c85e98c2bd6a6023c63fae070 (diff) |
Proper error/warning handling
-rw-r--r-- | dealconvert/formats/ber.py | 21 | ||||
-rw-r--r-- | dealconvert/formats/bhg.py | 14 | ||||
-rw-r--r-- | dealconvert/formats/bri.py | 21 | ||||
-rw-r--r-- | dealconvert/formats/cds.py | 4 | ||||
-rw-r--r-- | dealconvert/formats/dge.py | 13 | ||||
-rw-r--r-- | dealconvert/formats/dlm.py | 29 | ||||
-rw-r--r-- | dealconvert/formats/dup.py | 15 | ||||
-rw-r--r-- | dealconvert/formats/rzd.py | 13 |
8 files changed, 70 insertions, 60 deletions
diff --git a/dealconvert/formats/ber.py b/dealconvert/formats/ber.py index b47e738..54ffd37 100644 --- a/dealconvert/formats/ber.py +++ b/dealconvert/formats/ber.py @@ -1,24 +1,24 @@ -import sys +import warnings from . import DealFormat from .. import dto class BERFormat(DealFormat): - number_warning = 'WARNING: .ber file format assumes consequent deal numbers from 1' + number_warning = '.ber file format assumes consequent deal numbers from 1' @property def suffix(self): return '.ber' def parse_content(self, content): - print self.number_warning + warnings.warn(self.number_warning) dealset = [] number = 1 while True: deal_str = content.read(52).strip() if len(deal_str) > 0: if len(deal_str) < 52: - print 'WARNING: truncated .ber input: %s' % (deal_str) + warnings.warn('truncated .ber input: %s' % (deal_str)) break deal = dto.Deal() deal.number = number @@ -29,8 +29,9 @@ class BERFormat(DealFormat): try: deal.hands[int(deal_str[suit*13 + card])-1][suit].append(self.cards[card]) except (IndexError, ValueError): - print 'ERROR: invalid character in .ber file: %s' % (deal_str[suit*13 + card]) - sys.exit() + raise RuntimeError( + 'invalid character in .ber file: %s' % ( + deal_str[suit*13 + card])) dealset.append(deal) number += 1 else: @@ -39,7 +40,7 @@ class BERFormat(DealFormat): def output_content(self, out_file, dealset): - print self.number_warning + warnings.warn(self.number_warning) for board in dealset: deal_str = [' '] * 52 for i, hand in enumerate(board.hands): @@ -48,7 +49,9 @@ class BERFormat(DealFormat): try: deal_str[j*13 + self.cards.index(card)] = str(i + 1) except ValueError: - print 'ERROR: invalid card character: %s' % (card) + raise RuntimeError( + 'invalid card character: %s' % (card)) if ' ' in deal_str: - print 'WARNING: not all cards present in board %d' % (board.number) + warnings.warn('not all cards present in board %d' % ( + board.number)) out_file.write(''.join(deal_str)) diff --git a/dealconvert/formats/bhg.py b/dealconvert/formats/bhg.py index 7ff0704..23a1673 100644 --- a/dealconvert/formats/bhg.py +++ b/dealconvert/formats/bhg.py @@ -1,4 +1,4 @@ -import sys +import warnings from . import DealFormat from .. import dto @@ -14,9 +14,12 @@ class BHGFormat(DealFormat): 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) + warnings.warn( + 'malformed .bhg line #%d: %s' % (board_no, line)) elif not line.isalpha(): - print 'WARNING: invalid characters in .bhg line #%d: %s' % (board_no, line) + warnings.warn( + 'invalid characters in .bhg line #%d: %s' % ( + board_no, line)) else: deal = dto.Deal() deal.number = board_no @@ -47,7 +50,8 @@ class BHGFormat(DealFormat): 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() + raise RuntimeError( + 'invalid suit %s in board #%d' % ( + ''.join(suit), deal.number)) lines[deal.number] = line out_file.write('\r\n'.join(lines)) diff --git a/dealconvert/formats/bri.py b/dealconvert/formats/bri.py index 56d0c2c..942c4e4 100644 --- a/dealconvert/formats/bri.py +++ b/dealconvert/formats/bri.py @@ -1,22 +1,24 @@ +import warnings + from . import DealFormat from .. import dto class BRIFormat(DealFormat): - number_warning = 'WARNING: .bri file format assumes consequent deal numbers from 1' + number_warning = '.bri file format assumes consequent deal numbers from 1' @property def suffix(self): return '.bri' def parse_content(self, content): - print self.number_warning + warnings.warn(self.number_warning) dealset = [] number = 1 while True: deal_str = content.read(128).strip() if len(deal_str) > 0: if len(deal_str) < 78: - print 'WARNING: truncated .bri input: %s' % (deal_str) + warning.warn('truncated .bri input: %s' % (deal_str)) break else: deal_obj = dto.Deal() @@ -35,8 +37,8 @@ class BRIFormat(DealFormat): try: deal = [int(deal_str[i*2:(i+1)*2], 10) for i in range(0, 39)] if max(deal) > 52: - print 'ERROR: invalid card in .bri file: %d' % (max(deal)) - sys.exit() + raise RuntimeError( + 'invalid card in .bri file: %d' % (max(deal))) for hand in range(0, 3): for card in deal[13*hand:13*(hand+1)]: card = card - 1 @@ -45,12 +47,11 @@ class BRIFormat(DealFormat): deal_obj.hands[hand][suit].append(self.cards[card]) deal_obj.fill_west() except ValueError: - print 'ERROR: invalid card in .bri file: %s' % (deal_str) - sys.exit() + raise RuntimeError('invalid card in .bri file: %s' % (deal_str)) return deal_obj.hands def output_content(self, out_file, dealset): - print self.number_warning + warnings.warn(self.number_warning) for deal in dealset: deal_str = self.single_deal_output(deal) deal_str += ' ' * 32 @@ -65,6 +66,6 @@ class BRIFormat(DealFormat): try: deal_str += '%02d' % (self.cards.index(card) + 13*i + 1) except ValueError: - print 'ERROR: invalid card character: %s' % (card) - sys.exit() + raise RuntimeError( + 'invalid card character: %s' % (card)) return deal_str diff --git a/dealconvert/formats/cds.py b/dealconvert/formats/cds.py index 4c2a15a..f624612 100644 --- a/dealconvert/formats/cds.py +++ b/dealconvert/formats/cds.py @@ -1,3 +1,5 @@ +import warnings + from . import DealFormat from .rzd import RZDFormat from .. import dto @@ -16,7 +18,7 @@ class CDSFormat(DealFormat): data = content.read(14) if len(data) < 14: if len(data) != 0: - print 'WARNING: .cds data truncated: %s' % (data) + warnings.warn('.cds data truncated: %s' % (data)) break deal = dto.Deal() deal.number = ord(data[0]) diff --git a/dealconvert/formats/dge.py b/dealconvert/formats/dge.py index 93eb971..d0a4c46 100644 --- a/dealconvert/formats/dge.py +++ b/dealconvert/formats/dge.py @@ -1,10 +1,10 @@ -import sys +import warnings from . import DealFormat from .. import dto class DGEFormat(DealFormat): - number_warning = 'WARNING: .dge file format assumes consequent deal numbers from 1' + number_warning = '.dge file format assumes consequent deal numbers from 1' suits = { chr(6): dto.SUIT_SPADES, chr(3): dto.SUIT_HEARTS, @@ -23,14 +23,14 @@ class DGEFormat(DealFormat): return '.dge' def parse_content(self, content): - print self.number_warning + warnings.warn(self.number_warning) dealset = [] number = 1 while True: deal_str = content.read(128).strip() if len(deal_str) > 0: if len(deal_str) < 68: - print 'WARNING: truncated .dge input: %s' % (deal_str) + warnings.warn('truncated .dge input: %s' % (deal_str)) break else: deal = dto.Deal() @@ -58,14 +58,13 @@ class DGEFormat(DealFormat): hand += 1 else: if suit is None: - print 'ERROR: invalid .dge line: %s' % (deal_str) - sys.exit() + raise RuntimeError('invalid .dge line: %s' % (deal_str)) else: deal.hands[hand][suit].append(char) return deal.hands def output_content(self, out_file, dealset): - print self.number_warning + warnings.warn(self.number_warning) for deal in dealset: deal_str = self.single_deal_output(deal) deal_str += chr(0) * 60 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): diff --git a/dealconvert/formats/dup.py b/dealconvert/formats/dup.py index 53b790b..1a2e1ba 100644 --- a/dealconvert/formats/dup.py +++ b/dealconvert/formats/dup.py @@ -1,4 +1,4 @@ -import sys +import warnings from . import DealFormat from .bri import BRIFormat @@ -20,18 +20,19 @@ class DUPFormat(DealFormat): boards.append(content.read(156)) if len(boards[-1]) < 156: if len(boards[-1]) > 0: - print 'WARNING: truncated .dup content: %s' % (boards[-1]) + warnings.warn('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() + raise RuntimeError('.dup file header not found') 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': - print 'WARNING: .dup file header has "reverse" flag set, nobody knows what to do with it, so it\'s time to panic' + warnings.warn( + '.dup file header has "reverse" flag set, ' + + 'nobody knows what to do with it, so it\'s time to panic') dealset = [] for idx, board in enumerate(boards): deal = dto.Deal() @@ -48,8 +49,8 @@ class DUPFormat(DealFormat): 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() + raise RuntimeError( + '.dup format requires consequent board numbers') header = 'YN%s 0 %02d ' % (str(first_board).ljust(2, ' '), board_count) for deal in dealset: out_file.write(self.bri.single_deal_output(deal)) diff --git a/dealconvert/formats/rzd.py b/dealconvert/formats/rzd.py index d6cbc5e..8ca1e97 100644 --- a/dealconvert/formats/rzd.py +++ b/dealconvert/formats/rzd.py @@ -1,10 +1,10 @@ -import sys +import warnings from . import DealFormat from .. import dto class RZDFormat(DealFormat): - number_warning = 'WARNING: .rzd file format assumes consequent deal numbers from 1' + number_warning = '.rzd file format assumes consequent deal numbers from 1' @property def suffix(self): @@ -20,7 +20,7 @@ class RZDFormat(DealFormat): return deal.hands def parse_content(self, content): - print self.number_warning + warnings.warn(self.number_warning) dealset = [] header = None number = 1 @@ -28,7 +28,7 @@ class RZDFormat(DealFormat): data = content.read(13) if len(data) < 13: if len(data) != 0: - print 'WARNING: .rzd data truncated: %s' % (data) + warnings.warn('.rzd data truncated: %s' % (data)) break if header is None: header = data @@ -51,8 +51,7 @@ class RZDFormat(DealFormat): try: idx = self.cards.index(card) except ValueError: - print 'ERROR: invalid card: %s' % (card) - sys.exit() + raise RuntimeError('invalid card: %s' % (card)) values[idx*4+suit] = (i + offset)%4 for i in range(0, 13): byte = 0 @@ -63,7 +62,7 @@ class RZDFormat(DealFormat): return value def output_content(self, out_file, dealset): - print self.number_warning + warnings.warn(self.number_warning) board_count = len(dealset) out_file.write(chr(board_count%256)) out_file.write(chr(board_count/256)) |