From 88d0e1dbdd7e46ceb3d44e053435613472120521 Mon Sep 17 00:00:00 2001 From: Aviv Keller <38299977+RedYetiDev@users.noreply.github.com> Date: Mon, 12 Aug 2024 20:15:38 -0400 Subject: [PATCH 1/2] tools: update `sdwrap.py` to use Python3.8 features --- tools/zos/sdwrap.py | 120 +++++++++++++++++++------------------------- 1 file changed, 53 insertions(+), 67 deletions(-) diff --git a/tools/zos/sdwrap.py b/tools/zos/sdwrap.py index 5253d945f40892..1acc93e2ba83ad 100755 --- a/tools/zos/sdwrap.py +++ b/tools/zos/sdwrap.py @@ -1,82 +1,68 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import argparse import sys def wrap(args): - l = args.input.readline(72) - firstline = True - while l: - if l[-1] == '\n': - if firstline: - outstr = "{}".format(l) - else: - outstr = " {}".format(l) - firstline = True - l = args.input.readline(72) - else: - if firstline: - outstr = "{:<71}*\n".format(l[:-1]) - firstline = False - else: - outstr = " {:<70}*\n".format(l[:-1]) - l = l[-1] + args.input.readline(70) - args.output.write(outstr) - - return 0 + firstline = True + while (line := args.input.read(72)): + if line.endswith('\n'): + if firstline: + outstr = f"{line}" + else: + outstr = f" {line}" + firstline = True + else: + if firstline: + outstr = f"{line:<71}*\n" + firstline = False + else: + outstr = f" {line:<70}*\n" + line = line[-1] + args.input.read(70) + args.output.write(outstr) + return 0 def unwrap(args): - l = args.input.readline() - firstline = True - while l: - if len(l) > 80: - print("Error: input line invalid (longer than 80 characters)", file=sys.stderr) - return 1 - if not firstline and l[0] != ' ': - print("Error: continuation line not start with blank", file=sys.stderr) - return 1 - - if len(l) > 71 and l[71] == '*': - if firstline: - args.output.write(l[:71]) - firstline = False - else: - args.output.write(l[1:71]) - else: - if firstline: - args.output.write(l) - else: - args.output.write(l[1:]) - firstline = True - l = args.input.readline() - return 0 + firstline = True + for line in args.input: + if len(line) > 80: + print("Error: input line invalid (longer than 80 characters)", file=sys.stderr) + return 1 + if not firstline and line[0] != ' ': + print("Error: continuation line does not start with a blank", file=sys.stderr) + return 1 -def Main(): - parser = argparse.ArgumentParser(description="Wrap sidedeck source to card formats") - parser.add_argument("-u", "--unwrap", - help="Unwrap sidedeck cards to source formats instead", action="store_true", - default=False) - parser.add_argument("-i", "--input", help="input filename, default to stdin", - action="store", default=None) - parser.add_argument("-o", "--output", help="output filename, default to stdout", - action="store", default=None) + if len(line) > 71 and line[71] == '*': + if firstline: + args.output.write(line[:71]) + firstline = False + else: + args.output.write(line[1:71]) + else: + if firstline: + args.output.write(line) + else: + args.output.write(line[1:]) + firstline = True + return 0 - args = parser.parse_args() +def main(): + parser = argparse.ArgumentParser(description="Wrap sidedeck source to card formats") + parser.add_argument("-u", "--unwrap", help="Unwrap sidedeck cards to source formats instead", action="store_true") + parser.add_argument("-i", "--input", help="Input filename, defaults to stdin", default=None) + parser.add_argument("-o", "--output", help="Output filename, defaults to stdout", default=None) - if args.input is None: - args.input = sys.stdin - else: - args.input = open(args.input, 'r') + args = parser.parse_args() - if args.output is None: - args.output = sys.stdout - else: - args.output = open(args.output, 'w') + with open(args.input, 'r') if args.input else sys.stdin as infile, \ + open(args.output, 'w') if args.output else sys.stdout as outfile: - if args.unwrap: - return unwrap(args) + args.input = infile + args.output = outfile - return wrap(args) + if args.unwrap: + return unwrap(args) + return wrap(args) if __name__ == '__main__': - sys.exit(Main()) + sys.exit(main()) From 5a70f4a38e596e39c13a060cd6aa251ce1b46f14 Mon Sep 17 00:00:00 2001 From: Aviv Keller <38299977+RedYetiDev@users.noreply.github.com> Date: Tue, 13 Aug 2024 12:29:56 -0400 Subject: [PATCH 2/2] Update sdwrap.py --- tools/zos/sdwrap.py | 102 ++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/tools/zos/sdwrap.py b/tools/zos/sdwrap.py index 1acc93e2ba83ad..3c5af9d01f1efb 100755 --- a/tools/zos/sdwrap.py +++ b/tools/zos/sdwrap.py @@ -4,65 +4,65 @@ import sys def wrap(args): - firstline = True - while (line := args.input.read(72)): - if line.endswith('\n'): - if firstline: - outstr = f"{line}" - else: - outstr = f" {line}" - firstline = True - else: - if firstline: - outstr = f"{line:<71}*\n" - firstline = False - else: - outstr = f" {line:<70}*\n" - line = line[-1] + args.input.read(70) - args.output.write(outstr) - return 0 + firstline = True + while (line := args.input.read(72)): + if line.endswith('\n'): + if firstline: + outstr = f"{line}" + else: + outstr = f" {line}" + firstline = True + else: + if firstline: + outstr = f"{line:<71}*\n" + firstline = False + else: + outstr = f" {line:<70}*\n" + line = line[-1] + args.input.read(70) + args.output.write(outstr) + return 0 def unwrap(args): - firstline = True - for line in args.input: - if len(line) > 80: - print("Error: input line invalid (longer than 80 characters)", file=sys.stderr) - return 1 - if not firstline and line[0] != ' ': - print("Error: continuation line does not start with a blank", file=sys.stderr) - return 1 + firstline = True + for line in args.input: + if len(line) > 80: + print("Error: input line invalid (longer than 80 characters)", file=sys.stderr) + return 1 + if not firstline and line[0] != ' ': + print("Error: continuation line does not start with a blank", file=sys.stderr) + return 1 - if len(line) > 71 and line[71] == '*': - if firstline: - args.output.write(line[:71]) - firstline = False - else: - args.output.write(line[1:71]) - else: - if firstline: - args.output.write(line) - else: - args.output.write(line[1:]) - firstline = True - return 0 + if len(line) > 71 and line[71] == '*': + if firstline: + args.output.write(line[:71]) + firstline = False + else: + args.output.write(line[1:71]) + else: + if firstline: + args.output.write(line) + else: + args.output.write(line[1:]) + firstline = True + return 0 def main(): - parser = argparse.ArgumentParser(description="Wrap sidedeck source to card formats") - parser.add_argument("-u", "--unwrap", help="Unwrap sidedeck cards to source formats instead", action="store_true") - parser.add_argument("-i", "--input", help="Input filename, defaults to stdin", default=None) - parser.add_argument("-o", "--output", help="Output filename, defaults to stdout", default=None) + parser = argparse.ArgumentParser(description="Wrap sidedeck source to card formats") + parser.add_argument("-u", "--unwrap", help="Unwrap sidedeck cards to source formats instead", action="store_true") + parser.add_argument("-i", "--input", help="Input filename, defaults to stdin", default=None) + parser.add_argument("-o", "--output", help="Output filename, defaults to stdout", default=None) - args = parser.parse_args() + args = parser.parse_args() - with open(args.input, 'r') if args.input else sys.stdin as infile, \ - open(args.output, 'w') if args.output else sys.stdout as outfile: + with open(args.input, 'r') if args.input else sys.stdin as infile, \ + open(args.output, 'w') if args.output else sys.stdout as outfile: - args.input = infile - args.output = outfile + args.input = infile + args.output = outfile - if args.unwrap: - return unwrap(args) - return wrap(args) + if args.unwrap: + return unwrap(args) + return wrap(args) if __name__ == '__main__': - sys.exit(main()) + sys.exit(main())