fix some weird things from the patch from runenes,

re-replace recognisePlayerIDs(), new one converts all names to UTF-8 prior to processing, as apparently there are problems with dicts using unicode keys, and deals iwth database escaping issues.
This commit is contained in:
eblade 2009-03-27 16:52:27 -04:00
parent 121ce1b648
commit eb514902ee
2 changed files with 42 additions and 34 deletions

View File

@ -208,9 +208,7 @@ class GuiBulkImport():
self.lab_drop.set_sensitive(False) self.lab_drop.set_sensitive(False)
def main(argv=None): def main(argv=None):
"""main can also be called in the python interpreter, by supplying the command line as the argument. """main can also be called in the python interpreter, by supplying the command line as the argument."""
>>>import GuiBulkImport
>>>GuiBulkImport.main(['-f'.'~/data/hands'])"""
if argv is None: if argv is None:
argv = sys.argv[1:] argv = sys.argv[1:]
@ -238,13 +236,14 @@ def main(argv=None):
if os.name == 'nt': settings['os'] = 'windows' if os.name == 'nt': settings['os'] = 'windows'
else: settings['os'] = 'linuxmac' else: settings['os'] = 'linuxmac'
settings.update(config.get_db_parameters('fpdb')) # settings.update(config.get_db_parameters('fpdb'))
settings.update(config.get_db_parameters())
settings.update(config.get_tv_parameters()) settings.update(config.get_tv_parameters())
settings.update(config.get_import_parameters()) settings.update(config.get_import_parameters())
settings.update(config.get_default_paths()) settings.update(config.get_default_paths())
if not options.gui: if not options.gui:
print """-q is deprecated. Just use "-f filename" instead""" print '-q is deprecated. Just use "-f filename" instead'
# This is because -q on its own causes an error, so -f is necessary and sufficient for cmd line use # This is because -q on its own causes an error, so -f is necessary and sufficient for cmd line use
if not options.filename: if not options.filename:
i = GuiBulkImport(db, settings, config) i = GuiBulkImport(db, settings, config)

View File

@ -163,9 +163,9 @@ def prepareBulkImport(fdb):
# DON'T FORGET TO RECREATE THEM!! # DON'T FORGET TO RECREATE THEM!!
#print "dropping pg fk", fk['fktab'], fk['fkcol'] #print "dropping pg fk", fk['fktab'], fk['fkcol']
try: try:
#print "alter table %s drop constraint %s_%s_fkey" % (fk['fktab'], fk['fktab'], fk['fkcol']) #print "alter table %s drop constraint %s_%s_fkey" % (fk['fktab'], fk['fktab'], fk['fkcol'])
fdb.cursor.execute("alter table %s drop constraint %s_%s_fkey" % (fk['fktab'], fk['fktab'], fk['fkcol'])) fdb.cursor.execute("alter table %s drop constraint %s_%s_fkey" % (fk['fktab'], fk['fktab'], fk['fkcol']))
print "dropped pg fk pg fk %s_%s_fkey" % (fk['fktab'], fk['fkcol']) print "dropped pg fk pg fk %s_%s_fkey" % (fk['fktab'], fk['fkcol'])
except: except:
print "! failed drop pg fk %s_%s_fkey" % (fk['fktab'], fk['fkcol']) print "! failed drop pg fk %s_%s_fkey" % (fk['fktab'], fk['fkcol'])
else: else:
@ -1392,34 +1392,43 @@ def recogniseTourneyTypeId(cursor, siteId, buyin, fee, knockout, rebuyOrAddon):
# { playername: id } instead of depending on it's relation to the positions list # { playername: id } instead of depending on it's relation to the positions list
# then this can be reduced in complexity a bit # then this can be reduced in complexity a bit
def recognisePlayerIDs(cursor, names, site_id):
result = []
for i in xrange(len(names)):
cursor.execute ("SELECT id FROM Players WHERE name=%s", (names[i],))
tmp=cursor.fetchall()
if (len(tmp)==0): #new player
cursor.execute ("INSERT INTO Players (name, siteId) VALUES (%s, %s)", (names[i], site_id))
#print "Number of players rows inserted: %d" % cursor.rowcount
cursor.execute ("SELECT id FROM Players WHERE name=%s", (names[i],))
tmp=cursor.fetchall()
#print "recognisePlayerIDs, names[i]:",names[i],"tmp:",tmp
result.append(tmp[0][0])
return result
#def recognisePlayerIDs(cursor, names, site_id): #def recognisePlayerIDs(cursor, names, site_id):
# cursor.execute("SELECT name,id FROM Players WHERE name='%s'" % "' OR name='".join(names)) # get all playerids by the names passed in # result = []
# ids = dict(cursor.fetchall()) # convert to dict # for i in xrange(len(names)):
# if len(ids) != len(names): # cursor.execute ("SELECT id FROM Players WHERE name=%s", (names[i],))
# notfound = [n for n in names if n not in ids] # make list of names not in database # tmp=cursor.fetchall()
# if notfound: # insert them into database # if (len(tmp)==0): #new player
# cursor.executemany("INSERT INTO Players (name, siteId) VALUES (%s, "+str(site_id)+")", (notfound)) # cursor.execute ("INSERT INTO Players (name, siteId) VALUES (%s, %s)", (names[i], site_id))
# cursor.execute("SELECT name,id FROM Players WHERE name='%s'" % "' OR name='".join(notfound)) # get their new ids # #print "Number of players rows inserted: %d" % cursor.rowcount
# tmp = dict(cursor.fetchall()) # cursor.execute ("SELECT id FROM Players WHERE name=%s", (names[i],))
# for n in tmp: # put them all into the same dict # tmp=cursor.fetchall()
# ids[n] = tmp[n] # #print "recognisePlayerIDs, names[i]:",names[i],"tmp:",tmp
# # result.append(tmp[0][0])
# # return them in the SAME ORDER that they came in in the names argument, rather than the order they came out of the DB # return result
# return [ids[n] for n in names]
def recognisePlayerIDs(cursor, names, site_id):
names = [n.encode("utf-8") for n in names]
namestring = "name=%s"
for x in xrange(len(names)-1):
namestring += " OR name=%s"
# print "names=", names, "\nnamestring=", namestring
cursor.execute("SELECT name,id FROM Players WHERE %s" % namestring , names) # get all playerids by the names passed in
ids = dict(cursor.fetchall()) # convert to dict
if len(ids) != len(names):
notfound = [n for n in names if n not in ids] # make list of names not in database
if notfound: # insert them into database
namestring = "name=%s"
for x in xrange(len(notfound)-1):
namestring += " OR name=%s"
# print "namestring=",namestring,"\nnotfound=", notfound
cursor.executemany("INSERT INTO Players (name, siteId) VALUES (%s, "+str(site_id)+")", (notfound))
cursor.execute("SELECT name,id FROM Players WHERE %s" % namestring, notfound) # get their new ids
tmp = dict(cursor.fetchall())
for n in tmp: # put them all into the same dict
ids[n] = tmp[n]
# print "ids=", ids
# return them in the SAME ORDER that they came in in the names argument, rather than the order they came out of the DB
return [ids[n] for n in names]
#end def recognisePlayerIDs #end def recognisePlayerIDs