# L-22 MCS 275 Wed 5 Mar 2008 : filldb.py

import os, MySQLdb
from grabpyhead import EnumFields

def MyDate(s):
   """
   Converts a string like Wed 5 Mar 2008
   into the format 2008-03-05.
   """
   m = {"Jan":"01", "Feb":"02", "Mar":"03", \
        "Apr":"04", "May":"05", "Jun":"06", \
        "Jul":"07", "Aug":"08", "Sep":"09", \
        "Oct":"10", "Nov":"11", "Dec":"12"}
   L = s.split(' ')
   day = '%02d' % int(L[1])
   return L[3] + '-' + m[L[2]] + '-' + day

def InsertData(c):
   """
   Data is inserted into the database,
   using the cursor c.
   """
   def Insert(s):
      """
      Uses the tuple s to insert into
      the table scripts.
      """
      d = 'insert into scripts values (' \
        + '\"' + s[0] + '\"' + ',' \
        + '\"' + s[1] + '\"' + ',' \
        + '\"' + MyDate(s[2]) + '\"' + ',' \
        + '\"' + s[3] + '\"' + ');'
      c.execute(d)

   n = EnumFields('.',Insert)
   return n

def main():
   """
   Prints the header of all .py files
   in the current directory.
   """
   p = os.getcwd()
   os.chdir('../MCS275py')
   db = MySQLdb.connect(db="OurPyFiles")
   c = db.cursor() 
   n = InsertData(c)
   print 'inserted %d .py files' % n
   os.chdir(p)

if __name__ == "__main__": main()
