|
4 | 4 | import sys
|
5 | 5 |
|
6 | 6 | def wrap(args):
|
7 |
| - firstline = True |
8 |
| - while (line := args.input.read(72)): |
9 |
| - if line.endswith('\n'): |
10 |
| - if firstline: |
11 |
| - outstr = f"{line}" |
12 |
| - else: |
13 |
| - outstr = f" {line}" |
14 |
| - firstline = True |
15 |
| - else: |
16 |
| - if firstline: |
17 |
| - outstr = f"{line:<71}*\n" |
18 |
| - firstline = False |
19 |
| - else: |
20 |
| - outstr = f" {line:<70}*\n" |
21 |
| - line = line[-1] + args.input.read(70) |
22 |
| - args.output.write(outstr) |
23 |
| - return 0 |
| 7 | + firstline = True |
| 8 | + while (line := args.input.read(72)): |
| 9 | + if line.endswith('\n'): |
| 10 | + if firstline: |
| 11 | + outstr = f"{line}" |
| 12 | + else: |
| 13 | + outstr = f" {line}" |
| 14 | + firstline = True |
| 15 | + else: |
| 16 | + if firstline: |
| 17 | + outstr = f"{line:<71}*\n" |
| 18 | + firstline = False |
| 19 | + else: |
| 20 | + outstr = f" {line:<70}*\n" |
| 21 | + line = line[-1] + args.input.read(70) |
| 22 | + args.output.write(outstr) |
| 23 | + return 0 |
24 | 24 |
|
25 | 25 | def unwrap(args):
|
26 |
| - firstline = True |
27 |
| - for line in args.input: |
28 |
| - if len(line) > 80: |
29 |
| - print("Error: input line invalid (longer than 80 characters)", file=sys.stderr) |
30 |
| - return 1 |
31 |
| - if not firstline and line[0] != ' ': |
32 |
| - print("Error: continuation line does not start with a blank", file=sys.stderr) |
33 |
| - return 1 |
| 26 | + firstline = True |
| 27 | + for line in args.input: |
| 28 | + if len(line) > 80: |
| 29 | + print("Error: input line invalid (longer than 80 characters)", file=sys.stderr) |
| 30 | + return 1 |
| 31 | + if not firstline and line[0] != ' ': |
| 32 | + print("Error: continuation line does not start with a blank", file=sys.stderr) |
| 33 | + return 1 |
34 | 34 |
|
35 |
| - if len(line) > 71 and line[71] == '*': |
36 |
| - if firstline: |
37 |
| - args.output.write(line[:71]) |
38 |
| - firstline = False |
39 |
| - else: |
40 |
| - args.output.write(line[1:71]) |
41 |
| - else: |
42 |
| - if firstline: |
43 |
| - args.output.write(line) |
44 |
| - else: |
45 |
| - args.output.write(line[1:]) |
46 |
| - firstline = True |
47 |
| - return 0 |
| 35 | + if len(line) > 71 and line[71] == '*': |
| 36 | + if firstline: |
| 37 | + args.output.write(line[:71]) |
| 38 | + firstline = False |
| 39 | + else: |
| 40 | + args.output.write(line[1:71]) |
| 41 | + else: |
| 42 | + if firstline: |
| 43 | + args.output.write(line) |
| 44 | + else: |
| 45 | + args.output.write(line[1:]) |
| 46 | + firstline = True |
| 47 | + return 0 |
48 | 48 |
|
49 | 49 | def main():
|
50 |
| - parser = argparse.ArgumentParser(description="Wrap sidedeck source to card formats") |
51 |
| - parser.add_argument("-u", "--unwrap", help="Unwrap sidedeck cards to source formats instead", action="store_true") |
52 |
| - parser.add_argument("-i", "--input", help="Input filename, defaults to stdin", default=None) |
53 |
| - parser.add_argument("-o", "--output", help="Output filename, defaults to stdout", default=None) |
| 50 | + parser = argparse.ArgumentParser(description="Wrap sidedeck source to card formats") |
| 51 | + parser.add_argument("-u", "--unwrap", help="Unwrap sidedeck cards to source formats instead", action="store_true") |
| 52 | + parser.add_argument("-i", "--input", help="Input filename, defaults to stdin", default=None) |
| 53 | + parser.add_argument("-o", "--output", help="Output filename, defaults to stdout", default=None) |
54 | 54 |
|
55 |
| - args = parser.parse_args() |
| 55 | + args = parser.parse_args() |
56 | 56 |
|
57 |
| - with open(args.input, 'r') if args.input else sys.stdin as infile, \ |
58 |
| - open(args.output, 'w') if args.output else sys.stdout as outfile: |
| 57 | + with open(args.input, 'r') if args.input else sys.stdin as infile, \ |
| 58 | + open(args.output, 'w') if args.output else sys.stdout as outfile: |
59 | 59 |
|
60 |
| - args.input = infile |
61 |
| - args.output = outfile |
| 60 | + args.input = infile |
| 61 | + args.output = outfile |
62 | 62 |
|
63 |
| - if args.unwrap: |
64 |
| - return unwrap(args) |
65 |
| - return wrap(args) |
| 63 | + if args.unwrap: |
| 64 | + return unwrap(args) |
| 65 | + return wrap(args) |
66 | 66 |
|
67 | 67 | if __name__ == '__main__':
|
68 |
| - sys.exit(main()) |
| 68 | + sys.exit(main()) |
0 commit comments