diff options
Diffstat (limited to 'dealconvert/formats')
-rw-r--r-- | dealconvert/formats/__init__.py | 26 | ||||
-rw-r--r-- | dealconvert/formats/bhg.py | 2 | ||||
-rw-r--r-- | dealconvert/formats/bri.py | 2 | ||||
-rw-r--r-- | dealconvert/formats/cds.py | 11 | ||||
-rw-r--r-- | dealconvert/formats/csv.py | 1 | ||||
-rw-r--r-- | dealconvert/formats/dge.py | 2 | ||||
-rw-r--r-- | dealconvert/formats/dlm.py | 10 | ||||
-rw-r--r-- | dealconvert/formats/dup.py | 2 | ||||
-rw-r--r-- | dealconvert/formats/html.py | 18 | ||||
-rw-r--r-- | dealconvert/formats/lin.py | 4 | ||||
-rw-r--r-- | dealconvert/formats/pbn.py | 2 | ||||
-rw-r--r-- | dealconvert/formats/pdf.py | 6 | ||||
-rw-r--r-- | dealconvert/formats/rzd.py | 21 | ||||
-rw-r--r-- | dealconvert/formats/stdout.py | 2 |
14 files changed, 66 insertions, 43 deletions
diff --git a/dealconvert/formats/__init__.py b/dealconvert/formats/__init__.py index 1d8b460..fc19280 100644 --- a/dealconvert/formats/__init__.py +++ b/dealconvert/formats/__init__.py @@ -8,20 +8,34 @@ class DealFormat(object): self.interactive = interactive self.options = kwargs + def file_to_read(self, input_path): + return open(input_path, 'r') + + def file_to_write(self, output_path): + return open(output_path, 'w') + def parse(self, input_file): - with open(input_file, 'rb') as content: + with self.file_to_read(input_file) as content: return self.parse_content(content) def output(self, output_file, deal, analyze=False): self.analyze = analyze if not len(deal): raise RuntimeError('Dealset is empty') - with open(output_file, 'wb') as out_file: + with self.file_to_write(output_file) as out_file: return self.output_content(out_file, deal) def match_file(self, filename): return filename.lower().endswith(self.suffix) + def parse_byte(self, byte): + try: + converted_byte = ord(byte) + byte = converted_byte + except TypeError: + converted_byte = byte # in Python3, ord() is not needed + return converted_byte + @property def suffix(self): pass @@ -33,6 +47,14 @@ class DealFormat(object): pass +class BinaryFormat(DealFormat): + def file_to_read(self, input_path): + return open(input_path, 'rb') + + def file_to_write(self, output_path): + return open(output_path, 'wb') + + modules = glob.glob(join(dirname(__file__), "*.py")) __all__ = [ basename(f)[:-3] for f in modules diff --git a/dealconvert/formats/bhg.py b/dealconvert/formats/bhg.py index 6964ea3..5737a8e 100644 --- a/dealconvert/formats/bhg.py +++ b/dealconvert/formats/bhg.py @@ -33,7 +33,7 @@ class BHGFormat(DealFormat): ] for hand, cards in enumerate(hands): for card in cards: - suit = card / 13 + suit = card // 13 card = self.cards[card % 13] deal.hands[(hand + deal.dealer) % 4][suit].append(card) deals.append(deal) diff --git a/dealconvert/formats/bri.py b/dealconvert/formats/bri.py index 1a4cc0f..55163fe 100644 --- a/dealconvert/formats/bri.py +++ b/dealconvert/formats/bri.py @@ -42,7 +42,7 @@ class BRIFormat(DealFormat): for hand in range(0, 3): for card in deal[13*hand:13*(hand+1)]: card = card - 1 - suit = card / 13 + suit = card // 13 card = card % 13 deal_obj.hands[hand][suit].append(self.cards[card]) deal_obj.fill_west() diff --git a/dealconvert/formats/cds.py b/dealconvert/formats/cds.py index 6137c4d..34ce46f 100644 --- a/dealconvert/formats/cds.py +++ b/dealconvert/formats/cds.py @@ -1,10 +1,10 @@ import warnings -from . import DealFormat +from . import BinaryFormat from .rzd import RZDFormat from .. import dto -class CDSFormat(DealFormat): +class CDSFormat(BinaryFormat): @property def suffix(self): return '.cds' @@ -21,15 +21,14 @@ class CDSFormat(DealFormat): warnings.warn('.cds data truncated: %s' % (data)) break deal = dto.Deal() - deal.number = ord(data[0]) + deal.number = self.parse_byte(data[0]) deal.dealer = deal.get_dealer(deal.number) deal.vulnerable = deal.get_vulnerability(deal.number) deal.hands = self.rzd_format.parse_deal(data[1:], offset=1) dealset.append(deal) return dealset - def output_content(self, out_file, dealset): for deal in dealset: - out_file.write(chr(deal.number)) - out_file.write(self.rzd_format.dump_deal(deal, offset=1)) + out_file.write(bytearray([deal.number])) + out_file.write(bytearray(self.rzd_format.dump_deal(deal, offset=1))) diff --git a/dealconvert/formats/csv.py b/dealconvert/formats/csv.py index d0d8b28..633720a 100644 --- a/dealconvert/formats/csv.py +++ b/dealconvert/formats/csv.py @@ -1,4 +1,5 @@ from __future__ import absolute_import + import csv from . import DealFormat diff --git a/dealconvert/formats/dge.py b/dealconvert/formats/dge.py index d0a4c46..d180d33 100644 --- a/dealconvert/formats/dge.py +++ b/dealconvert/formats/dge.py @@ -13,7 +13,7 @@ class DGEFormat(DealFormat): } def suit_indicator(self, the_suit): - for indicator, suit in self.suits.iteritems(): + for indicator, suit in self.suits.items(): if suit == the_suit: return indicator return None diff --git a/dealconvert/formats/dlm.py b/dealconvert/formats/dlm.py index 56f6e5d..f1e27c8 100644 --- a/dealconvert/formats/dlm.py +++ b/dealconvert/formats/dlm.py @@ -20,7 +20,7 @@ class DLMFormat(DealFormat): except IndexError: warnings.warn('unable to parse .dlm line: %s' % (line)) try: - boards = range(int(fields['From board']), int(fields['To board'])+1) + boards = list(range(int(fields['From board']), int(fields['To board'])+1)) except (ValueError, IndexError): raise RuntimeError('unable to parse .dlm board number data') checksum = len(boards) @@ -42,9 +42,9 @@ class DLMFormat(DealFormat): str_checksum = int(board_str[26:]) values = [] for char in board_str[0:26]: - checksum ^= ord(char) - value = ord(char) - 97 - values.append(value / 4) + checksum ^= self.parse_byte(char) + value = self.parse_byte(char) - 97 + values.append(value // 4) values.append(value % 4) if checksum != str_checksum: warnings.warn( @@ -84,7 +84,7 @@ class DLMFormat(DealFormat): header.append('Crypto key=0') header.append('Checksum=%d' % (board_count^1)) lines = ['', ''] * 99 - for i in range(1, first_board) + range(first_board+board_count, 100): + for i in list(range(1, first_board)) + list(range(first_board+board_count, 100)): lines[(i-1)*2] = 'Duplicates %02d=0' % (i) lines[(i-1)*2+1] = 'Board %02d=aaaaaabffffffkkkkkklpppppp%03d' % ( i, i^14) diff --git a/dealconvert/formats/dup.py b/dealconvert/formats/dup.py index 3e34f2c..05b634e 100644 --- a/dealconvert/formats/dup.py +++ b/dealconvert/formats/dup.py @@ -28,7 +28,7 @@ class DUPFormat(DealFormat): 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) + board_numbers = list(range(start_board, start_board+board_count)) if boards[0][2][1].upper() != 'N': warnings.warn( '.dup file header has "reverse" flag set, ' + diff --git a/dealconvert/formats/html.py b/dealconvert/formats/html.py index 71ed008..c4325fd 100644 --- a/dealconvert/formats/html.py +++ b/dealconvert/formats/html.py @@ -7,15 +7,15 @@ from bcdd.Exceptions import FieldNotFoundException from bcdd.ParScore import ParScore from bcdd.PBNBoard import PBNBoard -from . import DealFormat +from . import BinaryFormat from .. import dto HTML_SUITS = OrderedDict([ - ('s', u'\u2660'), - ('h', u'\u2665'), - ('d', u'\u2666'), - ('c', u'\u2663') + ('s', '\u2660'), + ('h', '\u2665'), + ('d', '\u2666'), + ('c', '\u2663') ]) _page_template = ''' @@ -70,20 +70,20 @@ thead { </html> ''' -class HTMLFormat(DealFormat): +class HTMLFormat(BinaryFormat): @property def suffix(self): return '.html' def __init__(self, *args, **kwargs): - DealFormat.__init__(self, *args, **kwargs) + BinaryFormat.__init__(self, *args, **kwargs) self.deals_per_column = self.options.get('columns', 6) or 6 def parse_content(self, content): raise NotImplementedError def _get_html_hands(self, deal): - suits = HTML_SUITS.keys() + suits = list(HTML_SUITS.keys()) return [ [ self._get_html_suit(suits[i], suit) @@ -141,7 +141,7 @@ class HTMLFormat(DealFormat): data['table'] += '<td>' + self._get_suit_symbol(suit) + '</td>' data['table'] += '</tr>' player_order = [0, 2, 1, 3] # NESW -> NSEW - denom_order = range(4, -1, -1) # CDHSN -> NSHDC + denom_order = list(range(4, -1, -1)) # CDHSN -> NSHDC for player in player_order: data['table'] += '<tr>' data['table'] += '<td style="border-left: solid 1px black">' + BCalcWrapper.PLAYERS[player] + '</td>' diff --git a/dealconvert/formats/lin.py b/dealconvert/formats/lin.py index 91a6b3d..43c5ac3 100644 --- a/dealconvert/formats/lin.py +++ b/dealconvert/formats/lin.py @@ -48,9 +48,9 @@ class LINFormat(DealFormat): raise RuntimeError('multiple boards #%d with different layouts' % (deal_number)) deals[deal_number] = layout else: - print 'layout not parsed: ' + fields['md'][0] + print('layout not parsed: ' + fields['md'][0]) dealset = [] - for number, layout in deals.iteritems(): + for number, layout in deals.items(): deal = dto.Deal() deal.event = event deal.number = number diff --git a/dealconvert/formats/pbn.py b/dealconvert/formats/pbn.py index 11295f9..5c5e769 100644 --- a/dealconvert/formats/pbn.py +++ b/dealconvert/formats/pbn.py @@ -154,4 +154,4 @@ class PBNFormat(DealFormat): except Exception as e: warnings.warn('unable to determine double-dummy data: %s' % e) for line in lines + ['']: - out_file.write(line + '\r\n') + out_file.write((line + '\r\n')) diff --git a/dealconvert/formats/pdf.py b/dealconvert/formats/pdf.py index 9a4f6ca..df7a66c 100644 --- a/dealconvert/formats/pdf.py +++ b/dealconvert/formats/pdf.py @@ -3,18 +3,18 @@ import warnings import pdfkit -from . import DealFormat +from . import BinaryFormat from .html import HTMLFormat from .. import dto -class PDFFormat(DealFormat): +class PDFFormat(BinaryFormat): @property def suffix(self): return '.pdf' def __init__(self, *args, **kwargs): - DealFormat.__init__(self, *args, **kwargs) + BinaryFormat.__init__(self, *args, **kwargs) self.html_formatter = HTMLFormat(*args, **kwargs) def parse_content(self, content): diff --git a/dealconvert/formats/rzd.py b/dealconvert/formats/rzd.py index 93dbc45..2a15b55 100644 --- a/dealconvert/formats/rzd.py +++ b/dealconvert/formats/rzd.py @@ -1,9 +1,9 @@ import warnings -from . import DealFormat +from . import BinaryFormat from .. import dto -class RZDFormat(DealFormat): +class RZDFormat(BinaryFormat): number_warning = '.rzd file format assumes consequent deal numbers from 1' @property @@ -13,10 +13,10 @@ class RZDFormat(DealFormat): def parse_deal(self, data, offset=0): deal = dto.Deal() for card, byte in enumerate(data): - byte = ord(byte) + byte = self.parse_byte(byte) for suit in range(3, -1, -1): deal.hands[(byte%4 - offset)%4][suit].append(self.cards[card]) - byte /= 4 + byte //= 4 return deal.hands def parse_content(self, content): @@ -43,7 +43,7 @@ class RZDFormat(DealFormat): return dealset def dump_deal(self, deal, offset=0): - value = '' + value = [] values = [None] * 52 for i, hand in enumerate(deal.hands): for suit, cards in enumerate(hand): @@ -60,14 +60,15 @@ class RZDFormat(DealFormat): raise RuntimeError('missing card: %s%s in board %d' % ('SHDC'[j], self.cards[i], deal.number)) byte *= 4 byte += values[4*i+j] - value += chr(byte) + value.append(byte) + print(value) return value def output_content(self, out_file, dealset): warnings.warn(self.number_warning) board_count = len(dealset) - out_file.write(chr(board_count%256)) - out_file.write(chr(board_count/256)) - out_file.write(' '*11) + out_file.write(bytearray([board_count%256])) + out_file.write(bytearray([board_count//256])) + out_file.write((' '*11).encode()) for deal in dealset: - out_file.write(self.dump_deal(deal)) + out_file.write(bytearray(self.dump_deal(deal))) diff --git a/dealconvert/formats/stdout.py b/dealconvert/formats/stdout.py index 9725c06..1452dde 100644 --- a/dealconvert/formats/stdout.py +++ b/dealconvert/formats/stdout.py @@ -1,4 +1,4 @@ -from __future__ import print_function + from . import DealFormat |