1#!/usr/bin/env python 2 3import argparse 4import sys 5 6def wrap(args): 7 l = args.input.readline(72) 8 firstline = True 9 while l: 10 if l[-1] == '\n': 11 if firstline: 12 outstr = "{}".format(l) 13 else: 14 outstr = " {}".format(l) 15 firstline = True 16 l = args.input.readline(72) 17 else: 18 if firstline: 19 outstr = "{:<71}*\n".format(l[:-1]) 20 firstline = False 21 else: 22 outstr = " {:<70}*\n".format(l[:-1]) 23 l = l[-1] + args.input.readline(70) 24 args.output.write(outstr) 25 26 return 0 27 28def unwrap(args): 29 l = args.input.readline() 30 firstline = True 31 while l: 32 if len(l) > 80: 33 print("Error: input line invalid (longer than 80 characters)", file=sys.stderr) 34 return 1 35 if not firstline and l[0] != ' ': 36 print("Error: continuation line not start with blank", file=sys.stderr) 37 return 1 38 39 if len(l) > 71 and l[71] == '*': 40 if firstline: 41 args.output.write(l[:71]) 42 firstline = False 43 else: 44 args.output.write(l[1:71]) 45 else: 46 if firstline: 47 args.output.write(l) 48 else: 49 args.output.write(l[1:]) 50 firstline = True 51 l = args.input.readline() 52 return 0 53 54def Main(): 55 parser = argparse.ArgumentParser(description="Wrap sidedeck source to card formats") 56 parser.add_argument("-u", "--unwrap", 57 help="Unwrap sidedeck cards to source formats instead", action="store_true", 58 default=False) 59 parser.add_argument("-i", "--input", help="input filename, default to stdin", 60 action="store", default=None) 61 parser.add_argument("-o", "--output", help="output filename, default to stdout", 62 action="store", default=None) 63 64 args = parser.parse_args() 65 66 if args.input is None: 67 args.input = sys.stdin 68 else: 69 args.input = open(args.input, 'r') 70 71 if args.output is None: 72 args.output = sys.stdout 73 else: 74 args.output = open(args.output, 'w') 75 76 if args.unwrap: 77 return unwrap(args) 78 79 return wrap(args) 80 81if __name__ == '__main__': 82 sys.exit(Main()) 83