From b35e0e48802a358009cdf2b8456cb2922f2f9b46 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 16 Oct 2008 15:15:28 -0400 Subject: [PATCH] various changes with threaded HUD_main.py --- pyfpdb/Database.py | 2 + pyfpdb/HUD_main.py | 7 +- pyfpdb/fpdb_import.py | 152 +++++++++++++----------------------------- 3 files changed, 53 insertions(+), 108 deletions(-) diff --git a/pyfpdb/Database.py b/pyfpdb/Database.py index 63de54b2..09461a87 100644 --- a/pyfpdb/Database.py +++ b/pyfpdb/Database.py @@ -79,9 +79,11 @@ class Database: self.connection.close() def get_table_name(self, hand_id): + print "searching for ", hand_id c = self.connection.cursor() c.execute(self.sql.query['get_table_name'], (hand_id, )) row = c.fetchone() + print "found = ", row return row def get_last_hand(self): diff --git a/pyfpdb/HUD_main.py b/pyfpdb/HUD_main.py index 5e3286f1..faacc0bf 100755 --- a/pyfpdb/HUD_main.py +++ b/pyfpdb/HUD_main.py @@ -36,6 +36,7 @@ Main for FreePokerTools HUD. import sys import os import thread +import time errorfile = open('HUD-error.txt', 'w', 0) sys.stderr = errorfile @@ -91,6 +92,7 @@ def producer(): # This is the thread function while True: # wait for a new hand number on stdin new_hand_id = sys.stdin.readline() + print "hand = ", new_hand_id if new_hand_id == "": # blank line means quit destroy() @@ -98,9 +100,10 @@ def producer(): # This is the thread function for h in hud_dict.keys(): if hud_dict[h].deleted: del(hud_dict[h]) - + print "getting table name" (table_name, max, poker_game) = db_connection.get_table_name(new_hand_id) stat_dict = db_connection.get_stats_from_hand(new_hand_id) + print "table = %s, max = %s, game = %s" % (table_name, max, poker_game) # if a hud for this table exists, just update it if hud_dict.has_key(table_name): @@ -108,8 +111,10 @@ def producer(): # This is the thread function # otherwise create a new hud else: table_windows = Tables.discover(config) + print "searching for %s" % (table_name) for t in table_windows.keys(): if table_windows[t].name == table_name: + print "found" create_HUD(new_hand_id, table_windows[t], db_name, table_name, max, poker_game, db_connection, config, stat_dict) break diff --git a/pyfpdb/fpdb_import.py b/pyfpdb/fpdb_import.py index b652f204..6960de03 100755 --- a/pyfpdb/fpdb_import.py +++ b/pyfpdb/fpdb_import.py @@ -36,136 +36,67 @@ import os import datetime import fpdb_simple import fpdb_parse_logic +from optparse import OptionParser from time import time class Importer: - def __init__(self, caller, settings): + def __init__(self): """Constructor""" - self.settings=settings - self.caller=caller + self.settings={'imp-callFpdbHud':False} self.db = None self.cursor = None - self.filelist = [] - self.dirlist = [] - self.monitor = False - self.updated = 0 #Time last import was run, used as mtime reference + self.options = None self.callHud = False self.lines = None self.pos_in_file = {} # dict to remember how far we have read in the file - #Set defaults - if not self.settings.has_key('imp-callFpdbHud'): - self.settings['imp-callFpdbHud'] = False - if not self.settings.has_key('minPrint'): - self.settings['minPrint'] = 30 - self.dbConnect() - def dbConnect(self): + def dbConnect(self, options, settings): #connect to DB - if self.settings['db-backend'] == 2: + if settings['db-backend'] == 2: if not mysqlLibFound: raise fpdb_simple.FpdbError("interface library MySQLdb not found but MySQL selected as backend - please install the library or change the config file") - self.db = MySQLdb.connect(self.settings['db-host'], self.settings['db-user'], - self.settings['db-password'], self.settings['db-databaseName']) - elif self.settings['db-backend'] == 3: + self.db = MySQLdb.connect(host = options.server, user = options.user, + passwd = options.password, db = options.database) + elif settings['db-backend'] == 3: if not pgsqlLibFound: raise fpdb_simple.FpdbError("interface library psycopg2 not found but PostgreSQL selected as backend - please install the library or change the config file") - self.db = psycopg2.connect(self.settings['db-host'], self.settings['db-user'], - self.settings['db-password'], self.settings['db-databaseName']) - elif self.settings['db-backend'] == 4: + self.db = psycopg2.connect(host = options.server, user = options.user, + password = options.password, database = options.database) + elif settings['db-backend'] == 4: pass else: pass self.cursor = self.db.cursor() - #Set functions def setCallHud(self, value): self.callHud = value - def setMinPrint(self, value): - self.settings['minPrint'] = int(value) - - def setHandCount(self, value): - self.settings['handCount'] = int(value) - - def setQuiet(self, value): - self.settings['quiet'] = value - - def setFailOnError(self, value): - self.settings['failOnError'] = value - - def setWatchTime(self): - self.updated = time() - - def clearFileList(self): - self.filelist = [] - - #Add an individual file to filelist - def addImportFile(self, filename): - #todo: test it is a valid file - self.filelist = self.filelist + [filename] - #Remove duplicates - self.filelist = list(set(self.filelist)) - - #Add a directory of files to filelist - def addImportDirectory(self,dir,monitor = False): - #todo: test it is a valid directory - if monitor == True: - self.monitor = True - self.dirlist = self.dirlist + [dir] - - for file in os.listdir(dir): - if os.path.isdir(file): - print "BulkImport is not recursive - please select the final directory in which the history files are" - else: - self.filelist = self.filelist + [os.path.join(dir, file)] - #Remove duplicates - self.filelist = list(set(self.filelist)) - - #Run full import on filelist - def runImport(self): - for file in self.filelist: - self.import_file_dict(file) - - #Run import on updated files, then store latest update time. - def runUpdated(self): - #Check for new files in directory - #todo: make efficient - always checks for new file, should be able to use mtime of directory - # ^^ May not work on windows - for dir in self.dirlist: - for file in os.listdir(dir): - self.filelist = self.filelist + [dir+os.sep+file] - - self.filelist = list(set(self.filelist)) - - for file in self.filelist: - stat_info = os.stat(file) - if stat_info.st_mtime > self.updated: - self.import_file_dict(file) - self.updated = time() - - # This is now an internal function that should not be called directly. - def import_file_dict(self, file): + def import_file_dict(self, options, settings): starttime = time() last_read_hand=0 loc = 0 - if (file=="stdin"): + if (options.inputFile=="stdin"): inputFile=sys.stdin else: - inputFile=open(file, "rU") - try: loc = self.pos_in_file[file] + inputFile=open(options.inputFile, "rU") + try: loc = self.pos_in_file[options.inputFile] except: pass + self.dbConnect(options,settings) + # Read input file into class and close file inputFile.seek(loc) self.lines=fpdb_simple.removeTrailingEOL(inputFile.readlines()) - self.pos_in_file[file] = inputFile.tell() + self.pos_in_file[options.inputFile] = inputFile.tell() inputFile.close() firstline = self.lines[0] if firstline.find("Tournament Summary")!=-1: print "TODO: implement importing tournament summaries" + self.cursor.close() + self.db.close() return 0 site=fpdb_simple.recogniseSite(firstline) @@ -211,8 +142,7 @@ class Importer: if not isTourney: fpdb_simple.filterAnteBlindFold(site,hand) hand=fpdb_simple.filterCrap(site, hand, isTourney) - self.hand=hand - + try: handsId=fpdb_parse_logic.mainParser(self.db, self.cursor, site, category, hand) self.db.commit() @@ -220,36 +150,42 @@ class Importer: stored+=1 self.db.commit() # if settings['imp-callFpdbHud'] and self.callHud and os.sep=='/': - if self.settings['imp-callFpdbHud'] and self.callHud: + if settings['imp-callFpdbHud'] and self.callHud: #print "call to HUD here. handsId:",handsId #pipe the Hands.id out to the HUD - self.caller.pipe_to_hud.stdin.write("%s" % (handsId) + os.linesep) +# options.pipe_to_hud.write("%s" % (handsId) + os.linesep) + print "handsID = ", handsID + options.pipe_to_hud.stdin.write("%s" % (handsId) + os.linesep) except fpdb_simple.DuplicateError: duplicates+=1 except (ValueError), fe: errors+=1 - self.printEmailErrorMessage(errors, file, hand[0]) + self.printEmailErrorMessage(errors, options.inputFile, hand[0]) - if (self.settings['failOnError']): - self.db.commit() #dont remove this, in case hand processing was cancelled. + if (options.failOnError): + self.db.commit() #dont remove this, in case hand processing was cancelled this ties up any open ends. + self.cursor.close() + self.db.close() raise except (fpdb_simple.FpdbError), fe: errors+=1 - self.printEmailErrorMessage(errors, file, hand[0]) + self.printEmailErrorMessage(errors, options.inputFile, hand[0]) #fe.printStackTrace() #todo: get stacktrace self.db.rollback() - if (self.settings['failOnError']): - self.db.commit() #dont remove this, in case hand processing was cancelled. + if (options.failOnError): + self.db.commit() #dont remove this, in case hand processing was cancelled this ties up any open ends. + self.cursor.close() + self.db.close() raise - if (self.settings['minPrint']!=0): - if ((stored+duplicates+partial+errors)%self.settings['minPrint']==0): + if (options.minPrint!=0): + if ((stored+duplicates+partial+errors)%options.minPrint==0): print "stored:", stored, "duplicates:", duplicates, "partial:", partial, "errors:", errors - if (self.settings['handCount']!=0): - if ((stored+duplicates+partial+errors)>=self.settings['handCount']): - if (not self.settings['quiet']): + if (options.handCount!=0): + if ((stored+duplicates+partial+errors)>=options.handCount): + if (not options.quiet): print "quitting due to reaching the amount of hands to be imported" print "Total stored:", stored, "duplicates:", duplicates, "partial/damaged:", partial, "errors:", errors, " time:", (time() - starttime) sys.exit(0) @@ -267,14 +203,16 @@ class Importer: handsId=0 #todo: this will cause return of an unstored hand number if the last hand was error or partial self.db.commit() + self.cursor.close() + self.db.close() return handsId #end def import_file_dict def printEmailErrorMessage(self, errors, filename, line): print "Error No.",errors,", please send the hand causing this to steffen@sycamoretest.info so I can fix it." - print "Filename:", filename + print "Filename:",options.inputFile print "Here is the first line so you can identify it. Please mention that the error was a ValueError:" - print self.hand[0] + print hand[0] if __name__ == "__main__":