diff options
author | emkael <emkael@tlen.pl> | 2019-05-20 23:58:50 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2019-05-20 23:58:50 +0200 |
commit | 8bf8380af6db557420595128816d23212ebfe7f7 (patch) | |
tree | 40aae7a1978bc2e80be9b9e9ed78c321eb578df0 | |
parent | 627b3aa689674498068d45487f3325b488c32de0 (diff) |
PBN format support + framework for other formats
-rw-r--r-- | dealconvert/formats/__init__.py | 31 | ||||
-rw-r--r-- | dealconvert/formats/ber.py | 6 | ||||
-rw-r--r-- | dealconvert/formats/bhg.py | 6 | ||||
-rw-r--r-- | dealconvert/formats/bri.py | 6 | ||||
-rw-r--r-- | dealconvert/formats/cds.py | 6 | ||||
-rw-r--r-- | dealconvert/formats/csv.py | 6 | ||||
-rw-r--r-- | dealconvert/formats/dge.py | 6 | ||||
-rw-r--r-- | dealconvert/formats/dlm.py | 6 | ||||
-rw-r--r-- | dealconvert/formats/dup.py | 6 | ||||
-rw-r--r-- | dealconvert/formats/pbn.py | 101 | ||||
-rw-r--r-- | dealconvert/formats/rzd.py | 6 |
11 files changed, 186 insertions, 0 deletions
diff --git a/dealconvert/formats/__init__.py b/dealconvert/formats/__init__.py new file mode 100644 index 0000000..e442cea --- /dev/null +++ b/dealconvert/formats/__init__.py @@ -0,0 +1,31 @@ +from os.path import dirname, basename, isfile, join +import glob + +class DealFormat(object): + def parse(self, input_file): + with open(input_file, 'rb') as content: + return self.parse_content(content) + + def output(self, output_file, deal): + with open(output_file, 'wb') as out_file: + return self.output_content(out_file, deal) + + def match_file(self, filename): + return filename.lower().endswith(self.suffix) + + @property + def suffix(self): + pass + + def parse_content(self, content): + pass + + def output_content(self, out_file, deal): + pass + + +modules = glob.glob(join(dirname(__file__), "*.py")) +__all__ = [ + basename(f)[:-3] for f in modules + if isfile(f) and not f.endswith('__init__.py') +] diff --git a/dealconvert/formats/ber.py b/dealconvert/formats/ber.py new file mode 100644 index 0000000..0303407 --- /dev/null +++ b/dealconvert/formats/ber.py @@ -0,0 +1,6 @@ +from . import DealFormat + +class BERFormat(DealFormat): + @property + def suffix(self): + return '.ber' diff --git a/dealconvert/formats/bhg.py b/dealconvert/formats/bhg.py new file mode 100644 index 0000000..1542aa1 --- /dev/null +++ b/dealconvert/formats/bhg.py @@ -0,0 +1,6 @@ +from . import DealFormat + +class BHGFormat(DealFormat): + @property + def suffix(self): + return '.bhg' diff --git a/dealconvert/formats/bri.py b/dealconvert/formats/bri.py new file mode 100644 index 0000000..096de34 --- /dev/null +++ b/dealconvert/formats/bri.py @@ -0,0 +1,6 @@ +from . import DealFormat + +class BRIFormat(DealFormat): + @property + def suffix(self): + return '.bri' diff --git a/dealconvert/formats/cds.py b/dealconvert/formats/cds.py new file mode 100644 index 0000000..637e18f --- /dev/null +++ b/dealconvert/formats/cds.py @@ -0,0 +1,6 @@ +from . import DealFormat + +class CDSFormat(DealFormat): + @property + def suffix(self): + return '.cds' diff --git a/dealconvert/formats/csv.py b/dealconvert/formats/csv.py new file mode 100644 index 0000000..928d62e --- /dev/null +++ b/dealconvert/formats/csv.py @@ -0,0 +1,6 @@ +from . import DealFormat + +class CSVFormat(DealFormat): + @property + def suffix(self): + return '.csv' diff --git a/dealconvert/formats/dge.py b/dealconvert/formats/dge.py new file mode 100644 index 0000000..fa1720a --- /dev/null +++ b/dealconvert/formats/dge.py @@ -0,0 +1,6 @@ +from . import DealFormat + +class DGEFormat(DealFormat): + @property + def suffix(self): + return '.dge' diff --git a/dealconvert/formats/dlm.py b/dealconvert/formats/dlm.py new file mode 100644 index 0000000..7486a65 --- /dev/null +++ b/dealconvert/formats/dlm.py @@ -0,0 +1,6 @@ +from . import DealFormat + +class DLMFormat(DealFormat): + @property + def suffix(self): + return '.dlm' diff --git a/dealconvert/formats/dup.py b/dealconvert/formats/dup.py new file mode 100644 index 0000000..081599a --- /dev/null +++ b/dealconvert/formats/dup.py @@ -0,0 +1,6 @@ +from . import DealFormat + +class DUPFormat(DealFormat): + @property + def suffix(self): + return '.dup' diff --git a/dealconvert/formats/pbn.py b/dealconvert/formats/pbn.py new file mode 100644 index 0000000..e3fc384 --- /dev/null +++ b/dealconvert/formats/pbn.py @@ -0,0 +1,101 @@ +import re + +from . import DealFormat +from .. import dto + +class PBNField(object): + __slots__ = ['key', 'value'] + def __init__(self, key, value): + self.key = key + self.value = value + + field_regex = re.compile(r'\[(.*) "(.*)"\]') + + @staticmethod + def parse_line(line): + match = PBNField.field_regex.match(line) + if match: + return PBNField(key=match.group(1), value=match.group(2)) + return PBNField(None, line) + +class PBNDeal(object): + __slots__ = ['fields'] + + def parse(self, lines): + self.fields = [] + for line in lines: + self.fields.append(PBNField.parse_line(line)) + + def has_field(self, fieldname): + for field in self.fields: + if field.key == fieldname: + return True + return False + + def get_field(self, fieldname): + for field in self.fields: + if field.key == fieldname: + return field.value + return None + +class PBNFormat(DealFormat): + @property + def suffix(self): + return '.pbn' + + def parse_content(self, content): + lines = [line.strip() for line in content.readlines()] + deals = [] + current_deal = [] + for line in lines: + if len(line): + current_deal.append(line) + else: + deals.append(current_deal) + current_deal = [] + if len(current_deal): + deals.append(current_deal) + result = [] + for deal in deals: + deal_obj = PBNDeal() + deal_obj.parse(deal) + deal_dto = dto.Deal() + if deal_obj.has_field('Event'): + deal_dto.event = deal_obj.get_field('Event') + if deal_obj.has_field('Board'): + deal_dto.number = int(deal_obj.get_field('Board')) + dealers = {'N': dto.POSITION_NORTH, + 'E': dto.POSITION_EAST, + 'S': dto.POSITION_SOUTH, + 'W': dto.POSITION_WEST} + deal_dto.dealer = dealers[deal_obj.get_field('Dealer')] + vulnerability = deal_obj.get_field('Vulnerable') + for pair in deal_dto.vulnerable: + deal_dto.vulnerable[pair] = vulnerability in [pair, 'All'] + deal_parts = deal_obj.get_field('Deal').split(':') + dealer = dealers[deal_parts[0]] + hands = deal_parts[1].split(' ') + for hand in range(0, 4): + for i, suit in enumerate(hands[hand].split('.')): + deal_dto.hands[(hand + dealer) % 4][i] = list(suit) + result.append(deal_dto) + return result + + def output_content(self, out_file, deal): + for board in deal: + out_file.write('[Event "%s"]\r\n' % (board.event)) + out_file.write('[Board "%d"]\r\n' % (board.number)) + out_file.write('[Dealer "%s"]\r\n' % ( + 'NESW'[board.dealer] + )) + out_file.write('[Vulnerable "%s"]\r\n' % ( + ('All' if board.vulnerable['EW'] else 'NS') if + board.vulnerable['NS'] else + ('EW' if board.vulnerable['EW'] else 'None') + )) + out_file.write('[Deal "N:%s"]\r\n' % ( + ' '.join([ + '.'.join([''.join(suit) for suit in hand]) + for hand in board.hands + ]))) + out_file.write('\r\n') diff --git a/dealconvert/formats/rzd.py b/dealconvert/formats/rzd.py new file mode 100644 index 0000000..e663500 --- /dev/null +++ b/dealconvert/formats/rzd.py @@ -0,0 +1,6 @@ +from . import DealFormat + +class RZDFormat(DealFormat): + @property + def suffix(self): + return '.rzd' |