#!/cygdrive/c/python34/python
#
#
# print the metadata on all the images (.jpg/cr2) in the specified directory.

import pdb
import exifread
from bird_utils import get_subdirs
from bird_utils import ls_cmd

pdir_base = "c:/users/Nancy/Pictures/"
alist=[]    # stores one or more possible answers to which directory

    # get all the subdirectories starting at 'pdir_base'/2*
    # e.g., ../Pictures/2015_...
files = get_subdirs(pdir_base+'2*')
while 1:

        # dir of files to examine
    rawd = ""
    alist = []
    ans = input("Directory: ")
    ans = ans.rstrip('\n')
    if ans=='q' or len(ans)<1:
        break
    # new directory provided
    if len(ans) > 1:
                # see if full path path provided, if so, skip to end of loop and save it
        if 'c:' not in ans:
                # full path not provided, find all match for name provided in subdirectories
            for i in files:
                if ans.lower() in i.lower():
                    alist.append(i)
                # if multiple subdirectories found, ask for the correct one to use.
            if len(alist) > 1:
                j = 1
                for al in alist:
                    print (j, ": ", al)
                    j += 1
                an1 = input ("Enter number: ")
                an1 = an1.rstrip('\n')
                rawd = alist[int(an1)-1] + '/'
            else:
                rawd = alist[0] + '/'

    # Now check out all the jpg and cr2 files.  I only print the information for
    # the first one found in a group.
    limages = ls_cmd(None, rawd)
    last = ""
    #pdb.set_trace()
    outf=open('exif.dat', 'w')
    print ('{:>10}{:>12}{:>10}{:>10}{:>10}{:>8}{:>12}{:>12}'.format(' ', 'Mode', 'TV', 'AV', 'Exp Comp', 'ISO', 'Focal Len', 'Date'))
    aline=[]
    for im in limages:
        if '.CR2' in im or '.JPG' in im or '.jpg' in im:
            base = im[0:-4]
            afile = rawd + im
            if not base in last:
                    # process the metadata
                last = base
                ff = open(afile, 'rb')
                tags = exifread.process_file(ff)
                outf.write(im)
                outf.write('----------------------------------------')
                for tag in tags.keys():
                    #if tag not in ('JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'EXIF MakerNote'):
                    #if 'EXIF' in tag:
                        #print ("Key: %s, value %s" % (tag, tags[tag]))
                    #    outf.write ("Key: %s, value %s\n" % (tag, tags[tag]))
                    if "MakerNote ExposureMode" in tag:
                        exp = str(tags[tag])
                        exp = exp.replace('-priority','')
                    if "ExposureTime" in tag:
                        exptime =   str(tags[tag])
                    if "FNumber" in tag:
                        aper =   str(tags[tag])
                        if '/' in aper:
                            fields = aper.split('/')
                            num = int(fields[0])/int(fields[1])
                            aper = str(num)
                        else:
                             aper = aper + '.0lb'
                    if "ExposureBias" in tag:
                        expcomp =   str(tags[tag])
                    if "ISOSpeedRatings" in tag:
                        iso =   str(tags[tag])
                    if "EXIF FocalLength" in tag:
                        focal = str(tags[tag])
                        focal = focal + ' mm'
                    if "DateTimeOriginal" in tag:
                        date =   str(tags[tag])
                        index = date.index(' ')
                        date = date[0:index]
                        date = date.replace(':','-')
                image = im[0:-4]
                aline.append('{:<10}{:>12}{:>10}{:>10}{:>10}{:>8}{:>12}{:>12}'.format(image, exp, exptime, aper, expcomp, iso, focal, date))
                ff.close()
    #pdb.set_trace()
    aline.sort()
    for line in aline:
        print (line)
