#!/cygdrive/c/python34/python
##!/cygdrive/c/python33/python
# Program to print out what information is available for each each bird.
# For example, feet pictures, tongue pictures, videos, partial (text dir)
# or complete (Description) description and the number of pictures for
# each bird.

import pdb
import re
import subprocess
from bird_utils import ls_cmd

# for the specificified directory (path) list all the files with the
# extension (exten).
def ls_proc (path, exten):
    out = {}
    npath = path + '*' + exten
#    pdb.set_trace()
    alist = ls_cmd(None, npath)
    for i in alist:
        if len(i) < 1:
            continue
        i = i.replace(path,'')	# get rid of path name
        i = i.replace(exten,'') # get rid of the extension (e.g., '.txt' or '.jpg')
		# get rid of any ending '_(number)' that I have for videos, feet, etc.
        i = re.sub('_.*','', i, count=0, flags=0)
        if (i in out):
            out[i] += 1
        else:
            out[i] = 1
    return out

def astrip(line):
    line = line.rstrip('\n')
    line = line.lstrip()
    line = line.rstrip()
    return line

birds = {}          # contains the full name and count of pics
pntr = {}           # relates birdname with spaces to birdname without spaces
feet = {}           # number of feet pictures per bird
tongue = {}         # number of tongue pictures per bird
video = {}          # number of videos per bird
text = {}           # If available, text in subdirectory text 
desc = {}	    # If available, text in subdirectory Description
pics = {}           # hold the number of pics per bird

# First get a list of the files in the various directories being examined.
# ls_proc also counts the number of files per bird which is only relevant
# for non-text files
#pdb.set_trace()
text = ls_proc('../text/', '.txt')
desc = ls_proc('../Descriptions/', '.txt')
video = ls_proc('../videos/', '.mp4')
feet = ls_proc('../feet/', '.jpg')
tongue = ls_proc('../Tongue/', '.jpg')
#ls_place = ls_proc('../text_place/', '.txt')
#ls_class = ls_proc('../text_class/', '.txt')
#ls_title = ls_proc('../text_title/', '.txt')

# now get a list of the bird full names and put it into a dictionary with the
# shortened name (no spaces) being the key
infile = open('bird_class')
for line in infile:
    if 'bird:' in line:
         fields = line.split(':')
	 	# get rid of any beginning or ending spaces
         fullname = fields[1].lstrip()
         fullname = fullname.rstrip()
	 	# get rid of spaces, if there, for multiple word names: Common Raven
         p_name = fields[1].replace(' ','')
         birds[fullname] = p_name

# read in the inventory database to get a list of the unique birds
# count the number of pictures per bird
inv = open('inventory.txt')
for line in inv:
    line = astrip(line)
    if len(line) < 1: continue
    if line[0] == '*': continue
    fields = line.split('\t')
    fullname = astrip(fields[4])    # delete any beginning, ending spaces & carriage returns
    p_name = fullname.replace(' ','')  # get rid of spaces between the bird name
    if p_name in pics:
        pics[p_name] += 1
    else:
        pics[p_name] = 1
inv.close()
#pdb.set_trace()

count = 0
tcount = 0
total_pics = 0
no_text = "\n  Files that don't have any descriptions\n"

# now ready to print out the information
keys = list(birds.keys())
keys.sort()
tstring = "{0:<32} {1:>8} {2:>8} {3:>8} {4:>8} {5:>8} {6:>8}".format('--Name', 'Pics', 'Text', 'Desc', 'Videos', 'Feet', 'Tongues')
print (tstring)
for k in keys:
    #pdb.set_trace()
    p_name = birds[k]
    atext = adesc = avideo = afeet = atongue = '-'
    if p_name in text:
        atext = "Y"
    if p_name in desc:
        adesc = "Y"
    if p_name in video:
        avideo = str(video[p_name])
    if p_name in feet:
        afeet = str(feet[p_name])
    if p_name in tongue:
        atongue = str(tongue[p_name])
		# if no description at all, keep track of these files for later printing
    if '-' in atext and '-' in adesc:
        no_text += k + "\n"
    numb = pics[p_name]
    total_pics += numb
    astring = "{0:<32} {1:>8} {2:>8} {3:>8} {4:>8} {5:>8} {6:>8}".format(k, numb, atext, adesc, avideo, afeet, atongue)
    print (astring)
    count += 1
    if count > 4:
        count = 0
        print ("")
    tcount += 1
    if tcount > 49:
        tcount = 0
        print (tstring)
cstring = "{0:<32} {1:>8}".format("Total number of Pictures", str(total_pics))
print (cstring)
print (no_text)
