<filename>pycodecs/pybase64.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
#  pybase64.py
#  
#
#  (c) 2015 <NAME>
#  
#  MIT License

from __future__ import print_function
from argparse import ArgumentParser
import base64
import sys, os, os.path
import logging
import hashlib

_log = logging.getLogger(__name__)

def writefile(args, outdata, ofile=sys.stdout):
    ofile.write(outdata)

def code_one(args, indata, tocode):
    outdata = tocode(indata)
    _log.debug(" %s: len(output) = %d" % (args.mode, len(outdata)))
    if args.output == '-' or (len(args.pathnames) == 0 and args.output is None): 
        writefile(args, outdata, sys.stdout)
        return 0
    if args.output is None:
        pn = os.path.basename(args.pathnames[0])
        stem, ext = os.path.splitext(pn)
        if ext == ".b64": args.output = stem
        else: args.output = pn + "." + args.mode + "d"
    _log.debug(" writing to %s" % args.output)
    with open(args.output, 'wb') as ofile:
        writefile(args, outdata, ofile)
    if args.verbose and args.mode == 'decode':
        with open(args.output, 'rb') as dfile:
            hasher = hashlib.new(args.algorithm).new(dfile.read())
            _log.debug(" %s: %s" % (args.algorithm, hasher.hexdigest()))
    return 0

def encode_all(args):
    decoded = read_from_pathname(args)
    code_one(args, decoded, base64.b64encode)
    return 0

def read_from_pathname(args):
    if len(args.pathnames) == 0:
        stuff = sys.stdin.read()
    else:
        with open(args.pathnames[0], 'rb') as ifile:
            stuff = ifile.read()
    _log.debug(" %s: len(input) = %d" % (args.mode, len(stuff)))
    return stuff

def decode_all(args):
    encoded = read_from_pathname(args)
    code_one(args, encoded, base64.b64decode)
    return 0

def main():
    parser = ArgumentParser()
    parser.add_argument("mode", choices=['encode', 'decode'])
    parser.add_argument("pathnames", nargs="+")
    parser.add_argument("-o", "--output")
    parser.add_argument("-e", "--encoding", default="utf8")
    parser.add_argument("-v", "--verbose", action="store_true")
    parser.add_argument("-a", "--algorithm", default="md5", help="set algorithm (must be supported by hashlib)")
    args = parser.parse_args()
    if args.verbose:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)
    if args.mode == 'decode':
        return decode_all(args)
    elif args.mode == 'encode':
        return encode_all(args)
    else:
        raise ValueError("mode must be 'decode' or 'encode'")


<filename>pycodecs/pybase64.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
#  pybase64.py
#  
#
#  (c) 2015 <NAME>
#  
#  MIT License

from __future__ import print_function
from argparse import ArgumentParser
import base64
import sys, os, os.path
import logging
import hashlib

_log = logging.getLogger(__name__)

def writefile(args, outdata, ofile=sys.stdout):
    ofile.write(outdata)

def code_one(args, indata, tocode):
    outdata = tocode(indata)
    _log.debug(" %s: len(output) = %d" % (args.mode, len(outdata)))
    if args.output == '-' or (len(args.pathnames) == 0 and args.output is None): 
        writefile(args, outdata, sys.stdout)
        return 0
    if args.output is None:
        pn = os.path.basename(args.pathnames[0])
        stem, ext = os.path.splitext(pn)
        if ext == ".b64": args.output = stem
        else: args.output = pn + "." + args.mode + "d"
    _log.debug(" writing to %s" % args.output)
    with open(args.output, 'wb') as ofile:
        writefile(args, outdata, ofile)
    if args.verbose and args.mode == 'decode':
        with open(args.output, 'rb') as dfile:
            hasher = hashlib.new(args.algorithm).new(dfile.read())
            _log.debug(" %s: %s" % (args.algorithm, hasher.hexdigest()))
    return 0

def encode_all(args):
    decoded = read_from_pathname(args)
    code_one(args, decoded, base64.b64encode)
    return 0

def read_from_pathname(args):
    if len(args.pathnames) == 0:
        stuff = sys.stdin.read()
    else:
        with open(args.pathnames[0], 'rb') as ifile:
            stuff = ifile.read()
    _log.debug(" %s: len(input) = %d" % (args.mode, len(stuff)))
    return stuff

def decode_all(args):
    encoded = read_from_pathname(args)
    code_one(args, encoded, base64.b64decode)
    return 0

def main():
    parser = ArgumentParser()
    parser.add_argument("mode", choices=['encode', 'decode'])
    parser.add_argument("pathnames", nargs="+")
    parser.add_argument("-o", "--output")
    parser.add_argument("-e", "--encoding", default="utf8")
    parser.add_argument("-v", "--verbose", action="store_true")
    parser.add_argument("-a", "--algorithm", default="md5", help="set algorithm (must be supported by hashlib)")
    args = parser.parse_args()
    if args.verbose:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)
    if args.mode == 'decode':
        return decode_all(args)
    elif args.mode == 'encode':
        return encode_all(args)
    else:
        raise ValueError("mode must be 'decode' or 'encode'")


#!/usr/bin/env python3 # -*- coding: utf-8 -*- # #  pybase64.py # # #  (c) 2015 <NAME> # #  MIT License