#!/usr/bin/python

# image_comment.py
#       --copyright--                   Copyright 2013 (C) Tranzoa, Co. All rights reserved.    Warranty: You're free and on your own here. This code is not necessarily up-to-date or of public quality.
#       --url--                         http://www.tranzoa.net/tzpython/
#       --email--                       pycode is the name to send to. tranzoa.com is the place to send to.
#       --bodstamps--
#       July 10, 2013           bar
#       --eodstamps--
##      \file
#       \namespace              tzpython.image_comment
#
#
#       Let the user input a 1-line comment for an image.
#       Put the comments with the image file name to an output file.
#
#


import  os
import  sys

from    PIL import  Image

import  tzlib


help_str    = """
%s (options)    output_comment_file     ambiguous_image_file_name...

Options:
    --sub_dirs                          Include matching files in subdirectories.

Show the given images and then ask the user to input a comment for it.

Print the image file name and comment to the end of the output file.

"""

#
#
if __name__ == '__main__' :

    import  TZCommandLineAtFile


    program_name    = sys.argv.pop(0)

    TZCommandLineAtFile.expand_at_sign_command_line_files(sys.argv)

    sub_dirs        = False
    cfn             = None

    while True :
        oi  = tzlib.array_find(sys.argv, [ "--help", "-h", "-?", "/?", "/h", "/H" ] )
        if  oi < 0 :    break
        del sys.argv[oi]
        print help_str % ( os.path.basename(program_name) )
        sys.exit(254)


    while True :
        oi  = tzlib.array_find(sys.argv, [ "--output", "-o", ] )
        if  oi < 0  : break
        del sys.argv[oi]
        cfn = sys.argv.pop(oi)

    while True :
        oi  = tzlib.array_find(sys.argv, [ "--sub_dirs", "--subdirs", "--sub-dirs", "--sub_dir", "--subdir", "--sub-dir", "--sd", "-s", ] )
        if  oi < 0 :    break
        del sys.argv[oi]
        sub_dirs    = True                  # search sub-directories


    if  not len(sys.argv) :
        print >>sys.stderr, "Please tell me the output comment file to add to!"
        sys.exit(101)

    if  not cfn :
        cfn = sys.argv.pop(0)

    afns    = {}
    while len(sys.argv) :
        fn  = sys.argv.pop(0)
        fns = tzlib.ambiguous_file_list(fn, do_sub_dirs = sub_dirs)
        if  not len(fns) :
            print >>sys.stderr, "No file found: %s!" % fn
            sys.exit(111)
        afns.update(tzlib.make_dictionary(fns))

    fns = afns.keys()
    fns.sort(lambda a, b : cmp(a.lower(), b.lower()))

    if  not len(fns) :
        print >>sys.stderr, "Please tell me some image files to comment on!"
        sys.exit(102)

    try :
        im  = Image.open(cfn)
        print >>sys.stderr, "I don't want to print to the image file, %s!" % cfn
        sys.exit(102)
    except ( OSError, IOError, TypeError, ValueError ) :
        pass

    fo  = open(cfn, "at")                   # add to the file

    for fn in fns :
        img = Image.open(fn)
        img.show()
        ln  = sys.stdin.readline()
        if  not ln :
            fo.close()
            print "Goodbye!"
            sys.exit(1)

        fo.write("%-20s %s\n" % ( ln.strip(), fn, ) )

    fo.close()


#
#
#
# eof
