Index: talkclips.py
===================================================================
--- talkclips.py	(revision 29000)
+++ talkclips.py	(working copy)
@@ -20,17 +20,19 @@
 
 import os
 import sys
+import commands 
+import time 
+
 # The following lines provide variables which you can modify to adjust
 # settings... See comments next to each line.
 # Start opts:
 espeak = '/usr/bin/espeak' # location of easpeak binary 
 rbspeexenc = './rbspeexenc' # path to rbspeexenc binary (default currentdir)
-VOPTS=espeak+" -s 320 -z" # Your espeak opts 
+VOPTS=espeak+" -s 300" # Your espeak opts 
 ROPTS=rbspeexenc+" -q 4 -c 10" # rbspeex opts 
-logfile="/tmp/talkclips.log" # a file where output should be logged
+logfile="/tmp/talkclips.log" # path to log 
 # End opts 
 # Don't touch the below settings. Unless you know what your doing. 
-log=open(logfile, 'w') # logging leave this var alone. 
 USAGE="Usage: %s <directory>" % (sys.argv[0]) # usage prompt don't touch 
 if not os.path.exists(rbspeexenc):
   print "%s not found, please change your rbspeexenc path appropriately,\n"\
@@ -58,37 +60,44 @@
     print "This script only currently works for directories.\n\n%s" % (USAGE)
     exit (-1) # not a dir 
 
-def gentalkclip(clipname, fullpath, isdir):
+def gentalkclip(clipname, fullpath, isdir, logfile):
   """Generate an individual talk clip.
 
   Based on the file name structure of talk clips, run them through the
   synth, and encoder, and save accordingly."""
+  log=open(logfile, 'a') # logging leave this var alone. 
 
   if isdir: # directory
     output=os.path.join(fullpath, "_dirname.talk") # dir clip name 
     if os.path.exists(output):
-      return True # no need to create again
+      log.close () # close the log before returning 
+      return False # no need to create again so return false
     try: # Don't let the script stop if bash raises filename errors
       os.system('%s "%s" -w "%s"' % (VOPTS, clipname, output+".tmp"))
       os.system('%s "%s" "%s"' % (ROPTS, output+".tmp", output))
       os.remove(output+".tmp") # delete the old wav file 
     except OSError:
       log.write('Failed to create clip for directory: "%s"\n' % (clipname)) 
-      return False 
+      log.close () # close the log file 
+      return False # Didn't create anything  
     log.write( 'Created clip for directory: "%s"\n' % (clipname)) # log
-    return True 
+    log.close () # Close log file 
+    return True # Made a clip 
   else: # file
     output=fullpath+".talk"
     if os.path.exists(output):
-      return True # no need to create again
+      log.close () # Close the open log file
+      return False # no need to create again so return false 
     try: 
       os.system('%s "%s" -w "%s"' % (VOPTS, clipname, output+".tmp"))
       os.system('%s "%s" "%s"' % (ROPTS, output+".tmp", output))
       os.remove (output+".tmp")
     except OSError: # don't let bash errors stop us
       log.write('Failed to create clip for file: "%s"\n' % (clipname))
+      log.close () # Close log 
       return False # something failed, so continue with next file
     log.write('Created clip for file: "%s"\n' % (clipname)) # logging
+  log.close () # Close the log 
   return True # clips created 
 
 def istalkclip(file):
@@ -102,22 +111,56 @@
   else: # Not a talk clip
     return False
 
-def walker(directory):
+def walker(directory, total, done):
   """Walk through a directory.
 
   Walk through a directory and subdirs, and operate on it, passing files
-  through to the correct functions to generate talk clips."""
+  through to the correct functions to generate talk clips. Dir is the
+  dir to walk through and create .talk clips in, while total is the
+  number of talk clips that will need to be generated, so we can give
+  the user some progress information... Done is the number of talk clips
+  already made"""
+
   for item in os.listdir(directory): # get subdirs and files
     if os.path.isdir(os.path.join(directory, item)):
-      gentalkclip (item, os.path.join(directory, item), True) # its a dir
-      walker(os.path.join(directory, item)) # go down into this sub dir 
+      if gentalkclip (item, os.path.join(directory, item), True, logfile): # its a dir
+        done+=1 # We've just done one talk clip
+        print "\r"+str(("%.0f%%" % float(float(done)/float(total)*100))), # percentage
+        sys.stdout.flush ()
+      done=walker(os.path.join(directory, item), total,done) # go down into this sub dir 
       continue
     else: # a file
       if istalkclip (item):
         continue # is a talk clip
       else: # create clip 
-        gentalkclip(item, os.path.join(directory, item), False) # a file
+        if gentalkclip(item, os.path.join(directory, item), False, logfile): # a file
+          done+=1 # We've just done one talk clip
+          print "\r"+str(("%.0f%%" % float(float(done)/float(total)*100))), # percentage
+          sys.stdout.flush ()
         continue
+  return done # Number of clips made 
+print "Preparing to create talk clips at %s" %(sys.argv[1])
+starttime=float(time.strftime ("%s"))
+# Open log, record start time, and close again. 
+log=open(logfile, 'a')
+log.write(sys.argv[0]+" started at "+time.strftime("%c")+"\n") # starting time 
+log.close() 
+print "Scanning %s" % (sys.argv[1])
+talkfiles=commands.getoutput ("find "+sys.argv[1]+" |grep -i '\\.talk'|wc -l")
+files = commands.getoutput ("find "+sys.argv[1]+" |grep -vi '\\.talk' |wc -l")
+numberfiles=int(files)-int(talkfiles)
+# How many files need talkclips to be made
+print "Creating clips for approximately %d files..." % (numberfiles)
+clipsmade=walker(RBDIR, numberfiles,0) # start the program:)
+endtime=float(time.strftime("%s"))
+timespent=endtime-starttime 
+summary="Created %d talkclips in %.0f seconds" %(clipsmade, timespent)
+print "\n%s" % (summary )
+# Open log, recording finishing time and close it again.
+log=open(logfile, 'a')
+log.write(summary+"\n")
+log.write(sys.argv[0]+" finished at "+time.strftime("%c")+"\n")
+log.close() # Close the log!
+print "Done!"
+exit () # Finished!
 
-walker(RBDIR) # start the program:)
-log.close() # close the log and finish
