#!/usr/bin/env python3
# $URL: http://pypng.googlecode.com/svn/trunk/code/pnglsch $
# $Rev: 107 $
# pnglsch
# PNG List Chunks

from binascii import hexlify

import png

def list(out, inp):
    r = png.Reader(file=inp)
    for t,v in r.chunks():
        add = ''
        if len(v) <= 28:
            add = ' ' + hexlify(v).decode('ascii')
        if isinstance(t, bytes):
            t = t.decode('latin1')
        print("%s %10d%s" % (t, len(v), add), file=out)

def main(argv=None):
    import sys

    if argv is None:
        argv = sys.argv
    arg = argv[1:]

    if len(arg) > 0:
        f = open(arg[0], 'rb')
    else:
        f = getattr(sys.stdin, 'buffer', sys.stdin)
    return list(sys.stdout, f)

if __name__ == '__main__':
    main()
