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() (options, sys.argv) = parser.parse_args()
settings={'imp-callFpdbHud':False, 'db-backend':2} settings={'imp-callFpdbHud':False, 'db-backend':2}
importer = fpdb_import.Importer() importer = fpdb_import.Importer(options,settings)
importer.import_file_dict(options, settings) importer.import_file_dict()

View File

@ -57,7 +57,7 @@ class GuiAutoImport (threading.Thread):
self.inputFile = os.path.join(self.path, file) self.inputFile = os.path.join(self.path, file)
stat_info = os.stat(self.inputFile) 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]: 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 self.import_files[self.inputFile] = stat_info.st_mtime
print "GuiAutoImport.import_dir done" print "GuiAutoImport.import_dir done"
@ -121,7 +121,7 @@ class GuiAutoImport (threading.Thread):
def __init__(self, settings, debug=True): def __init__(self, settings, debug=True):
"""Constructor for GuiAutoImport""" """Constructor for GuiAutoImport"""
self.settings=settings self.settings=settings
self.importer = fpdb_import.Importer() self.importer = fpdb_import.Importer(self,self.settings)
self.importer.setCallHud(True) self.importer.setCallHud(True)
self.server=settings['db-host'] 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" print "BulkImport is not recursive - please select the final directory in which the history files are"
else: else:
self.inputFile=self.path+os.sep+file 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" print "GuiBulkImport.import_dir done"
def load_clicked(self, widget, data=None): def load_clicked(self, widget, data=None):
@ -64,12 +64,10 @@ class GuiBulkImport (threading.Thread):
else: else:
self.failOnError=True self.failOnError=True
self.server, self.database, self.user, self.password=self.db.get_db_info()
if os.path.isdir(self.inputFile): if os.path.isdir(self.inputFile):
self.import_dir() self.import_dir()
else: else:
self.importer.import_file_dict(self, self.settings) self.importer.import_file_dict()
def get_vbox(self): def get_vbox(self):
"""returns the vbox of this thread""" """returns the vbox of this thread"""
@ -83,7 +81,7 @@ class GuiBulkImport (threading.Thread):
def __init__(self, db, settings): def __init__(self, db, settings):
self.db=db self.db=db
self.settings=settings self.settings=settings
self.importer = fpdb_import.Importer() self.importer = fpdb_import.Importer(self,self.settings)
self.vbox=gtk.VBox(False,1) self.vbox=gtk.VBox(False,1)
self.vbox.show() self.vbox.show()

View File

@ -255,9 +255,9 @@ class GuiTableViewer (threading.Thread):
self.failOnError=False self.failOnError=False
self.minPrint=0 self.minPrint=0
self.handCount=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 #end def table_viewer.import_clicked
def all_clicked(self, widget, data): def all_clicked(self, widget, data):

View File

@ -40,29 +40,32 @@ from time import time
class Importer: class Importer:
def __init__(self): def __init__(self, options, settings):
"""Constructor""" """Constructor"""
self.settings={'imp-callFpdbHud':False} self.settings=settings
self.options=options
self.db = None self.db = None
self.cursor = None self.cursor = None
self.options = None
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
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 #connect to DB
if settings['db-backend'] == 2: if self.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(host = options.server, user = options.user, self.db = MySQLdb.connect(self.settings['db-host'], self.settings['db-user'],
passwd = options.password, db = options.database) self.settings['db-password'], self.settings['db-databaseName'])
elif settings['db-backend'] == 3: elif self.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(host = options.server, user = options.user, self.db = psycopg2.connect(self.settings['db-host'], self.settings['db-user'],
password = options.password, database = options.database) self.settings['db-password'], self.settings['db-databaseName'])
elif settings['db-backend'] == 4: elif self.settings['db-backend'] == 4:
pass pass
else: else:
pass pass
@ -71,32 +74,30 @@ class Importer:
def setCallHud(self, value): def setCallHud(self, value):
self.callHud = value self.callHud = value
def import_file_dict(self, options, settings): def addImportFile(self, filename):
self.options=options self.options.inputFile = filename
def import_file_dict(self):
starttime = time() starttime = time()
last_read_hand=0 last_read_hand=0
loc = 0 loc = 0
if (options.inputFile=="stdin"): if (self.options.inputFile=="stdin"):
inputFile=sys.stdin inputFile=sys.stdin
else: else:
inputFile=open(options.inputFile, "rU") inputFile=open(self.options.inputFile, "rU")
try: loc = self.pos_in_file[options.inputFile] try: loc = self.pos_in_file[self.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[options.inputFile] = inputFile.tell() self.pos_in_file[self.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)
@ -151,41 +152,36 @@ 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 settings['imp-callFpdbHud'] and self.callHud: if self.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
# options.pipe_to_hud.write("%s" % (handsId) + os.linesep) self.options.pipe_to_hud.stdin.write("%s" % (handsId) + os.linesep)
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, 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.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, options.inputFile, hand[0]) self.printEmailErrorMessage(errors, self.options.inputFile, hand[0])
#fe.printStackTrace() #todo: get stacktrace #fe.printStackTrace() #todo: get stacktrace
self.db.rollback() 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.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 (options.minPrint!=0): if (self.options.minPrint!=0):
if ((stored+duplicates+partial+errors)%options.minPrint==0): if ((stored+duplicates+partial+errors)%sielf.options.minPrint==0):
print "stored:", stored, "duplicates:", duplicates, "partial:", partial, "errors:", errors print "stored:", stored, "duplicates:", duplicates, "partial:", partial, "errors:", errors
if (options.handCount!=0): if (self.options.handCount!=0):
if ((stored+duplicates+partial+errors)>=options.handCount): if ((stored+duplicates+partial+errors)>=self.options.handCount):
if (not options.quiet): if (not self.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)
@ -203,8 +199,6 @@ 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