Skip to content

Commit

Permalink
Add list option to scripts/codecs.py and restructure script with …
Browse files Browse the repository at this point in the history
…subcommands.
  • Loading branch information
acsor committed Sep 19, 2018
1 parent bbd8619 commit ad90ced
Showing 1 changed file with 55 additions and 34 deletions.
89 changes: 55 additions & 34 deletions scripts/codecs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#!/usr/bin/env python3
# TO-DO Add license notice
"""
Encodes/decodes data fed from the command line with PyPDF codecs.
Although PyPDF4 mandates Python 2 support as well, only Python 3 is supported
by this script.
"""
import argparse
from sys import path, stderr

from sys import argv, path, stderr
from os.path import abspath, dirname, join, pardir

PROJECT_ROOT = abspath(
Expand All @@ -13,64 +18,80 @@

from pypdf4.filters import *

__version__ = "0.1.0"
__version__ = "0.2.0"
CODECS = {
"flate": FlateCodec, "asciihex": ASCIIHexCodec, "lzw": LZWCodec,
"ascii85": ASCII85Codec, "dct": DCTCodec, "jpx": JPXCodec,
"ccittfax": CCITTFaxCodec
}
# TO-DO Add a list command
ENCODE, DECODE = ("encode", "decode")
ACTIONS = (ENCODE, DECODE)

ENCODE, DECODE, LIST = ("encode", "decode", "list")
CODEC_ACTIONS = (ENCODE, DECODE)
VIEW_ACTIONS = (LIST, )


def main():
"""
:return: exit status of program (``0`` with no errors, ``1`` with a generic
error).
"""
parser = argparse.ArgumentParser(
description="Encodes/decodes some data fed in with PyPDF codecs",
epilog="Version %s" % __version__
)

# When not specified, the default action= keyword is "store"
parser.add_argument(
"action", choices=ACTIONS, help="The action to perform"
subparsers = parser.add_subparsers(title="Commands", dest="action")
codecParser = subparsers.add_parser(
ENCODE, aliases=(DECODE, ), help="Encode/decode data"
)
parser.add_argument("data", help="Data to either encode or decode")
listParser = subparsers.add_parser(LIST, help="List available codecs")

subparsers.required = True
parser.add_argument(
"-f", "--file", dest="isfile", action="store_const", const=True,
help="Whether the argument provided to DATA should be interpreted as a"
" file path"
"-v", "--version", action="version", version="%(prog)s " + __version__
)
parser.add_argument(

codecParser.add_argument("data", help="Data to either encode or decode")
# TO-DO Add chained list of encoders/decoders support (like
# ASCIIHexDecode(LZWDecode(data))).
codecParser.add_argument(
"-c", "--codec", choices=CODECS.keys(), required=True,
help="The codec to encode/decode with"
)
parser.add_argument(
"-v", "--version", action="version", version="%(prog)s " + __version__
codecParser.add_argument(
"-f", "--file", dest="isfile", action="store_const", const=True,
help="Whether the argument provided to DATA should be interpreted as a"
" file path"
)

args = parser.parse_args()
codec = CODECS[args.codec]

if args.isfile:
try:
with open(args.data, "rb") as infile:
data = infile.read()
except IOError as e:
print(e, file=stderr)
exit(1)
else:
data = args.data.encode("LATIN1")

# TO-DO If the output is in bytes form, strip the b'' characters from the
# output
if args.action == ENCODE:
print(codec.encode(data))
elif args.action == DECODE:
print(codec.decode(data))
if args.action in CODEC_ACTIONS:
codec = CODECS[args.codec]

if args.isfile:
try:
with open(args.data, "rb") as infile:
data = infile.read()
except IOError as e:
print(e, file=stderr)
return 1
else:
data = args.data.encode("LATIN1")

if args.action == ENCODE:
print(codec.encode(data))
elif args.action == DECODE:
print(codec.decode(data))
elif args.action == LIST:
print("Available codecs:", *CODECS.keys(), sep="\n\t")
else:
print("Unsupported action %s" % args.action, file=stderr)
exit(1)
print("Unrecognized action", args.action, file=stderr)
return 1

return 0


if __name__ == "__main__":
main()
exit(main())

0 comments on commit ad90ced

Please sign in to comment.