No longer mess with sys.argv (messing with system variables is a bad thing, right), use argv to determine pathname of executeable as sys.path[0] is just the first component of the path. also all database errors except MySQL reporting "Access Denied" should now crash FPDB, so someone can fill those into Exceptions, and into the fpdb_db and the fpdb files.

Process: get crash info, add exception info to Exceptions.py, catch generic database exception in fpdb_db.py (around the connect line), throw correct Fpdb exception, then catch it in fpdb.py and do the appropriate thing on the GUI end.
This commit is contained in:
Eric Blade 2009-11-22 00:00:23 -05:00
parent 14ccde73a2
commit 64d9a3582b
6 changed files with 146 additions and 128 deletions

View File

@ -6,17 +6,17 @@ Handles HUD configuration files.
"""
# Copyright 2008, 2009, Ray E. Barker
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
@ -54,16 +54,20 @@ def get_exec_path():
"""Returns the path to the fpdb.(py|exe) file we are executing"""
if hasattr(sys, "frozen"): # compiled by py2exe
return os.path.dirname(sys.executable)
else:
return os.path.dirname(sys.path[0])
else:
print "argv=", sys.argv
pathname = os.path.dirname(sys.argv[0])
return os.path.abspath(pathname)
def get_config(file_name, fallback = True):
"""Looks in cwd and in self.default_config_path for a config file."""
config_path = os.path.join(get_exec_path(), file_name)
print "config_path=", config_path
if os.path.exists(config_path): # there is a file in the cwd
return config_path # so we use it
else: # no file in the cwd, look where it should be in the first place
config_path = os.path.join(get_default_config_path(), file_name)
print "config path 2=", config_path
if os.path.exists(config_path):
return config_path
@ -142,7 +146,7 @@ class Layout:
if node.hasAttribute('fav_seat'): self.fav_seat = int( node.getAttribute('fav_seat') )
self.width = int( node.getAttribute('width') )
self.height = int( node.getAttribute('height') )
self.location = []
self.location = map(lambda x: None, range(self.max+1)) # fill array with max seats+1 empty entries
@ -161,7 +165,7 @@ class Layout:
temp = temp + " Locations = "
for i in range(1, len(self.location)):
temp = temp + "(%d,%d)" % self.location[i]
return temp + "\n"
class Site:
@ -171,7 +175,7 @@ class Site:
if os.path.exists(path):
return os.path.abspath(path)
return path
self.site_name = node.getAttribute("site_name")
self.table_finder = node.getAttribute("table_finder")
self.screen_name = node.getAttribute("screen_name")
@ -191,7 +195,7 @@ class Site:
self.ypad = node.getAttribute("ypad")
self.layout = {}
print "Loading site", self.site_name
print "Loading site", self.site_name
for layout_node in node.getElementsByTagName('layout'):
lo = Layout(layout_node)
@ -204,7 +208,7 @@ class Site:
self.hudopacity = 1.0 if self.hudopacity == "" else float(self.hudopacity)
if self.use_frames == "": self.use_frames = False
if self.font == "": self.font = "Sans"
if self.font == "": self.font = "Sans"
if self.hudbgcolor == "": self.hudbgcolor = "#000000"
if self.hudfgcolor == "": self.hudfgcolor = "#FFFFFF"
@ -216,20 +220,20 @@ class Site:
value = getattr(self, key)
if callable(value): continue
temp = temp + ' ' + key + " = " + str(value) + "\n"
for layout in self.layout:
temp = temp + "%s" % self.layout[layout]
return temp
class Stat:
def __init__(self):
pass
def __str__(self):
temp = " stat_name = %s, row = %d, col = %d, tip = %s, click = %s, popup = %s\n" % (self.stat_name, self.row, self.col, self.tip, self.click, self.popup)
return temp
class Game:
def __init__(self, node):
self.game_name = node.getAttribute("game_name")
@ -262,9 +266,9 @@ class Game:
stat.hudprefix = stat_node.getAttribute("hudprefix")
stat.hudsuffix = stat_node.getAttribute("hudsuffix")
stat.hudcolor = stat_node.getAttribute("hudcolor")
self.stats[stat.stat_name] = stat
def __str__(self):
temp = "Game = " + self.game_name + "\n"
temp = temp + " rows = %d\n" % self.rows
@ -272,12 +276,12 @@ class Game:
temp = temp + " xpad = %d\n" % self.xpad
temp = temp + " ypad = %d\n" % self.ypad
temp = temp + " aux = %s\n" % self.aux
for stat in self.stats.keys():
temp = temp + "%s" % self.stats[stat]
return temp
class Database:
def __init__(self, node):
self.db_name = node.getAttribute("db_name")
@ -288,7 +292,7 @@ class Database:
self.db_selected = string_to_bool(node.getAttribute("default"), default=False)
log.debug("Database db_name:'%(name)s' db_server:'%(server)s' db_ip:'%(ip)s' db_user:'%(user)s' db_pass (not logged) selected:'%(sel)s'" \
% { 'name':self.db_name, 'server':self.db_server, 'ip':self.db_ip, 'user':self.db_user, 'sel':self.db_selected} )
def __str__(self):
temp = 'Database = ' + self.db_name + '\n'
for key in dir(self):
@ -336,7 +340,7 @@ class Popup:
self.pu_stats = []
for stat_node in node.getElementsByTagName('pu_stat'):
self.pu_stats.append(stat_node.getAttribute("pu_stat_name"))
def __str__(self):
temp = "Popup = " + self.name + "\n"
for stat in self.pu_stats:
@ -385,7 +389,7 @@ class Tv:
self.combinedPostflop = string_to_bool(node.getAttribute("combinedPostflop"), default=True)
def __str__(self):
return (" combinedStealFold = %s\n combined2B3B = %s\n combinedPostflop = %s\n" %
return (" combinedStealFold = %s\n combined2B3B = %s\n combinedPostflop = %s\n" %
(self.combinedStealFold, self.combined2B3B, self.combinedPostflop) )
class Config:
@ -410,7 +414,7 @@ class Config:
print "\nReading configuration file %s\n" % file
try:
doc = xml.dom.minidom.parse(file)
except:
except:
log.error("Error parsing %s. See error log file." % (file))
traceback.print_exc(file=sys.stderr)
print "press enter to continue"
@ -438,9 +442,9 @@ class Config:
for game_node in doc.getElementsByTagName("game"):
game = Game(node = game_node)
self.supported_games[game.game_name] = game
# parse databases defined by user in the <supported_databases> section
# the user may select the actual database to use via commandline or by setting the selected="bool"
# the user may select the actual database to use via commandline or by setting the selected="bool"
# attribute of the tag. if no database is explicitely selected, we use the first one we come across
# s_dbs = doc.getElementsByTagName("supported_databases")
#TODO: do we want to take all <database> tags or all <database> tags contained in <supported_databases>
@ -452,7 +456,7 @@ class Config:
if self.db_selected is None or db.db_selected:
self.db_selected = db.db_name
self.supported_databases[db.db_name] = db
#TODO: if the user may passes '' (empty string) as database name via command line, his choice is ignored
#TODO: if the user may passes '' (empty string) as database name via command line, his choice is ignored
# ..when we parse the xml we allow for ''. there has to be a decission if to allow '' or not
if dbname and dbname in self.supported_databases:
self.db_selected = dbname
@ -502,7 +506,7 @@ class Config:
def set_hhArchiveBase(self, path):
self.imp.node.setAttribute("hhArchiveBase", path)
def find_default_conf(self):
if os.name == 'posix':
config_path = os.path.join(os.path.expanduser("~"), '.fpdb', 'default.conf')
@ -534,7 +538,7 @@ class Config:
def get_layout_node(self, site_node, layout):
for layout_node in site_node.getElementsByTagName("layout"):
if layout_node.getAttribute("max") is None:
if layout_node.getAttribute("max") is None:
return None
if int( layout_node.getAttribute("max") ) == int( layout ):
return layout_node
@ -618,7 +622,7 @@ class Config:
elif self.supported_databases[name].db_server== DATABASE_TYPE_POSTGRESQL:
db['db-backend'] = 3
elif self.supported_databases[name].db_server== DATABASE_TYPE_SQLITE:
db['db-backend'] = 4
db['db-backend'] = 4
else:
raise ValueError('Unsupported database backend: %s' % self.supported_databases[name].db_server)
return db
@ -639,7 +643,7 @@ class Config:
if db_server is not None: self.supported_databases[db_name].dp_server = db_server
if db_type is not None: self.supported_databases[db_name].dp_type = db_type
return
def getDefaultSite(self):
"Returns first enabled site or None"
for site_name,site in self.supported_sites.iteritems():
@ -702,7 +706,7 @@ class Config:
return hui
def get_import_parameters(self):
imp = {}
try: imp['callFpdbHud'] = self.imp.callFpdbHud
@ -728,10 +732,10 @@ class Config:
path = os.path.expanduser(self.supported_sites[site].HH_path)
assert(os.path.isdir(path) or os.path.isfile(path)) # maybe it should try another site?
paths['hud-defaultPath'] = paths['bulkImport-defaultPath'] = path
except AssertionError:
except AssertionError:
paths['hud-defaultPath'] = paths['bulkImport-defaultPath'] = "** ERROR DEFAULT PATH IN CONFIG DOES NOT EXIST **"
return paths
def get_frames(self, site = "PokerStars"):
if site not in self.supported_sites: return False
return self.supported_sites[site].use_frames == True
@ -751,7 +755,7 @@ class Config:
else:
colors['hudfgcolor'] = self.supported_sites[site].hudfgcolor
return colors
def get_default_font(self, site='PokerStars'):
font = "Sans"
font_size = "8"
@ -770,17 +774,17 @@ class Config:
if location is not None:
return location.location
return (
( 0, 0), (684, 61), (689, 239), (692, 346),
( 0, 0), (684, 61), (689, 239), (692, 346),
(586, 393), (421, 440), (267, 440), ( 0, 361),
( 0, 280), (121, 280), ( 46, 30)
( 0, 280), (121, 280), ( 46, 30)
)
def get_aux_locations(self, aux = "mucked", max = "9"):
try:
locations = self.aux_windows[aux].layout[max].location
except:
locations = ( ( 0, 0), (684, 61), (689, 239), (692, 346),
locations = ( ( 0, 0), (684, 61), (689, 239), (692, 346),
(586, 393), (421, 440), (267, 440), ( 0, 361),
( 0, 280), (121, 280), ( 46, 30) )
return locations
@ -791,7 +795,7 @@ class Config:
return self.supported_sites.keys()
else:
return [site_name for (site_name, site) in self.supported_sites.items() if site.enabled]
def get_site_parameters(self, site):
"""Returns a dict of the site parameters for the specified site"""
parms = {}
@ -814,7 +818,7 @@ class Config:
return parms
def set_site_parameters(self, site_name, converter = None, decoder = None,
hudbgcolor = None, hudfgcolor = None,
hudbgcolor = None, hudfgcolor = None,
hudopacity = None, screen_name = None,
site_path = None, table_finder = None,
HH_path = None, enabled = None,
@ -852,7 +856,7 @@ class Config:
return param
return None
def get_game_parameters(self, name):
"""Get the configuration parameters for the named game."""
param = {}
@ -878,7 +882,7 @@ class Config:
if __name__== "__main__":
c = Config()
print "\n----------- SUPPORTED SITES -----------"
for s in c.supported_sites.keys():
print c.supported_sites[s]
@ -905,7 +909,7 @@ if __name__== "__main__":
for w in c.hhcs.keys():
print c.hhcs[w]
print "----------- END HAND HISTORY CONVERTERS -----------"
print "\n----------- POPUP WINDOW FORMATS -----------"
for w in c.popup_windows.keys():
print c.popup_windows[w]
@ -921,7 +925,7 @@ if __name__== "__main__":
c.edit_layout("PokerStars", 6, locations=( (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6) ))
c.save(file="testout.xml")
print "db = ", c.get_db_parameters()
# print "tv = ", c.get_tv_parameters()
# print "imp = ", c.get_import_parameters()
@ -932,7 +936,7 @@ if __name__== "__main__":
print c.get_aux_parameters(mw)
print "mucked locations =", c.get_aux_locations('mucked', 9)
# c.edit_aux_layout('mucked', 9, locations = [(487, 113), (555, 469), (572, 276), (522, 345),
# c.edit_aux_layout('mucked', 9, locations = [(487, 113), (555, 469), (572, 276), (522, 345),
# (333, 354), (217, 341), (150, 273), (150, 169), (230, 115)])
# print "mucked locations =", c.get_aux_locations('mucked', 9)

View File

@ -4,7 +4,7 @@ class FpdbError(Exception):
def __str__(self):
return repr(self.value)
class FpdbParseError(FpdbError):
class FpdbParseError(FpdbError):
def __init__(self,value='',hid=''):
self.value = value
self.hid = hid
@ -17,8 +17,15 @@ class FpdbParseError(FpdbError):
class FpdbDatabaseError(FpdbError):
pass
class FpdbMySQLFailedError(FpdbDatabaseError):
class FpdbMySQLError(FpdbDatabaseError):
pass
class FpdbMySQLAccessDenied(FpdbDatabaseError):
def __init__(self, value='', errmsg=''):
self.value = value
self.errmsg = errmsg
def __str__(self):
return repr(self.value +" " + self.errmsg)
class DuplicateError(FpdbError):
pass

View File

@ -5,17 +5,17 @@
Main for FreePokerTools HUD.
"""
# Copyright 2008, 2009, Ray E. Barker
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
@ -33,7 +33,7 @@ import os
import Options
import traceback
(options, sys.argv) = Options.fpdb_options()
(options, argv) = Options.fpdb_options()
if not options.errorsToConsole:
print "Note: error output is being diverted to fpdb-error-log.txt and HUD-error.txt. Any major error will be reported there _only_."
@ -101,20 +101,20 @@ class HUD_main(object):
def create_HUD(self, new_hand_id, table, table_name, max, poker_game, type, stat_dict, cards):
"""type is "ring" or "tour" used to set hud_params"""
def idle_func():
gtk.gdk.threads_enter()
try: # TODO: seriously need to decrease the scope of this block.. what are we expecting to error?
# TODO: The purpose of this try/finally block is to make darn sure that threads_leave()
# TODO: gets called. If there is an exception and threads_leave() doesn't get called we
# TODO: gets called. If there is an exception and threads_leave() doesn't get called we
# TODO: lock up. REB
table.gdkhandle = gtk.gdk.window_foreign_new(table.number)
newlabel = gtk.Label("%s - %s" % (table.site, table_name))
self.vb.add(newlabel)
newlabel.show()
self.main_window.resize_children()
self.hud_dict[table_name].tablehudlabel = newlabel
self.hud_dict[table_name].create(new_hand_id, self.config, stat_dict, cards)
for m in self.hud_dict[table_name].aux_windows:
@ -151,23 +151,23 @@ class HUD_main(object):
[aw.update_data(new_hand_id, self.db_connection) for aw in self.hud_dict[table_name].aux_windows]
gobject.idle_add(idle_func)
def update_HUD(self, new_hand_id, table_name, config):
"""Update a HUD gui from inside the non-gui read_stdin thread."""
# This is written so that only 1 thread can touch the gui--mainly
# for compatibility with Windows. This method dispatches the
# for compatibility with Windows. This method dispatches the
# function idle_func() to be run by the gui thread, at its leisure.
def idle_func():
gtk.gdk.threads_enter()
# try:
# try:
self.hud_dict[table_name].update(new_hand_id, config)
[aw.update_gui(new_hand_id) for aw in self.hud_dict[table_name].aux_windows]
# finally:
gtk.gdk.threads_leave()
return False
gobject.idle_add(idle_func)
def read_stdin(self): # This is the thread function
"""Do all the non-gui heavy lifting for the HUD program."""
@ -176,7 +176,7 @@ class HUD_main(object):
# need their own access to the database, but should open their own
# if it is required.
self.db_connection = Database.Database(self.config)
# get hero's screen names and player ids
self.hero, self.hero_ids = {}, {}
for site in self.config.get_supported_sites():
@ -185,7 +185,7 @@ class HUD_main(object):
site_id = result[0][0]
self.hero[site_id] = self.config.supported_sites[site].screen_name
self.hero_ids[site_id] = self.db_connection.get_player_id(self.config, site, self.hero[site_id])
while 1: # wait for a new hand number on stdin
new_hand_id = sys.stdin.readline()
new_hand_id = string.rstrip(new_hand_id)
@ -199,7 +199,7 @@ class HUD_main(object):
(table_name, max, poker_game, type, site_id, site_name, tour_number, tab_number) = \
self.db_connection.get_table_info(new_hand_id)
except Exception, err:
print "db error: skipping %s" % new_hand_id
print "db error: skipping %s" % new_hand_id
sys.stderr.write("Database error: could not find hand %s.\n" % new_hand_id)
continue
@ -210,7 +210,7 @@ class HUD_main(object):
# Update an existing HUD
if temp_key in self.hud_dict:
# get stats using hud's specific params and get cards
# get stats using hud's specific params and get cards
self.db_connection.init_hud_stat_vars( self.hud_dict[temp_key].hud_params['hud_days']
, self.hud_dict[temp_key].hud_params['h_hud_days'])
stat_dict = self.db_connection.get_stats_from_hand(new_hand_id, type, self.hud_dict[temp_key].hud_params, self.hero_ids[site_id])
@ -222,7 +222,7 @@ class HUD_main(object):
self.hud_dict[temp_key].cards = cards
[aw.update_data(new_hand_id, self.db_connection) for aw in self.hud_dict[temp_key].aux_windows]
self.update_HUD(new_hand_id, temp_key, self.config)
# Or create a new HUD
else:
# get stats using default params--also get cards

