From 1d315e91df1809e08439d3b3f934c78e929dc269 Mon Sep 17 00:00:00 2001 From: PatriotRossii Date: Wed, 2 Jun 2021 13:27:12 +0500 Subject: [PATCH 1/3] Replaced shitty argument parser with argparse --- owodecode.py | 48 ++++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/owodecode.py b/owodecode.py index ff4f080..043ba5a 100644 --- a/owodecode.py +++ b/owodecode.py @@ -4,31 +4,27 @@ # Made by Glitch, 2020 # https://www.glitchfur.net -from sys import argv, stdout, stderr -from os.path import exists, split +from sys import stdout, stderr +from os.path import exists from os import remove -KEEP_ORIG = False -STDOUT_FLAG = False +import argparse def main(): - if len(argv) < 2: - print("The syntax for running this script is as follows:") - print("python owodecode.py [-kc] [ ... ]") - exit(0) - in_fns = argv[1:] - # There is probably a better way to handle parameters. But considering - # there are only two, I'm not too worried about it right now. - for param in in_fns: - if param.startswith("-"): - global KEEP_ORIG - global STDOUT_FLAG - if "k" in param: - KEEP_ORIG = True - if "c" in param: - STDOUT_FLAG = True - KEEP_ORIG = True # Output going to stdout, keep original file - in_fns.remove(param) + parser = argparse.ArgumentParser() + + parser.add_argument("encoded_file", metavar="", type=str, nargs="+") + parser.add_argument('-k', dest="keep_original", action='store_true', + help="Keep the input file. Do not delete it after doing encoding or decoding") + parser.add_argument('-c', dest="to_stdout", action='store_true', + help="Encode or decode to stdout instead of a file. This implies -k. This also makes piping output possible") + + args = parser.parse_args() + + to_stdout = args.to_stdout + keep_original = args.keep_original or to_stdout + in_fns = args.encoded_file + for fn in in_fns: if not exists(fn): print("%s: No such file or directory" % fn, file=stderr) @@ -43,11 +39,11 @@ def main(): in_fns.remove(fn) out_fns = [fn[:-4] for fn in in_fns] for i in range(len(in_fns)): - decode(in_fns[i], out_fns[i]) + decode(in_fns[i], out_fns[i], to_stdout, keep_original) -def decode(in_fn, out_fn): +def decode(in_fn, out_fn, to_stdout=False, keep_original=False): in_fp = open(in_fn, "r") - if STDOUT_FLAG == False: + if to_stdout == False: out_fp = open(out_fn, "wb") else: out_fp = stdout.buffer @@ -66,9 +62,9 @@ def decode(in_fn, out_fn): "it is not an encoded file. Aborting." % in_fn) exit(1) in_fp.close() - if STDOUT_FLAG == False: + if to_stdout == False: out_fp.close() - if KEEP_ORIG == False: + if keep_original == False: remove(in_fn) if __name__ == "__main__": From 4f62aa8aa767f6c3b2b883b5f947fe923ce2eb6f Mon Sep 17 00:00:00 2001 From: PatriotRossii Date: Wed, 2 Jun 2021 13:32:25 +0500 Subject: [PATCH 2/3] Self-written argument parsing replaced by argparse --- owoencode.py | 48 ++++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/owoencode.py b/owoencode.py index afb2681..813441a 100644 --- a/owoencode.py +++ b/owoencode.py @@ -4,31 +4,27 @@ # Made by Glitch, 2020 # https://www.glitchfur.net -from sys import argv, stdout, stderr -from os.path import exists, split +from sys import stdout, stderr +from os.path import exists from os import remove -KEEP_ORIG = False -STDOUT_FLAG = False +import argparse def main(): - if len(argv) < 2: - print("The syntax for running this script is as follows:") - print("python owoencode.py [-kc] [ ... ]") - exit(0) - in_fns = argv[1:] - # There is probably a better way to handle parameters. But considering - # there are only two, I'm not too worried about it right now. - for param in in_fns: - if param.startswith("-"): - global KEEP_ORIG - global STDOUT_FLAG - if "k" in param: - KEEP_ORIG = True - if "c" in param: - STDOUT_FLAG = True - KEEP_ORIG = True # Output going to stdout, keep original file - in_fns.remove(param) + parser = argparse.ArgumentParser() + + parser.add_argument("encoded_file", metavar="", type=str, nargs="+") + parser.add_argument('-k', dest="keep_original", action='store_true', + help="Keep the input file. Do not delete it after doing encoding") + parser.add_argument('-c', dest="to_stdout", action='store_true', + help="Encode to stdout instead of a file. This implies -k. This also makes piping output possible") + + args = parser.parse_args() + + to_stdout = args.to_stdout + keep_original = args.keep_original or to_stdout + in_fns = args.encoded_file + for fn in in_fns: if not exists(fn): print("%s: No such file or directory" % fn, file=stderr) @@ -39,11 +35,11 @@ def main(): in_fns.remove(fn) out_fns = ["%s.owo" % fn for fn in in_fns] for i in range(len(in_fns)): - encode(in_fns[i], out_fns[i]) + encode(in_fns[i], out_fns[i], to_stdout, keep_original) -def encode(in_fn, out_fn): +def encode(in_fn, out_fn, to_stdout=False, keep_original=False): in_fp = open(in_fn, "rb") - if STDOUT_FLAG == False: + if to_stdout == False: out_fp = open(out_fn, "w") else: out_fp = stdout @@ -54,9 +50,9 @@ def encode(in_fn, out_fn): out_buffer = ''.join([bin(byte)[2:].zfill(8) for byte in in_buffer]) out_fp.write(out_buffer.replace("1", "OwO").replace("0", "UwU")) in_fp.close() - if STDOUT_FLAG == False: + if to_stdout == False: out_fp.close() - if KEEP_ORIG == False: + if keep_original == False: remove(in_fn) if __name__ == "__main__": From 8c4c524772ab4418abe3ac8cf8d00a0bfcb59c6f Mon Sep 17 00:00:00 2001 From: PatriotRossii Date: Wed, 2 Jun 2021 13:32:40 +0500 Subject: [PATCH 3/3] Minor changes in help message --- owodecode.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/owodecode.py b/owodecode.py index 043ba5a..f13bfe5 100644 --- a/owodecode.py +++ b/owodecode.py @@ -15,9 +15,9 @@ def main(): parser.add_argument("encoded_file", metavar="", type=str, nargs="+") parser.add_argument('-k', dest="keep_original", action='store_true', - help="Keep the input file. Do not delete it after doing encoding or decoding") + help="Keep the input file. Do not delete it after doing decoding") parser.add_argument('-c', dest="to_stdout", action='store_true', - help="Encode or decode to stdout instead of a file. This implies -k. This also makes piping output possible") + help="Decode to stdout instead of a file. This implies -k. This also makes piping output possible") args = parser.parse_args()