home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / bale / baleit.py~ < prev    next >
Text File  |  2002-09-25  |  1KB  |  67 lines

  1. import os
  2. import string
  3. import MySQLdb  
  4.  
  5. def opendb():
  6.  host = 'localhost'
  7.  db  = 'events'
  8.   
  9.  user = 'root'
  10.  rdb = MySQLdb.connect(db=db, host=host, user=user)
  11.  cursor = rdb.cursor()
  12.  return rdb, cursor
  13.  
  14. def getfilelist():
  15.  pwd = "%s/groups/" % (os.getcwd())
  16.  fl = os.listdir(pwd)
  17.  
  18.  return pwd, fl
  19.  
  20. def parsefile(fname):
  21.  f = open(fname, 'r')
  22.  g = f.readlines()
  23.  f.close()
  24.  return g
  25.  
  26. def fudgeQuotes(str):
  27.  g = str.strip()
  28.  g = string.replace(g, "'", "''")
  29.  return g
  30.  
  31. def getfiles(cursor):
  32.  pwd, fl = getfilelist()
  33.  for i in fl:
  34.   lines = parsefile("%s%s" % (pwd, i))
  35.         
  36.   q1 = "select count(*) from groups where gcode = '%s'" % (i,)
  37.   q3 = "delete from groups where gcode = '%s'" % (i,)
  38.  
  39.   q2 = "insert into groups (gcode, gname, gcity, gcounty, "
  40.   q2 = q2 + "active, outofarea, description) values "
  41.   q2 = q2 + "('%s', '%s', '%s', '%s', %s, %s, '%s')"
  42.  
  43.   cursor.execute(q1)
  44.   gcode   = i
  45.   gname   = fudgeQuotes(lines[0])
  46.   gcity   = fudgeQuotes(lines[1])
  47.   gcounty = fudgeQuotes(lines[2])
  48.   gactive = fudgeQuotes(lines[3])
  49.   goutofarea = fudgeQuotes(lines[4])
  50.   description = fudgeQuotes(string.join(lines[5:], ''))
  51.  
  52.   resultSet = cursor.fetchall()
  53.   if resultSet[0][0] != 0:
  54.    cursor.execute(q3)
  55.   
  56.  
  57.   stmt = q2 % (gcode, gname, gcity, gcounty, gactive, goutofarea, description)
  58.  
  59.   cursor.execute(stmt)
  60.  
  61.  
  62.   print "group %s added successfully" % (i,)          
  63.  
  64. if __name__ == "__main__":
  65.  db, cursor = opendb()
  66.  getfiles(cursor)
  67.