From 79651706f67651602b3d612f62ea53958ee04f06 Mon Sep 17 00:00:00 2001 From: Worros Date: Wed, 8 Oct 2008 19:53:25 +0800 Subject: [PATCH 1/8] Make GuiGraphViewer use the query file. Make minor adjustment to Graph --- pyfpdb/FpdbSQLQueries.py | 36 ++++++++++++++++++++++++++++++++ pyfpdb/GuiGraphViewer.py | 44 +++++++++++++++++++--------------------- pyfpdb/fpdb.py | 5 ++++- 3 files changed, 61 insertions(+), 24 deletions(-) diff --git a/pyfpdb/FpdbSQLQueries.py b/pyfpdb/FpdbSQLQueries.py index 7c1e2ec5..e582c681 100644 --- a/pyfpdb/FpdbSQLQueries.py +++ b/pyfpdb/FpdbSQLQueries.py @@ -580,6 +580,42 @@ class FpdbSQLQueries: elif(self.dbname == 'SQLite'): self.query['createHudCacheTable'] = """ """ + ################################ + # Queries used in GuiGraphViewer + ################################ + + + # Returns all cash game handIds and the money won(winnings is the final pot) + # by the playerId for a single site. + if(self.dbname == 'MySQL InnoDB') or (self.dbname == 'PostgreSQL'): + self.query['getRingWinningsAllGamesPlayerIdSite'] = """SELECT handId, winnings FROM HandsPlayers + INNER JOIN Players ON HandsPlayers.playerId = Players.id + INNER JOIN Hands ON Hands.id = HandsPlayers.handId + WHERE Players.name = %s AND Players.siteId = %s AND (tourneysPlayersId IS NULL) + ORDER BY handStart""" + elif(self.dbname == 'SQLite'): + #Probably doesn't work. + self.query['getRingWinningsAllGamesPlayerIdSite'] = """SELECT handId, winnings FROM HandsPlayers + INNER JOIN Players ON HandsPlayers.playerId = Players.id + INNER JOIN Hands ON Hands.id = HandsPlayers.handId + WHERE Players.name = %s AND Players.siteId = %s AND (tourneysPlayersId IS NULL) + ORDER BY handStart""" + + # Returns the profit for a given ring game handId, Total pot - money invested by playerId + if(self.dbname == 'MySQL InnoDB') or (self.dbname == 'PostgreSQL'): + self.query['getRingProfitFromHandId'] = """SELECT SUM(amount) FROM HandsActions + INNER JOIN HandsPlayers ON HandsActions.handPlayerId = HandsPlayers.id + INNER JOIN Players ON HandsPlayers.playerId = Players.id + WHERE Players.name = %s AND HandsPlayers.handId = %s + AND Players.siteId = %s AND (tourneysPlayersId IS NULL)""" + elif(self.dbname == 'SQLite'): + #Probably doesn't work. + self.query['getRingProfitFromHandId'] = """SELECT SUM(amount) FROM HandsActions + INNER JOIN HandsPlayers ON HandsActions.handPlayerId = HandsPlayers.id + INNER JOIN Players ON HandsPlayers.playerId = Players.id + WHERE Players.name = %s AND HandsPlayers.handId = %s + AND Players.siteId = %s AND (tourneysPlayersId IS NULL)""" + if __name__== "__main__": from optparse import OptionParser diff --git a/pyfpdb/GuiGraphViewer.py b/pyfpdb/GuiGraphViewer.py index e441f8ba..5b3fa081 100644 --- a/pyfpdb/GuiGraphViewer.py +++ b/pyfpdb/GuiGraphViewer.py @@ -69,30 +69,12 @@ class GuiGraphViewer (threading.Thread): self.ax.set_xlabel("Hands", fontsize = 12) self.ax.set_ylabel("$", fontsize = 12) self.ax.grid(color='g', linestyle=':', linewidth=0.2) - - self.cursor.execute("""SELECT handId, winnings FROM HandsPlayers - INNER JOIN Players ON HandsPlayers.playerId = Players.id - INNER JOIN Hands ON Hands.id = HandsPlayers.handId - WHERE Players.name = %s AND Players.siteId = %s AND (tourneysPlayersId IS NULL) - ORDER BY siteHandNo""", (name, site)) - winnings = self.db.cursor.fetchall() - - profit=range(len(winnings)) - for i in profit: - self.cursor.execute("""SELECT SUM(amount) FROM HandsActions - INNER JOIN HandsPlayers ON HandsActions.handPlayerId = HandsPlayers.id - INNER JOIN Players ON HandsPlayers.playerId = Players.id - WHERE Players.name = %s AND HandsPlayers.handId = %s AND Players.siteId = %s AND (tourneysPlayersId IS NULL)""", (name, winnings[i][0], site)) - spent = self.db.cursor.fetchone() - profit[i]=(i, winnings[i][1]-spent[0]) - - y=map(lambda x:float(x[1]), profit) - line = cumsum(y) - line = line/100 - self.ax.annotate ("All Hands, Site %s", (61,25), xytext =(0.1, 0.9) , textcoords ="axes fraction" ,) - #Now draw plot + #Get graph data from DB + line = self.getRingProfitGraph(name, site) + + #Draw plot self.ax.plot(line,) self.canvas = FigureCanvas(self.fig) # a gtk.DrawingArea @@ -100,13 +82,29 @@ class GuiGraphViewer (threading.Thread): self.canvas.show() #end of def showClicked - def __init__(self, db, settings, debug=True): + def getRingProfitGraph(self, name, site): + self.cursor.execute(self.sql.query['getRingWinningsAllGamesPlayerIdSite'], (name, site)) + winnings = self.db.cursor.fetchall() + + profit=range(len(winnings)) + for i in profit: + self.cursor.execute(self.sql.query['getRingProfitFromHandId'], (name, winnings[i][0], site)) + spent = self.db.cursor.fetchone() + profit[i]=(i, winnings[i][1]-spent[0]) + + y=map(lambda x:float(x[1]), profit) + line = cumsum(y) + return line/100 + #end of def getRingProfitGraph + + def __init__(self, db, settings, querylist, debug=True): """Constructor for GraphViewer""" self.debug=debug #print "start of GraphViewer constructor" self.db=db self.cursor=db.cursor self.settings=settings + self.sql=querylist self.mainVBox = gtk.VBox(False, 0) self.mainVBox.show() diff --git a/pyfpdb/fpdb.py b/pyfpdb/fpdb.py index 841d71c0..8c8ac5e9 100755 --- a/pyfpdb/fpdb.py +++ b/pyfpdb/fpdb.py @@ -344,6 +344,9 @@ class fpdb: response = diaDbVersionWarning.run() diaDbVersionWarning.destroy() + + # Database connected to successfully, load queries to pass on to other classes + self.querydict = FpdbSQLQueries.FpdbSQLQueries(self.db.get_backend_name()) #end def load_profile def not_implemented(self): @@ -407,7 +410,7 @@ This program is licensed under the AGPL3, see docs"""+os.sep+"agpl-3.0.txt") def tabGraphViewer(self, widget, data): """opens a graph viewer tab""" #print "start of tabGraphViewer" - new_gv_thread=GuiGraphViewer.GuiGraphViewer(self.db, self.settings) + new_gv_thread=GuiGraphViewer.GuiGraphViewer(self.db, self.settings,self.querydict) self.threads.append(new_gv_thread) gv_tab=new_gv_thread.get_vbox() self.add_and_display_tab(gv_tab, "Graphs") From 98b556f42c67bf4571dc71ea60d2605c6dd442b9 Mon Sep 17 00:00:00 2001 From: Worros Date: Thu, 9 Oct 2008 01:36:08 +0800 Subject: [PATCH 2/8] Turn fpdb_import functions into class Importer Fix all callers of fpdb_import --- pyfpdb/GuiAutoImport.py | 3 ++- pyfpdb/GuiBulkImport.py | 5 +++-- pyfpdb/GuiTableViewer.py | 3 ++- pyfpdb/HUD_main.py | 0 pyfpdb/RegressionTest.py | 6 +++--- pyfpdb/fpdb.py | 1 + pyfpdb/fpdb_import.py | 25 +++++++++++++++---------- 7 files changed, 26 insertions(+), 17 deletions(-) mode change 100644 => 100755 pyfpdb/HUD_main.py diff --git a/pyfpdb/GuiAutoImport.py b/pyfpdb/GuiAutoImport.py index 5081022f..4b594d52 100644 --- a/pyfpdb/GuiAutoImport.py +++ b/pyfpdb/GuiAutoImport.py @@ -57,7 +57,7 @@ class GuiAutoImport (threading.Thread): self.inputFile = os.path.join(self.path, file) stat_info = os.stat(self.inputFile) if not self.import_files.has_key(self.inputFile) or stat_info.st_mtime > self.import_files[self.inputFile]: - fpdb_import.import_file_dict(self, self.settings, callHud = True) + self.importer.import_file_dict(self, self.settings, callHud = True) self.import_files[self.inputFile] = stat_info.st_mtime print "GuiAutoImport.import_dir done" @@ -121,6 +121,7 @@ class GuiAutoImport (threading.Thread): def __init__(self, settings, debug=True): """Constructor for GuiAutoImport""" self.settings=settings + self.importer = fpdb_import.Importer() self.server=settings['db-host'] self.user=settings['db-user'] diff --git a/pyfpdb/GuiBulkImport.py b/pyfpdb/GuiBulkImport.py index 0a6bd10d..cd1277b6 100644 --- a/pyfpdb/GuiBulkImport.py +++ b/pyfpdb/GuiBulkImport.py @@ -32,7 +32,7 @@ class GuiBulkImport (threading.Thread): print "BulkImport is not recursive - please select the final directory in which the history files are" else: self.inputFile=self.path+os.sep+file - fpdb_import.import_file_dict(self, self.settings, False) + self.importer.import_file_dict(self, self.settings, False) print "GuiBulkImport.import_dir done" def load_clicked(self, widget, data=None): @@ -69,7 +69,7 @@ class GuiBulkImport (threading.Thread): if os.path.isdir(self.inputFile): self.import_dir() else: - fpdb_import.import_file_dict(self, self.settings, False) + self.importer.import_file_dict(self, self.settings, False) def get_vbox(self): """returns the vbox of this thread""" @@ -83,6 +83,7 @@ class GuiBulkImport (threading.Thread): def __init__(self, db, settings): self.db=db self.settings=settings + self.importer = fpdb_import.Importer() self.vbox=gtk.VBox(False,1) self.vbox.show() diff --git a/pyfpdb/GuiTableViewer.py b/pyfpdb/GuiTableViewer.py index 15a21149..3582971f 100644 --- a/pyfpdb/GuiTableViewer.py +++ b/pyfpdb/GuiTableViewer.py @@ -255,8 +255,9 @@ class GuiTableViewer (threading.Thread): self.failOnError=False self.minPrint=0 self.handCount=0 + self.importer = fpdb_import.Importer() - self.last_read_hand_id=fpdb_import.import_file_dict(self, self.settings, False) + self.last_read_hand_id=importer.import_file_dict(self, self.settings, False) #end def table_viewer.import_clicked def all_clicked(self, widget, data): diff --git a/pyfpdb/HUD_main.py b/pyfpdb/HUD_main.py old mode 100644 new mode 100755 diff --git a/pyfpdb/RegressionTest.py b/pyfpdb/RegressionTest.py index db1a88b9..8a8facc5 100644 --- a/pyfpdb/RegressionTest.py +++ b/pyfpdb/RegressionTest.py @@ -57,19 +57,19 @@ class TestSequenceFunctions(unittest.TestCase): print self.pgdict.query['list_tables'] self.result = self.pg_db.cursor.execute(self.pgdict.query['list_tables']) - self.failUnless(self.result==13, "Number of tables in database incorrect. Expected 13 got " + str(self.result)) + self.failUnless(self.result==13, "Number of tables in database incorrect. Expected 13 got " + str(self.result)) def testMySQLRecreateTables(self): """Test droping then recreating fpdb table schema""" self.mysql_db.recreate_tables() self.result = self.mysql_db.cursor.execute("SHOW TABLES") - self.failUnless(self.result==13, "Number of tables in database incorrect. Expected 13 got " + str(self.result)) + self.failUnless(self.result==13, "Number of tables in database incorrect. Expected 13 got " + str(self.result)) def testPostgresSQLRecreateTables(self): """Test droping then recreating fpdb table schema""" self.pg_db.recreate_tables() self.result = self.pg_db.cursor.execute(self.pgdict.query['list_tables']) - self.failUnless(self.result==13, "Number of tables in database incorrect. Expected 13 got " + str(self.result)) + self.failUnless(self.result==13, "Number of tables in database incorrect. Expected 13 got " + str(self.result)) if __name__ == '__main__': unittest.main() diff --git a/pyfpdb/fpdb.py b/pyfpdb/fpdb.py index 8c8ac5e9..b4df905c 100755 --- a/pyfpdb/fpdb.py +++ b/pyfpdb/fpdb.py @@ -32,6 +32,7 @@ import GuiBulkImport import GuiTableViewer import GuiAutoImport import GuiGraphViewer +import FpdbSQLQueries class fpdb: def tab_clicked(self, widget, tab_name): diff --git a/pyfpdb/fpdb_import.py b/pyfpdb/fpdb_import.py index 9c52d6bc..6fe8e28a 100755 --- a/pyfpdb/fpdb_import.py +++ b/pyfpdb/fpdb_import.py @@ -38,17 +38,22 @@ import fpdb_simple import fpdb_parse_logic from optparse import OptionParser +class Importer: -def import_file(server, database, user, password, inputFile): - self.server=server - self.database=database - self.user=user - self.password=password - self.inputFile=inputFile - self.settings={'imp-callFpdbHud':False} - import_file_dict(self, settings) + def __init__(self): + """Constructor""" -def import_file_dict(options, settings, callHud=False): + + def import_file(self, server, database, user, password, inputFile): + self.server=server + self.database=database + self.user=user + self.password=password + self.inputFile=inputFile + self.settings={'imp-callFpdbHud':False} + self.import_file_dict(self, settings) + + def import_file_dict(self, options, settings, callHud=False): last_read_hand=0 if (options.inputFile=="stdin"): inputFile=sys.stdin @@ -225,4 +230,4 @@ if __name__ == "__main__": (options, sys.argv) = parser.parse_args() settings={'imp-callFpdbHud':False, 'db-backend':2} - import_file_dict(options, settings, False) +# import_file_dict(options, settings, False) From 14ab9f68145e8bead9238d408759b5a00f3ec388 Mon Sep 17 00:00:00 2001 From: Worros Date: Thu, 9 Oct 2008 01:48:16 +0800 Subject: [PATCH 3/8] Kill command line interface to fpdb_import until restructured --- pyfpdb/fpdb_import.py | 37 +------------------------------------ 1 file changed, 1 insertion(+), 36 deletions(-) diff --git a/pyfpdb/fpdb_import.py b/pyfpdb/fpdb_import.py index 6fe8e28a..e65a7ece 100755 --- a/pyfpdb/fpdb_import.py +++ b/pyfpdb/fpdb_import.py @@ -42,16 +42,7 @@ class Importer: def __init__(self): """Constructor""" - - - def import_file(self, server, database, user, password, inputFile): - self.server=server - self.database=database - self.user=user - self.password=password - self.inputFile=inputFile self.settings={'imp-callFpdbHud':False} - self.import_file_dict(self, settings) def import_file_dict(self, options, settings, callHud=False): last_read_hand=0 @@ -204,30 +195,4 @@ class Importer: if __name__ == "__main__": - failOnError=False - quiet=False - - #process CLI parameters - parser = OptionParser() - parser.add_option("-c", "--handCount", default="0", type="int", - help="Number of hands to import (default 0 means unlimited)") - parser.add_option("-d", "--database", default="fpdb", help="The MySQL database to use (default fpdb)") - parser.add_option("-e", "--errorFile", default="failed.txt", - help="File to store failed hands into. (default: failed.txt) Not implemented.") - parser.add_option("-f", "--inputFile", "--file", "--inputfile", default="stdin", - help="The file you want to import (remember to use quotes if necessary)") - parser.add_option("-m", "--minPrint", "--status", default="50", type="int", - help="How often to print a one-line status report (0 means never, default is 50)") - parser.add_option("-p", "--password", help="The password for the MySQL user") - parser.add_option("-q", "--quiet", action="store_true", - help="If this is passed it doesn't print a total at the end nor the opening line. Note that this purposely does NOT change --minPrint") - parser.add_option("-s", "--server", default="localhost", - help="Hostname/IP of the MySQL server (default localhost)") - parser.add_option("-u", "--user", default="fpdb", help="The MySQL username (default fpdb)") - parser.add_option("-x", "--failOnError", action="store_true", - help="If this option is passed it quits when it encounters any error") - - (options, sys.argv) = parser.parse_args() - - settings={'imp-callFpdbHud':False, 'db-backend':2} -# import_file_dict(options, settings, False) + print "CLI for fpdb_import is currently on vacation please check in later" From 037178ead3698a3175a13a17bd26ef339dbcad6f Mon Sep 17 00:00:00 2001 From: Worros Date: Fri, 10 Oct 2008 00:13:56 +0800 Subject: [PATCH 4/8] Shift db connection code to its own function Add class vars for db and cursor --- pyfpdb/fpdb_import.py | 71 ++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/pyfpdb/fpdb_import.py b/pyfpdb/fpdb_import.py index 22715ec1..53b5362b 100755 --- a/pyfpdb/fpdb_import.py +++ b/pyfpdb/fpdb_import.py @@ -43,6 +43,27 @@ class Importer: def __init__(self): """Constructor""" self.settings={'imp-callFpdbHud':False} + self.db = None + self.cursor = None + self.options = None + + def dbConnect(self, options, settings): + #connect to DB + 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(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(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() def import_file_dict(self, options, settings, callHud=False): last_read_hand=0 @@ -51,33 +72,15 @@ class Importer: else: inputFile=open(options.inputFile, "rU") - #connect to DB - 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") - 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") - db = psycopg2.connect(host = options.server, user = options.user, - password = options.password, database = options.database) - elif settings['db-backend'] == 4: - pass - else: - pass - cursor = db.cursor() - - if (not options.quiet): - print "Opened file", options.inputFile, "and connected to MySQL on", options.server + self.dbConnect(options,settings) line=inputFile.readline() if line.find("Tournament Summary")!=-1: print "TODO: implement importing tournament summaries" inputFile.close() - cursor.close() - db.close() + self.cursor.close() + self.db.close() return 0 site=fpdb_simple.recogniseSite(line) @@ -127,11 +130,11 @@ class Importer: hand=fpdb_simple.filterCrap(site, hand, isTourney) try: - handsId=fpdb_parse_logic.mainParser(db, cursor, site, category, hand) - db.commit() + handsId=fpdb_parse_logic.mainParser(self.db, self.cursor, site, category, hand) + self.db.commit() stored+=1 - db.commit() + self.db.commit() # if settings['imp-callFpdbHud'] and callHud and os.sep=='/': if settings['imp-callFpdbHud'] and callHud: #print "call to HUD here. handsId:",handsId @@ -148,10 +151,10 @@ class Importer: print hand[0] if (options.failOnError): - db.commit() #dont remove this, in case hand processing was cancelled this ties up any open ends. + self.db.commit() #dont remove this, in case hand processing was cancelled this ties up any open ends. inputFile.close() - cursor.close() - db.close() + self.cursor.close() + self.db.close() raise except (fpdb_simple.FpdbError), fe: errors+=1 @@ -160,13 +163,13 @@ class Importer: print "Here is the first line so you can identify it." print hand[0] #fe.printStackTrace() #todo: get stacktrace - db.rollback() + self.db.rollback() if (options.failOnError): - db.commit() #dont remove this, in case hand processing was cancelled this ties up any open ends. + self.db.commit() #dont remove this, in case hand processing was cancelled this ties up any open ends. inputFile.close() - cursor.close() - db.close() + self.cursor.close() + self.db.close() raise if (options.minPrint!=0): if ((stored+duplicates+partial+errors)%options.minPrint==0): @@ -191,10 +194,10 @@ class Importer: print "failed to read a single hand from file:", inputFile handsId=0 #todo: this will cause return of an unstored hand number if the last hand was error or partial - db.commit() + self.db.commit() inputFile.close() - cursor.close() - db.close() + self.cursor.close() + self.db.close() return handsId #end def import_file_dict From ed7122ca31a5b12742e1da576f49090f77e37fef Mon Sep 17 00:00:00 2001 From: Worros Date: Fri, 10 Oct 2008 01:21:01 +0800 Subject: [PATCH 5/8] Move callHud to class attribute and remove from function parameters Fix all callers --- pyfpdb/GuiAutoImport.py | 3 ++- pyfpdb/GuiBulkImport.py | 4 ++-- pyfpdb/GuiTableViewer.py | 2 +- pyfpdb/fpdb_import.py | 10 +++++++--- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/pyfpdb/GuiAutoImport.py b/pyfpdb/GuiAutoImport.py index 4b594d52..b155b9f4 100644 --- a/pyfpdb/GuiAutoImport.py +++ b/pyfpdb/GuiAutoImport.py @@ -57,7 +57,7 @@ class GuiAutoImport (threading.Thread): self.inputFile = os.path.join(self.path, file) stat_info = os.stat(self.inputFile) if not self.import_files.has_key(self.inputFile) or stat_info.st_mtime > self.import_files[self.inputFile]: - self.importer.import_file_dict(self, self.settings, callHud = True) + self.importer.import_file_dict(self, self.settings) self.import_files[self.inputFile] = stat_info.st_mtime print "GuiAutoImport.import_dir done" @@ -122,6 +122,7 @@ class GuiAutoImport (threading.Thread): """Constructor for GuiAutoImport""" self.settings=settings self.importer = fpdb_import.Importer() + self.importer.setCallHud(True) self.server=settings['db-host'] self.user=settings['db-user'] diff --git a/pyfpdb/GuiBulkImport.py b/pyfpdb/GuiBulkImport.py index cd1277b6..4bd221cb 100644 --- a/pyfpdb/GuiBulkImport.py +++ b/pyfpdb/GuiBulkImport.py @@ -32,7 +32,7 @@ class GuiBulkImport (threading.Thread): print "BulkImport is not recursive - please select the final directory in which the history files are" else: self.inputFile=self.path+os.sep+file - self.importer.import_file_dict(self, self.settings, False) + self.importer.import_file_dict(self, self.settings) print "GuiBulkImport.import_dir done" def load_clicked(self, widget, data=None): @@ -69,7 +69,7 @@ class GuiBulkImport (threading.Thread): if os.path.isdir(self.inputFile): self.import_dir() else: - self.importer.import_file_dict(self, self.settings, False) + self.importer.import_file_dict(self, self.settings) def get_vbox(self): """returns the vbox of this thread""" diff --git a/pyfpdb/GuiTableViewer.py b/pyfpdb/GuiTableViewer.py index 3582971f..6b5cb398 100644 --- a/pyfpdb/GuiTableViewer.py +++ b/pyfpdb/GuiTableViewer.py @@ -257,7 +257,7 @@ class GuiTableViewer (threading.Thread): self.handCount=0 self.importer = fpdb_import.Importer() - self.last_read_hand_id=importer.import_file_dict(self, self.settings, False) + self.last_read_hand_id=importer.import_file_dict(self, self.settings) #end def table_viewer.import_clicked def all_clicked(self, widget, data): diff --git a/pyfpdb/fpdb_import.py b/pyfpdb/fpdb_import.py index 53b5362b..f5a11975 100755 --- a/pyfpdb/fpdb_import.py +++ b/pyfpdb/fpdb_import.py @@ -46,6 +46,7 @@ class Importer: self.db = None self.cursor = None self.options = None + self.callHud = False def dbConnect(self, options, settings): #connect to DB @@ -65,7 +66,10 @@ class Importer: pass self.cursor = self.db.cursor() - def import_file_dict(self, options, settings, callHud=False): + def setCallHud(self, value): + self.callHud = value + + def import_file_dict(self, options, settings): last_read_hand=0 if (options.inputFile=="stdin"): inputFile=sys.stdin @@ -135,8 +139,8 @@ class Importer: stored+=1 self.db.commit() -# if settings['imp-callFpdbHud'] and callHud and os.sep=='/': - if settings['imp-callFpdbHud'] and callHud: +# if settings['imp-callFpdbHud'] and self.callHud and os.sep=='/': + if settings['imp-callFpdbHud'] and self.callHud: #print "call to HUD here. handsId:",handsId #pipe the Hands.id out to the HUD # options.pipe_to_hud.write("%s" % (handsId) + os.linesep) From 5e1362abf90018c20d629ffb09c3ce6e4f3fc74d Mon Sep 17 00:00:00 2001 From: Worros Date: Fri, 10 Oct 2008 02:18:54 +0800 Subject: [PATCH 6/8] Read file in one hit then close. Make lines of for a class member Add timing code --- pyfpdb/fpdb_import.py | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/pyfpdb/fpdb_import.py b/pyfpdb/fpdb_import.py index f5a11975..bb2b5ec9 100755 --- a/pyfpdb/fpdb_import.py +++ b/pyfpdb/fpdb_import.py @@ -37,6 +37,7 @@ import datetime import fpdb_simple import fpdb_parse_logic from optparse import OptionParser +from time import time class Importer: @@ -47,6 +48,7 @@ class Importer: self.cursor = None self.options = None self.callHud = False + self.lines = None def dbConnect(self, options, settings): #connect to DB @@ -70,6 +72,7 @@ class Importer: self.callHud = value def import_file_dict(self, options, settings): + starttime = time() last_read_hand=0 if (options.inputFile=="stdin"): inputFile=sys.stdin @@ -78,19 +81,20 @@ class Importer: self.dbConnect(options,settings) - line=inputFile.readline() - - if line.find("Tournament Summary")!=-1: + # Read input file into class and close file + self.lines=fpdb_simple.removeTrailingEOL(inputFile.readlines()) + inputFile.close() + + firstline = self.lines[0] + + if firstline.find("Tournament Summary")!=-1: print "TODO: implement importing tournament summaries" - inputFile.close() self.cursor.close() self.db.close() return 0 - site=fpdb_simple.recogniseSite(line) - category=fpdb_simple.recogniseCategory(line) - inputFile.seek(0) - lines=fpdb_simple.removeTrailingEOL(inputFile.readlines()) + site=fpdb_simple.recogniseSite(firstline) + category=fpdb_simple.recogniseCategory(firstline) startpos=0 stored=0 #counter @@ -98,10 +102,10 @@ class Importer: partial=0 #counter errors=0 #counter - for i in range (len(lines)): #main loop, iterates through the lines of a file and calls the appropriate parser method - if (len(lines[i])<2): + for i in range (len(self.lines)): #main loop, iterates through the lines of a file and calls the appropriate parser method + if (len(self.lines[i])<2): endpos=i - hand=lines[startpos:endpos] + hand=self.lines[startpos:endpos] if (len(hand[0])<2): hand=hand[1:] @@ -120,7 +124,7 @@ class Importer: if (len(hand)<3): pass - #todo: the above 2 lines are kind of a dirty hack, the mentioned circumstances should be handled elsewhere but that doesnt work with DOS/Win EOL. actually this doesnt work. + #todo: the above 2 self.lines are kind of a dirty hack, the mentioned circumstances should be handled elsewhere but that doesnt work with DOS/Win EOL. actually this doesnt work. elif (hand[0].endswith(" (partial)")): #partial hand - do nothing partial+=1 elif (hand[1].find("Seat")==-1 and hand[2].find("Seat")==-1 and hand[3].find("Seat")==-1):#todo: should this be or instead of and? @@ -156,7 +160,6 @@ class Importer: if (options.failOnError): self.db.commit() #dont remove this, in case hand processing was cancelled this ties up any open ends. - inputFile.close() self.cursor.close() self.db.close() raise @@ -171,7 +174,6 @@ class Importer: if (options.failOnError): self.db.commit() #dont remove this, in case hand processing was cancelled this ties up any open ends. - inputFile.close() self.cursor.close() self.db.close() raise @@ -183,23 +185,22 @@ class Importer: 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 + print "Total stored:", stored, "duplicates:", duplicates, "partial/damaged:", partial, "errors:", errors, " time:", (time() - starttime) sys.exit(0) startpos=endpos - print "Total stored:", stored, "duplicates:", duplicates, "partial:", partial, "errors:", errors + print "Total stored:", stored, "duplicates:", duplicates, "partial:", partial, "errors:", errors, " time:", (time() - starttime) if stored==0: if duplicates>0: - for line_no in range(len(lines)): - if lines[line_no].find("Game #")!=-1: - final_game_line=lines[line_no] + for line_no in range(len(self.lines)): + if self.lines[line_no].find("Game #")!=-1: + final_game_line=self.lines[line_no] handsId=fpdb_simple.parseSiteHandNo(final_game_line) else: print "failed to read a single hand from file:", inputFile handsId=0 #todo: this will cause return of an unstored hand number if the last hand was error or partial self.db.commit() - inputFile.close() self.cursor.close() self.db.close() return handsId From 0743114c756700849ba8dc03d4fccc0eb0d4e2ec Mon Sep 17 00:00:00 2001 From: Worros Date: Fri, 10 Oct 2008 02:36:12 +0800 Subject: [PATCH 7/8] Factor out multiple instances of "email steffen" error message into a function --- pyfpdb/fpdb_import.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/pyfpdb/fpdb_import.py b/pyfpdb/fpdb_import.py index bb2b5ec9..295836d0 100755 --- a/pyfpdb/fpdb_import.py +++ b/pyfpdb/fpdb_import.py @@ -153,11 +153,8 @@ class Importer: duplicates+=1 except (ValueError), fe: errors+=1 - print "Error No.",errors,", please send the hand causing this to steffen@sycamoretest.info so I can fix it." - print "Filename:",options.inputFile - print "Here is the first line so you can identify it. Please mention that the error was a ValueError:" - print hand[0] - + self.printEmailErrorMessage(errors, options.inputFile, hand[0] + if (options.failOnError): self.db.commit() #dont remove this, in case hand processing was cancelled this ties up any open ends. self.cursor.close() @@ -165,10 +162,8 @@ class Importer: raise except (fpdb_simple.FpdbError), fe: errors+=1 - print "Error No.",errors,", please send the hand causing this to steffen@sycamoretest.info so I can fix it." - print "Filename:",options.inputFile - print "Here is the first line so you can identify it." - print hand[0] + self.printEmailErrorMessage(errors, options.inputFile, hand[0] + #fe.printStackTrace() #todo: get stacktrace self.db.rollback() @@ -206,6 +201,12 @@ class Importer: 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:",options.inputFile + print "Here is the first line so you can identify it. Please mention that the error was a ValueError:" + print hand[0] + if __name__ == "__main__": print "CLI for fpdb_import is currently on vacation please check in later" From 54f0eee98485b25ccec9a3016b0ae194efdf28c5 Mon Sep 17 00:00:00 2001 From: Worros Date: Fri, 10 Oct 2008 02:53:57 +0800 Subject: [PATCH 8/8] Syntax - Fix last commit --- pyfpdb/fpdb_import.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyfpdb/fpdb_import.py b/pyfpdb/fpdb_import.py index 295836d0..b510f9dc 100755 --- a/pyfpdb/fpdb_import.py +++ b/pyfpdb/fpdb_import.py @@ -124,7 +124,7 @@ class Importer: if (len(hand)<3): pass - #todo: the above 2 self.lines are kind of a dirty hack, the mentioned circumstances should be handled elsewhere but that doesnt work with DOS/Win EOL. actually this doesnt work. + #todo: the above 2 lines are kind of a dirty hack, the mentioned circumstances should be handled elsewhere but that doesnt work with DOS/Win EOL. actually this doesnt work. elif (hand[0].endswith(" (partial)")): #partial hand - do nothing partial+=1 elif (hand[1].find("Seat")==-1 and hand[2].find("Seat")==-1 and hand[3].find("Seat")==-1):#todo: should this be or instead of and? @@ -153,7 +153,7 @@ class Importer: duplicates+=1 except (ValueError), fe: errors+=1 - self.printEmailErrorMessage(errors, options.inputFile, hand[0] + self.printEmailErrorMessage(errors, options.inputFile, hand[0]) if (options.failOnError): self.db.commit() #dont remove this, in case hand processing was cancelled this ties up any open ends. @@ -162,7 +162,7 @@ class Importer: raise except (fpdb_simple.FpdbError), fe: errors+=1 - self.printEmailErrorMessage(errors, options.inputFile, hand[0] + self.printEmailErrorMessage(errors, options.inputFile, hand[0]) #fe.printStackTrace() #todo: get stacktrace self.db.rollback()