Change api so object and settings are passed in at creation time.

This commit is contained in:
Worros 2008-10-12 01:12:30 +08:00
parent ac551f6535
commit 27ca82ca48
5 changed files with 42 additions and 50 deletions

View File

@ -54,5 +54,5 @@ if __name__ == "__main__":
(options, sys.argv) = parser.parse_args()
settings={'imp-callFpdbHud':False, 'db-backend':2}
importer = fpdb_import.Importer()
importer.import_file_dict(options, settings)
importer = fpdb_import.Importer(options,settings)
importer.import_file_dict()

View File

@ -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)
self.importer.import_file_dict()
self.import_files[self.inputFile] = stat_info.st_mtime
print "GuiAutoImport.import_dir done"
@ -121,7 +121,7 @@ class GuiAutoImport (threading.Thread):
def __init__(self, settings, debug=True):
"""Constructor for GuiAutoImport"""
self.settings=settings
self.importer = fpdb_import.Importer()
self.importer = fpdb_import.Importer(self,self.settings)
self.importer.setCallHud(True)
self.server=settings['db-host']

View File

@ -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)
self.importer.import_file_dict()
print "GuiBulkImport.import_dir done"
def load_clicked(self, widget, data=None):
@ -64,12 +64,10 @@ class GuiBulkImport (threading.Thread):
else:
self.failOnError=True
self.server, self.database, self.user, self.password=self.db.get_db_info()
if os.path.isdir(self.inputFile):
self.import_dir()
else:
self.importer.import_file_dict(self, self.settings)
self.importer.import_file_dict()
def get_vbox(self):
"""returns the vbox of this thread"""
@ -83,7 +81,7 @@ class GuiBulkImport (threading.Thread):
def __init__(self, db, settings):
self.db=db
self.settings=settings
self.importer = fpdb_import.Importer()
self.importer = fpdb_import.Importer(self,self.settings)
self.vbox=gtk.VBox(False,1)
self.vbox.show()

View File

@ -255,9 +255,9 @@ class GuiTableViewer (threading.Thread):
self.failOnError=False
self.minPrint=0
self.handCount=0
self.importer = fpdb_import.Importer()
self.importer = fpdb_import.Importer(self, self.settings)
self.last_read_hand_id=self.importer.import_file_dict(self, self.settings)
self.last_read_hand_id=self.importer.import_file_dict()
#end def table_viewer.import_clicked
def all_clicked(self, widget, data):

View File

@ -40,29 +40,32 @@ from time import time
class Importer:
def __init__(self):
def __init__(self, options, settings):
"""Constructor"""
self.settings={'imp-callFpdbHud':False}
self.settings=settings
self.options=options
self.db = None
self.cursor = None
self.options = None
self.callHud = False
self.lines = None
self.pos_in_file = {} # dict to remember how far we have read in the file
if not self.settings.has_key('imp-callFpdbHud'):
self.settings['imp-callFpdbHud'] = False
self.dbConnect()
def dbConnect(self, options, settings):
def dbConnect(self):
#connect to DB
if settings['db-backend'] == 2:
if self.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:
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:
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:
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:
pass
else:
pass
@ -71,32 +74,30 @@ class Importer:
def setCallHud(self, value):
self.callHud = value
def import_file_dict(self, options, settings):
self.options=options
def addImportFile(self, filename):
self.options.inputFile = filename
def import_file_dict(self):
starttime = time()
last_read_hand=0
loc = 0
if (options.inputFile=="stdin"):
if (self.options.inputFile=="stdin"):
inputFile=sys.stdin
else:
inputFile=open(options.inputFile, "rU")
try: loc = self.pos_in_file[options.inputFile]
inputFile=open(self.options.inputFile, "rU")
try: loc = self.pos_in_file[self.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[options.inputFile] = inputFile.tell()
self.pos_in_file[self.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)
@ -151,41 +152,36 @@ class Importer:
stored+=1
self.db.commit()
# if settings['imp-callFpdbHud'] and self.callHud and os.sep=='/':
if settings['imp-callFpdbHud'] and self.callHud:
if self.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)
options.pipe_to_hud.stdin.write("%s" % (handsId) + os.linesep)
self.options.pipe_to_hud.stdin.write("%s" % (handsId) + os.linesep)
except fpdb_simple.DuplicateError:
duplicates+=1
except (ValueError), fe:
errors+=1
self.printEmailErrorMessage(errors, options.inputFile, hand[0])
self.printEmailErrorMessage(errors, self.options.inputFile, hand[0])
if (options.failOnError):
if (self.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, options.inputFile, hand[0])
self.printEmailErrorMessage(errors, self.options.inputFile, hand[0])
#fe.printStackTrace() #todo: get stacktrace
self.db.rollback()
if (options.failOnError):
if (self.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 (options.minPrint!=0):
if ((stored+duplicates+partial+errors)%options.minPrint==0):
if (self.options.minPrint!=0):
if ((stored+duplicates+partial+errors)%sielf.options.minPrint==0):
print "stored:", stored, "duplicates:", duplicates, "partial:", partial, "errors:", errors
if (options.handCount!=0):
if ((stored+duplicates+partial+errors)>=options.handCount):
if (not options.quiet):
if (self.options.handCount!=0):
if ((stored+duplicates+partial+errors)>=self.options.handCount):
if (not self.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)
@ -203,8 +199,6 @@ 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