various changes with threaded HUD_main.py

This commit is contained in:
Ray 2008-10-16 15:15:28 -04:00
parent 81f5450a17
commit b35e0e4880
3 changed files with 53 additions and 108 deletions

View File

@ -79,9 +79,11 @@ class Database:
self.connection.close() self.connection.close()
def get_table_name(self, hand_id): def get_table_name(self, hand_id):
print "searching for ", hand_id
c = self.connection.cursor() c = self.connection.cursor()
c.execute(self.sql.query['get_table_name'], (hand_id, )) c.execute(self.sql.query['get_table_name'], (hand_id, ))
row = c.fetchone() row = c.fetchone()
print "found = ", row
return row return row
def get_last_hand(self): def get_last_hand(self):

View File

@ -36,6 +36,7 @@ Main for FreePokerTools HUD.
import sys import sys
import os import os
import thread import thread
import time
errorfile = open('HUD-error.txt', 'w', 0) errorfile = open('HUD-error.txt', 'w', 0)
sys.stderr = errorfile sys.stderr = errorfile
@ -91,6 +92,7 @@ def producer(): # This is the thread function
while True: # wait for a new hand number on stdin while True: # wait for a new hand number on stdin
new_hand_id = sys.stdin.readline() new_hand_id = sys.stdin.readline()
print "hand = ", new_hand_id
if new_hand_id == "": # blank line means quit if new_hand_id == "": # blank line means quit
destroy() destroy()
@ -98,9 +100,10 @@ def producer(): # This is the thread function
for h in hud_dict.keys(): for h in hud_dict.keys():
if hud_dict[h].deleted: if hud_dict[h].deleted:
del(hud_dict[h]) del(hud_dict[h])
print "getting table name"
(table_name, max, poker_game) = db_connection.get_table_name(new_hand_id) (table_name, max, poker_game) = db_connection.get_table_name(new_hand_id)
stat_dict = db_connection.get_stats_from_hand(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 a hud for this table exists, just update it
if hud_dict.has_key(table_name): if hud_dict.has_key(table_name):
@ -108,8 +111,10 @@ def producer(): # This is the thread function
# otherwise create a new hud # otherwise create a new hud
else: else:
table_windows = Tables.discover(config) table_windows = Tables.discover(config)
print "searching for %s" % (table_name)
for t in table_windows.keys(): for t in table_windows.keys():
if table_windows[t].name == table_name: 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) create_HUD(new_hand_id, table_windows[t], db_name, table_name, max, poker_game, db_connection, config, stat_dict)
break break

View File

