Skip to content

Replace manual argument parsing with argparse #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 22 additions & 26 deletions owodecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] <encoded_file> [ ... ]")
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="<encoded_file>", type=str, nargs="+")
parser.add_argument('-k', dest="keep_original", action='store_true',
help="Keep the input file. Do not delete it after doing decoding")
parser.add_argument('-c', dest="to_stdout", action='store_true',
help="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)
Expand All @@ -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
Expand All @@ -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__":
Expand Down
48 changes: 22 additions & 26 deletions owoencode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] <original_file> [ ... ]")
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="<encoded_file>", 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)
Expand All @@ -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
Expand All @@ -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__":
Expand Down