#!/usr/bin/env python
# pripnglsch
# PNG List Chunks

from __future__ import print_function

import argparse
import binascii
import sys

import png


def list_chunks(out, inp):
    r = png.Reader(file=inp)
    for t, v in r.chunks():
        add = ""
        if len(v) <= 28:
            add = " " + hex(v)
        else:
            add = " " + hex(v[:26]) + "..."
        t = t.decode("ascii")
        print("%s %10d%s" % (t, len(v), add), file=out)


def hex(bs):
    """Convert the bytes `bs` to a hex string."""
    return binascii.hexlify(bs).decode("ascii")


def main(argv=None):
    parser = argparse.ArgumentParser()
    parser.add_argument("png", nargs="?", default="-")
    args = parser.parse_args()
    inp = png.cli_open(args.png)
    return list_chunks(sys.stdout, inp)


if __name__ == "__main__":
    main()
