diff options
-rw-r--r-- | deal-converter.py | 3 | ||||
-rw-r--r-- | dealconvert/formats/stdout.py | 33 |
2 files changed, 35 insertions, 1 deletions
diff --git a/deal-converter.py b/deal-converter.py index 5a59b6a..e56c33a 100644 --- a/deal-converter.py +++ b/deal-converter.py @@ -7,7 +7,8 @@ parser = argparse.ArgumentParser( description='Universal converter for bridge deal formats', formatter_class=argparse.RawTextHelpFormatter, epilog='Supported formats: BER BHG BRI CDS CSV DGE DLM DUP PBN RZD.\n' + \ - 'Formats are auto-detected based on file extension.') + 'Formats are auto-detected based on file extension.\n' + \ + 'To display deals on STDOUT, provide "-" as an output file name.') parser.add_argument('input', metavar='INPUT_FILE', help='Input file path') parser.add_argument('output', metavar='OUTPUT_FILE', nargs='*', diff --git a/dealconvert/formats/stdout.py b/dealconvert/formats/stdout.py new file mode 100644 index 0000000..eb1aedd --- /dev/null +++ b/dealconvert/formats/stdout.py @@ -0,0 +1,33 @@ +from . import DealFormat + +class STDOUTFormat(DealFormat): + def match_file(self, filename): + return filename == '-' + + def parse(self, input_file): + return [] + + def output(self, output_file, deal): + width = 9 + for board in deal: + lines = [] + header = '%3d/%s' % ( + board.number, + ('All' if board.vulnerable['NS'] else 'EW') \ + if board.vulnerable['EW'] else \ + ('NS' if board.vulnerable['NS'] else '-')) + for suit in board.hands[0]: + suit = ''.join(suit) if len(suit) else '==' + lines.append(' ' * width + suit) + for idx, suit in enumerate(board.hands[3]): + suit = ''.join(suit) if len(suit) else '==' + suit = ('%-' + str(width) + 's') % (suit) + east_suit = ''.join(board.hands[1][idx]) \ + if len(board.hands[1][idx]) else '==' + lines.append(suit + ' ' * width + east_suit) + for suit in board.hands[2]: + suit = ''.join(suit) if len(suit) else '==' + lines.append(' ' * width + suit) + lines.append('') + lines[1] = header + lines[1][len(header):] + print '\n'.join(lines) |