View File

@ -23,20 +23,20 @@ def fpdb_options():
"""Process command line options for fpdb and HUD_main."""
parser = OptionParser()
parser.add_option("-x", "--errorsToConsole",
action="store_true",
parser.add_option("-x", "--errorsToConsole",
action="store_true",
help="If passed error output will go to the console rather than .")
parser.add_option("-d", "--databaseName",
parser.add_option("-d", "--databaseName",
dest="dbname", default="fpdb",
help="Overrides the default database name")
parser.add_option("-c", "--configFile",
parser.add_option("-c", "--configFile",
dest="config", default=None,
help="Specifies a configuration file.")
parser.add_option("-r", "--rerunPython",
action="store_true",
parser.add_option("-r", "--rerunPython",
action="store_true",
help="Indicates program was restarted with a different path (only allowed once).")
(options, sys.argv) = parser.parse_args()
return (options, sys.argv)
(options, argv) = parser.parse_args()
return (options, argv)
if __name__== "__main__":
(options, sys.argv) = fpdb_options()
@ -45,4 +45,4 @@ if __name__== "__main__":
print "config file =", options.config
print "press enter to end"
sys.stdin.readline()
sys.stdin.readline()

View File

@ -41,7 +41,7 @@ if os.name == 'nt' and sys.version[0:3] not in ('2.5', '2.6') and '-r' not in sy
else:
pass
#print "debug - not changing path"
if os.name == 'nt':
import win32api
import win32con
@ -53,7 +53,7 @@ import threading
import Options
import string
cl_options = string.join(sys.argv[1:])
(options, sys.argv) = Options.fpdb_options()
(options, argv) = Options.fpdb_options()
if not options.errorsToConsole:
print "Note: error output is being diverted to fpdb-error-log.txt and HUD-error.txt. Any major error will be reported there _only_."
@ -80,7 +80,7 @@ import SQL
import Database
import FpdbSQLQueries
import Configuration
from Exceptions import *
import Exceptions
VERSION = "0.12"
@ -178,23 +178,23 @@ class fpdb:
"""obtains db root credentials from user"""
self.warning_box("Unimplemented: Get Root Database Credentials")
# user, pw=None, None
#
#
# dialog=gtk.Dialog(title="DB Credentials needed", parent=None, flags=0,
# buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,"Connect and recreate",gtk.RESPONSE_OK))
#
#
# label_warning1=gtk.Label("Please enter credentials for a database user for "+self.host+" that has permissions to create a database.")
#
#
#
#
# label_user=gtk.Label("Username")
# dialog.vbox.add(label_user)
# label_user.show()
#
#
# response=dialog.run()
# dialog.destroy()
# return (user, pw, response)
def dia_import_db(self, widget, data=None):
self.warning_box("Unimplemented: Import Database")
self.warning_box("Unimplemented: Import Database")
self.obtain_global_lock()
self.release_global_lock()
@ -211,7 +211,7 @@ class fpdb:
# chooser.set_filename(self.profile)
# response = chooser.run()
# chooser.destroy()
# chooser.destroy()
# if response == gtk.RESPONSE_OK:
# self.load_profile(chooser.get_filename())
# elif response == gtk.RESPONSE_CANCEL:
@ -239,7 +239,7 @@ class fpdb:
dia_confirm.destroy()
if response == gtk.RESPONSE_YES:
#if self.db.backend == self.fdb_lock.fdb.MYSQL_INNODB:
# mysql requires locks on all tables or none - easier to release this lock
# mysql requires locks on all tables or none - easier to release this lock
# than lock all the other tables
# ToDo: lock all other tables so that lock doesn't have to be released
# self.release_global_lock()
@ -252,7 +252,7 @@ class fpdb:
print 'User cancelled recreating tables'
#if not lock_released:
self.release_global_lock()
def dia_recreate_hudcache(self, widget, data=None):
if self.obtain_global_lock():
self.dia_confirm = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_WARNING, buttons=(gtk.BUTTONS_YES_NO), message_format="Confirm recreating HUD cache")
@ -314,7 +314,7 @@ class fpdb:
entry.set_text(ds)
win.destroy()
self.dia_confirm.set_modal(True)
def dia_regression_test(self, widget, data=None):
self.warning_box("Unimplemented: Regression Test")
self.obtain_global_lock()
@ -322,7 +322,7 @@ class fpdb:
def dia_save_profile(self, widget, data=None):
self.warning_box("Unimplemented: Save Profile (try saving a HUD layout, that should do it)")
def diaSetupWizard(self, path):
diaSetupWizard = gtk.Dialog(title="Fatal Error - Config File Missing", parent=None, flags=0, buttons=(gtk.STOCK_QUIT,gtk.RESPONSE_OK))
@ -453,21 +453,25 @@ class fpdb:
self.sql = SQL.Sql(db_server = self.settings['db-server'])
try:
self.db = Database.Database(self.config, sql = self.sql)
except FpdbMySQLFailedError:
self.warning_box("Unable to connect to MySQL! Is the MySQL server running?!", "FPDB ERROR")
except Exceptions.FpdbMySQLAccessDenied:
self.warning_box("MySQL Server reports: Access denied. Are your permissions set correctly?")
exit()
except FpdbError:
#print "Failed to connect to %s database with username %s." % (self.settings['db-server'], self.settings['db-user'])
self.warning_box("Failed to connect to %s database with username %s." % (self.settings['db-server'], self.settings['db-user']), "FPDB ERROR")
err = traceback.extract_tb(sys.exc_info()[2])[-1]
print "*** Error: " + err[2] + "(" + str(err[1]) + "): " + str(sys.exc_info()[1])
sys.stderr.write("Failed to connect to %s database with username %s." % (self.settings['db-server'], self.settings['db-user']))
except:
#print "Failed to connect to %s database with username %s." % (self.settings['db-server'], self.settings['db-user'])
self.warning_box("Failed to connect to %s database with username %s." % (self.settings['db-server'], self.settings['db-user']), "FPDB ERROR")
err = traceback.extract_tb(sys.exc_info()[2])[-1]
print "*** Error: " + err[2] + "(" + str(err[1]) + "): " + str(sys.exc_info()[1])
sys.stderr.write("Failed to connect to %s database with username %s." % (self.settings['db-server'], self.settings['db-user']))
# except FpdbMySQLFailedError:
# self.warning_box("Unable to connect to MySQL! Is the MySQL server running?!", "FPDB ERROR")
# exit()
# except FpdbError:
# #print "Failed to connect to %s database with username %s." % (self.settings['db-server'], self.settings['db-user'])
# self.warning_box("Failed to connect to %s database with username %s." % (self.settings['db-server'], self.settings['db-user']), "FPDB ERROR")
# err = traceback.extract_tb(sys.exc_info()[2])[-1]
# print "*** Error: " + err[2] + "(" + str(err[1]) + "): " + str(sys.exc_info()[1])
# sys.stderr.write("Failed to connect to %s database with username %s." % (self.settings['db-server'], self.settings['db-user']))
# except:
# #print "Failed to connect to %s database with username %s." % (self.settings['db-server'], self.settings['db-user'])
# self.warning_box("Failed to connect to %s database with username %s." % (self.settings['db-server'], self.settings['db-user']), "FPDB ERROR")
# err = traceback.extract_tb(sys.exc_info()[2])[-1]
# print "*** Error: " + err[2] + "(" + str(err[1]) + "): " + str(sys.exc_info()[1])
# sys.stderr.write("Failed to connect to %s database with username %s." % (self.settings['db-server'], self.settings['db-user']))
if self.db.wrongDbVersion:
diaDbVersionWarning = gtk.Dialog(title="Strong Warning - Invalid database version", parent=None, flags=0, buttons=(gtk.STOCK_OK,gtk.RESPONSE_OK))
@ -496,7 +500,7 @@ class fpdb:
# Database connected to successfully, load queries to pass on to other classes
self.db.rollback()
self.validate_config()
def not_implemented(self, widget, data=None):
@ -624,7 +628,7 @@ This program is licensed under the AGPL3, see docs"""+os.sep+"agpl-3.0.txt")
self.window.show()
self.load_profile()
self.statusIcon = gtk.StatusIcon()
if os.path.exists('../gfx/fpdb-cards.png'):
self.statusIcon.set_from_file('../gfx/fpdb-cards.png')
@ -643,19 +647,19 @@ This program is licensed under the AGPL3, see docs"""+os.sep+"agpl-3.0.txt")
self.statusMenu.append(menuItem)
self.statusIcon.connect('popup-menu', self.statusicon_menu, self.statusMenu)
self.statusIcon.set_visible(True)
self.window.connect('window-state-event', self.window_state_event_cb)
sys.stderr.write("fpdb starting ...")
def window_state_event_cb(self, window, event):
print "window_state_event", event
if event.changed_mask & gtk.gdk.WINDOW_STATE_ICONIFIED:
# -20 = GWL_EXSTYLE can't find it in the pywin32 libs
#bits = win32api.GetWindowLong(self.window.window.handle, -20)
#bits = bits ^ (win32con.WS_EX_TOOLWINDOW | win32con.WS_EX_APPWINDOW)
#win32api.SetWindowLong(self.window.window.handle, -20, bits)
if event.new_window_state & gtk.gdk.WINDOW_STATE_ICONIFIED:
self.window.hide()
self.window.set_skip_taskbar_hint(True)
@ -665,7 +669,7 @@ This program is licensed under the AGPL3, see docs"""+os.sep+"agpl-3.0.txt")
self.window.set_skip_pager_hint(False)
# Tell GTK not to propagate this signal any further
return True
def statusicon_menu(self, widget, button, time, data = None):
# we don't need to pass data here, since we do keep track of most all
# our variables .. the example code that i looked at for this
@ -676,7 +680,7 @@ This program is licensed under the AGPL3, see docs"""+os.sep+"agpl-3.0.txt")
data.show_all()
data.popup(None, None, None, 3, time)
pass
def statusicon_activate(self, widget, data = None):
# Let's allow the tray icon to toggle window visibility, the way
# most other apps work
@ -686,7 +690,7 @@ This program is licensed under the AGPL3, see docs"""+os.sep+"agpl-3.0.txt")
else:
self.window.show()
self.window.present()
def warning_box(self, str, diatitle="FPDB WARNING"):
diaWarning = gtk.Dialog(title=diatitle, parent=None, flags=0, buttons=(gtk.STOCK_OK,gtk.RESPONSE_OK))
@ -697,7 +701,7 @@ This program is licensed under the AGPL3, see docs"""+os.sep+"agpl-3.0.txt")
response = diaWarning.run()
diaWarning.destroy()
return response
def validate_config(self):
hhbase = self.config.get_import_parameters().get("hhArchiveBase")
hhbase = os.path.expanduser(hhbase)
@ -716,7 +720,7 @@ This program is licensed under the AGPL3, see docs"""+os.sep+"agpl-3.0.txt")
self.warning_box("WARNING: Unable to create hand output directory. Importing is not likely to work until this is fixed.")
elif response == gtk.RESPONSE_NO:
self.select_hhArchiveBase()
def select_hhArchiveBase(self, widget=None):
fc = gtk.FileChooserDialog(title="Select HH Output Directory", parent=None, action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, buttons=(gtk.STOCK_OPEN,gtk.RESPONSE_OK), backend=None)
fc.run()
@ -726,7 +730,7 @@ This program is licensed under the AGPL3, see docs"""+os.sep+"agpl-3.0.txt")
self.config.save()
self.load_profile() # we can't do this at the end of this func because load_profile calls this func
fc.destroy() # TODO: loop this to make sure we get valid data back from it, because the open directory thing in GTK lets you select files and not select things and other stupid bullshit
def main(self):
gtk.main()
return 0