@ -36,136 +36,67 @@ import os
import datetime import datetime
import fpdb_simple import fpdb_simple
import fpdb_parse_logic import fpdb_parse_logic
from optparse import OptionParser
from time import time from time import time
class Importer: class Importer:
def __init__(self, caller, settings): def __init__(self):
"""Constructor""" """Constructor"""
self.settings=settings self.settings={'imp-callFpdbHud':False}
self.caller=caller
self.db = None self.db = None
self.cursor = None self.cursor = None
self.filelist = [] self.options = None
self.dirlist = []
self.monitor = False
self.updated = 0 #Time last import was run, used as mtime reference
self.callHud = False self.callHud = False
self.lines = None self.lines = None
self.pos_in_file = {} # dict to remember how far we have read in the file 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 #connect to DB
if self.settings['db-backend'] == 2: if settings['db-backend'] == 2:
if not mysqlLibFound: 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") 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.db = MySQLdb.connect(host = options.server, user = options.user,
self.settings['db-password'], self.settings['db-databaseName']) passwd = options.password, db = options.database)
elif self.settings['db-backend'] == 3: elif settings['db-backend'] == 3:
if not pgsqlLibFound: 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") 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.db = psycopg2.connect(host = options.server, user = options.user,
self.settings['db-password'], self.settings['db-databaseName']) password = options.password, database = options.database)
elif self.settings['db-backend'] == 4: elif settings['db-backend'] == 4:
pass pass
else: else:
pass pass
self.cursor = self.db.cursor() self.cursor = self.db.cursor()
#Set functions
def setCallHud(self, value): def setCallHud(self, value):
self.callHud = value self.callHud = value
def setMinPrint(self, value): def import_file_dict(self, options, settings):
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):
starttime = time() starttime = time()
last_read_hand=0 last_read_hand=0
loc = 0 loc = 0
if (file=="stdin"): if (options.inputFile=="stdin"):
inputFile=sys.stdin inputFile=sys.stdin
else: else:
inputFile=open(file, "rU") inputFile=open(options.inputFile, "rU")
try: loc = self.pos_in_file[file] try: loc = self.pos_in_file[options.inputFile]
except: pass except: pass
self.dbConnect(options,settings)
# Read input file into class and close file # Read input file into class and close file
inputFile.seek(loc) inputFile.seek(loc)
self.lines=fpdb_simple.removeTrailingEOL(inputFile.readlines()) self.lines=fpdb_simple.removeTrailingEOL(inputFile.readlines())
self.pos_in_file[file] = inputFile.tell() self.pos_in_file[options.inputFile] = inputFile.tell()
inputFile.close() inputFile.close()
firstline = self.lines[0] firstline = self.lines[0]
if firstline.find("Tournament Summary")!=-1: if firstline.find("Tournament Summary")!=-1:
print "TODO: implement importing tournament summaries" print "TODO: implement importing tournament summaries"
self.cursor.close()
self.db.close()
return 0 return 0
site=fpdb_simple.recogniseSite(firstline) site=fpdb_simple.recogniseSite(firstline)
@ -211,7 +142,6 @@ class Importer:
if not isTourney: if not isTourney:
fpdb_simple.filterAnteBlindFold(site,hand) fpdb_simple.filterAnteBlindFold(site,hand)
hand=fpdb_simple.filterCrap(site, hand, isTourney) hand=fpdb_simple.filterCrap(site, hand, isTourney)
self.hand=hand
try: try:
handsId=fpdb_parse_logic.mainParser(self.db, self.cursor, site, category, hand) handsId=fpdb_parse_logic.mainParser(self.db, self.cursor, site, category, hand)
@ -220,36 +150,42 @@ class Importer:
stored+=1 stored+=1
self.db.commit() self.db.commit()
# if settings['imp-callFpdbHud'] and self.callHud and os.sep=='/': # 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 #print "call to HUD here. handsId:",handsId
#pipe the Hands.id out to the HUD #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: except fpdb_simple.DuplicateError:
duplicates+=1 duplicates+=1
except (ValueError), fe: except (ValueError), fe:
errors+=1 errors+=1
self.printEmailErrorMessage(errors, file, hand[0]) self.printEmailErrorMessage(errors, options.inputFile, hand[0])
if (self.settings['failOnError']): if (options.failOnError):
self.db.commit() #dont remove this, in case hand processing was cancelled. 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 raise
except (fpdb_simple.FpdbError), fe: except (fpdb_simple.FpdbError), fe:
errors+=1 errors+=1
self.printEmailErrorMessage(errors, file, hand[0]) self.printEmailErrorMessage(errors, options.inputFile, hand[0])
#fe.printStackTrace() #todo: get stacktrace #fe.printStackTrace() #todo: get stacktrace
self.db.rollback() self.db.rollback()
if (self.settings['failOnError']): if (options.failOnError):
self.db.commit() #dont remove this, in case hand processing was cancelled. 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 raise
if (self.settings['minPrint']!=0): if (options.minPrint!=0):
if ((stored+duplicates+partial+errors)%self.settings['minPrint']==0): if ((stored+duplicates+partial+errors)%options.minPrint==0):
print "stored:", stored, "duplicates:", duplicates, "partial:", partial, "errors:", errors print "stored:", stored, "duplicates:", duplicates, "partial:", partial, "errors:", errors
if (self.settings['handCount']!=0): if (options.handCount!=0):
if ((stored+duplicates+partial+errors)>=self.settings['handCount']): if ((stored+duplicates+partial+errors)>=options.handCount):
if (not self.settings['quiet']): if (not options.quiet):
print "quitting due to reaching the amount of hands to be imported" 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) print "Total stored:", stored, "duplicates:", duplicates, "partial/damaged:", partial, "errors:", errors, " time:", (time() - starttime)
sys.exit(0) sys.exit(0)
@ -267,14 +203,16 @@ class Importer:
handsId=0 handsId=0
#todo: this will cause return of an unstored hand number if the last hand was error or partial #todo: this will cause return of an unstored hand number if the last hand was error or partial
self.db.commit() self.db.commit()
self.cursor.close()
self.db.close()
return handsId return handsId
#end def import_file_dict #end def import_file_dict
def printEmailErrorMessage(self, errors, filename, line): 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 "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 "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__": if __name__ == "__main__":