View File

@ -82,10 +82,10 @@ class fpdb_db:
self.connect(backend=db['db-backend'],
host=db['db-host'],
database=db['db-databaseName'],
user=db['db-user'],
user=db['db-user'],
password=db['db-password'])
#end def do_connect
def connect(self, backend=None, host=None, database=None,
user=None, password=None):
"""Connects a database with the given parameters"""
@ -100,12 +100,15 @@ class fpdb_db:
import MySQLdb
if use_pool:
MySQLdb = pool.manage(MySQLdb, pool_size=5)
# try:
self.db = MySQLdb.connect(host=host, user=user, passwd=password, db=database, use_unicode=True)
try:
self.db = MySQLdb.connect(host=host, user=user, passwd=password, db=database, use_unicode=True)
#TODO: Add port option
# except:
# raise FpdbMySQLFailedError("MySQL connection failed")
elif backend==fpdb_db.PGSQL:
except MySQLdb.Error, ex:
if ex.args[0] == 1045:
raise FpdbMySQLAccessDenied(ex.args[0], ex.args[1])
else:
print "*** WARNING UNKNOWN MYSQL ERROR", ex
elif backend == fpdb_db.PGSQL:
import psycopg2
import psycopg2.extensions
if use_pool:
@ -131,8 +134,8 @@ class fpdb_db:
if not connected:
try:
self.db = psycopg2.connect(host = host,
user = user,
password = password,
user = user,
password = password,
database = database)
except:
msg = "PostgreSQL connection to database (%s) user (%s) failed. Are you sure the DB is running?" % (database, user)
@ -187,13 +190,13 @@ class fpdb_db:
self.cursor.close()
self.db.close()
#end def disconnect
def reconnect(self, due_to_error=False):
"""Reconnects the DB"""
#print "started fpdb_db.reconnect"
self.disconnect(due_to_error)
self.connect(self.backend, self.host, self.database, self.user, self.password)
def get_backend_name(self):
"""Returns the name of the currently used backend"""
if self.backend==2:
@ -205,7 +208,7 @@ class fpdb_db:
else:
raise FpdbError("invalid backend")
#end def get_backend_name
def get_db_info(self):
return (self.host, self.database, self.user, self.password)
#end def get_db_info