2008-08-04 05:44:28 +02:00
#!/usr/bin/python
#Copyright 2008 Steffen Jobbagy-Felso
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU Affero General Public License as published by
#the Free Software Foundation, version 3 of the License.
#
#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 Affero General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
#In the "official" distribution you can find the license in
#agpl-3.0.txt in the docs folder of the package.
import os
import sys
2009-08-01 13:45:10 +02:00
import re
# if path is set to use an old version of python look for a new one:
# (does this work in linux?)
if os . name == ' nt ' and sys . version [ 0 : 3 ] not in ( ' 2.5 ' , ' 2.6 ' ) and ' -r ' not in sys . argv :
#print "old path =", os.environ['PATH']
dirs = re . split ( os . pathsep , os . environ [ ' PATH ' ] )
# remove any trailing / or \ chars from dirs:
dirs = [ re . sub ( ' [ \\ /]$ ' , ' ' , p ) for p in dirs ]
# remove any dirs containing 'python' apart from those ending in 'python25', 'python26' or 'python':
dirs = [ p for p in dirs if not re . search ( ' python ' , p , re . I ) or re . search ( ' python25$ ' , p , re . I ) or re . search ( ' python26$ ' , p , re . I ) ]
tmppath = " ; " . join ( dirs )
#print "new path =", tmppath
if re . search ( ' python ' , tmppath , re . I ) :
os . environ [ ' PATH ' ] = tmppath
print " Python " + sys . version [ 0 : 3 ] + ' - press return to continue \n '
sys . stdin . readline ( )
os . execvpe ( ' python.exe ' , ( ' python.exe ' , ' fpdb.py ' , ' -r ' ) , os . environ ) # first arg is ignored (name of program being run)
else :
print " \n python 2.5 not found, please install python 2.5 or 2.6 for fpdb \n "
exit
else :
pass
#print "debug - not changing path"
2009-10-26 10:03:25 +01:00
if os . name == ' nt ' :
import win32api
import win32con
2009-08-01 13:45:10 +02:00
print " Python " + sys . version [ 0 : 3 ] + ' ... \n '
2009-10-13 22:53:51 +02:00
import traceback
2009-07-18 23:21:29 +02:00
import threading
2009-06-15 05:14:53 +02:00
import Options
import string
cl_options = string . join ( sys . argv [ 1 : ] )
( options , sys . argv ) = Options . fpdb_options ( )
2008-11-07 01:14:25 +01:00
if not options . errorsToConsole :
2009-03-03 12:32:01 +01:00
print " Note: error output is being diverted to fpdb-error-log.txt and HUD-error.txt. Any major error will be reported there _only_. "
errorFile = open ( ' fpdb-error-log.txt ' , ' w ' , 0 )
sys . stderr = errorFile
2008-09-24 04:56:44 +02:00
2009-07-14 01:04:10 +02:00
import logging
2008-08-04 05:44:28 +02:00
import pygtk
pygtk . require ( ' 2.0 ' )
import gtk
2009-07-19 13:28:17 +02:00
import interlocks
2009-06-28 20:19:32 +02:00
2008-08-17 02:48:03 +02:00
import GuiBulkImport
2008-12-06 11:09:18 +01:00
import GuiPlayerStats
2009-03-03 12:32:01 +01:00
import GuiPositionalStats
2008-08-17 02:48:03 +02:00
import GuiTableViewer
import GuiAutoImport
2008-09-20 06:56:16 +02:00
import GuiGraphViewer
2009-05-26 10:10:27 +02:00
import GuiSessionViewer
2009-06-23 21:45:11 +02:00
import SQL
2009-06-23 00:30:54 +02:00
import Database
2008-10-08 19:36:08 +02:00
import FpdbSQLQueries
2008-10-21 16:22:01 +02:00
import Configuration
2009-10-13 22:53:51 +02:00
from Exceptions import *
2008-08-04 05:44:28 +02:00
2009-10-23 07:50:06 +02:00
VERSION = " 0.12 "
2009-01-30 19:00:29 +01:00
2008-08-04 05:44:28 +02:00
class fpdb :
2009-03-03 12:32:01 +01:00
def tab_clicked ( self , widget , tab_name ) :
""" called when a tab button is clicked to activate that tab """
self . display_tab ( tab_name )
def add_and_display_tab ( self , new_tab , new_tab_name ) :
""" just calls the component methods """
self . add_tab ( new_tab , new_tab_name )
self . display_tab ( new_tab_name )
def add_tab ( self , new_tab , new_tab_name ) :
""" adds a tab, namely creates the button and displays it and appends all the relevant arrays """
for i in self . tab_names : #todo: check this is valid
2009-09-09 12:01:03 +02:00
if i == new_tab_name :
return # if tab already exists, just go to it
2009-03-03 12:32:01 +01:00
self . tabs . append ( new_tab )
self . tab_names . append ( new_tab_name )
2009-09-10 00:41:32 +02:00
new_tab_sel_button = gtk . ToggleButton ( new_tab_name )
2009-03-03 12:32:01 +01:00
new_tab_sel_button . connect ( " clicked " , self . tab_clicked , new_tab_name )
self . tab_box . add ( new_tab_sel_button )
new_tab_sel_button . show ( )
self . tab_buttons . append ( new_tab_sel_button )
def display_tab ( self , new_tab_name ) :
""" displays the indicated tab """
2009-03-16 23:35:56 +01:00
tab_no = - 1
for i , name in enumerate ( self . tab_names ) :
if name == new_tab_name :
tab_no = i
break
if tab_no == - 1 :
2009-08-12 02:46:39 +02:00
raise FpdbError ( " invalid tab_no " )
2009-03-03 12:32:01 +01:00
else :
self . main_vbox . remove ( self . current_tab )
self . current_tab = self . tabs [ tab_no ]
self . main_vbox . add ( self . current_tab )
self . tab_buttons [ tab_no ] . set_active ( True )
self . current_tab . show ( )
def delete_event ( self , widget , event , data = None ) :
return False
def destroy ( self , widget , data = None ) :
2009-05-23 22:42:26 +02:00
self . quit ( widget )
2009-03-03 12:32:01 +01:00
2009-05-23 22:42:26 +02:00
def dia_about ( self , widget , data = None ) :
2009-09-10 03:57:16 +02:00
#self.warning_box("About FPDB:\n\nFPDB was originally created by a guy named Steffen, sometime in 2008, \nand is mostly worked on these days by people named Eratosthenes, s0rrow, _mt, EricBlade, sqlcoder, and other strange people.\n\n", "ABOUT FPDB")
dia = gtk . AboutDialog ( )
dia . set_name ( " FPDB " )
dia . set_version ( VERSION )
2009-09-10 04:04:38 +02:00
dia . set_copyright ( " 2008-2009, Steffen, Eratosthenes, s0rrow, EricBlade, _mt, sqlcoder, and others " )
2009-09-10 03:57:16 +02:00
dia . set_comments ( " GTK AboutDialog comments here " )
dia . set_license ( " GPL v3 " )
dia . set_website ( " http://fpdb.sourceforge.net/ " )
dia . set_authors ( " Steffen, Eratosthenes, s0rrow, EricBlade, _mt, and others " )
dia . set_program_name ( " FPDB " )
dia . run ( )
dia . destroy ( )
2009-03-03 12:32:01 +01:00
2009-05-23 22:42:26 +02:00
def dia_create_del_database ( self , widget , data = None ) :
2009-09-10 01:36:55 +02:00
self . warning_box ( " Unimplemented: Create/Delete Database " )
2009-03-03 12:32:01 +01:00
self . obtain_global_lock ( )
2009-07-18 23:21:29 +02:00
self . release_global_lock ( )
2009-03-03 12:32:01 +01:00
2009-05-23 22:42:26 +02:00
def dia_create_del_user ( self , widget , data = None ) :
2009-09-10 01:36:55 +02:00
self . warning_box ( " Unimplemented: Create/Delete user " )
2009-03-03 12:32:01 +01:00
self . obtain_global_lock ( )
2009-07-18 23:21:29 +02:00
self . release_global_lock ( )
2009-03-03 12:32:01 +01:00
2009-05-23 22:42:26 +02:00
def dia_database_stats ( self , widget , data = None ) :
2009-09-10 01:36:55 +02:00
self . warning_box ( " Unimplemented: Database Stats " )
2009-03-03 12:32:01 +01:00
2009-05-23 22:42:26 +02:00
def dia_delete_db_parts ( self , widget , data = None ) :
2009-09-10 01:36:55 +02:00
self . warning_box ( " Unimplemented: Delete Database Parts " )
2009-03-03 12:32:01 +01:00
self . obtain_global_lock ( )
2009-07-18 23:21:29 +02:00
self . release_global_lock ( )
2009-03-03 12:32:01 +01:00
def dia_edit_profile ( self , widget = None , data = None , create_default = False , path = None ) :
2009-09-10 01:36:55 +02:00
self . warning_box ( " Unimplemented: Edit Profile " )
2009-03-03 12:32:01 +01:00
self . obtain_global_lock ( )
2009-07-18 23:21:29 +02:00
self . release_global_lock ( )
2009-03-03 12:32:01 +01:00
2009-05-23 22:42:26 +02:00
def dia_export_db ( self , widget , data = None ) :
2009-09-10 01:36:55 +02:00
self . warning_box ( " Unimplemented: Export Database " )
2009-03-03 12:32:01 +01:00
self . obtain_global_lock ( )
2009-07-18 23:21:29 +02:00
self . release_global_lock ( )
2009-03-03 12:32:01 +01:00
def dia_get_db_root_credentials ( self ) :
""" obtains db root credentials from user """
2009-09-10 01:36:55 +02:00
self . warning_box ( " Unimplemented: Get Root Database Credentials " )
2009-03-03 12:32:01 +01:00
# 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)
2009-05-23 22:42:26 +02:00
def dia_import_db ( self , widget , data = None ) :
2009-09-10 01:36:55 +02:00
self . warning_box ( " Unimplemented: Import Database " )
2009-03-03 12:32:01 +01:00
self . obtain_global_lock ( )
2009-07-18 23:21:29 +02:00
self . release_global_lock ( )
2009-03-03 12:32:01 +01:00
2009-05-23 22:42:26 +02:00
def dia_licensing ( self , widget , data = None ) :
2009-09-10 01:36:55 +02:00
self . warning_box ( " Unimplemented: Licensing " )
2009-03-03 12:32:01 +01:00
2009-05-23 22:42:26 +02:00
def dia_load_profile ( self , widget , data = None ) :
2009-03-03 12:32:01 +01:00
""" Dialogue to select a file to load a profile from """
2009-07-18 23:21:29 +02:00
if self . obtain_global_lock ( ) : # returns true if successful
2009-06-23 21:45:11 +02:00
#try:
# chooser = gtk.FileChooserDialog(title="Please select a profile file to load",
# action=gtk.FILE_CHOOSER_ACTION_OPEN,
# buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))
# chooser.set_filename(self.profile)
# response = chooser.run()
# chooser.destroy()
# if response == gtk.RESPONSE_OK:
# self.load_profile(chooser.get_filename())
# elif response == gtk.RESPONSE_CANCEL:
# print 'User cancelled loading profile'
#except:
# pass
2009-07-31 06:13:51 +02:00
#try:
self . load_profile ( )
#except:
# pass
2009-06-04 21:27:15 +02:00
self . r elease_global_lock ( )
2009-03-03 12:32:01 +01:00
2009-05-23 22:42:26 +02:00
def dia_recreate_tables ( self , widget , data = None ) :
2009-03-03 12:32:01 +01:00
""" Dialogue that asks user to confirm that he wants to delete and recreate the tables """
2009-07-18 23:21:29 +02:00
if self . obtain_global_lock ( ) : # returns true if successful
2009-06-04 21:27:15 +02:00
2009-07-18 23:21:29 +02:00
#lock_released = False
2009-08-16 11:46:41 +02:00
dia_confirm = gtk . MessageDialog ( parent = None , flags = 0 , type = gtk . MESSAGE_WARNING ,
buttons = ( gtk . BUTTONS_YES_NO ) , message_format = " Confirm deleting and recreating tables " )
diastring = " Please confirm that you want to (re-)create the tables. If there already are tables in the database " \
2009-10-13 22:53:51 +02:00
+ self . db . database + " on " + self . db . host + " they will be deleted. "
2009-08-16 11:46:41 +02:00
dia_confirm . format_secondary_text ( diastring ) #todo: make above string with bold for db, host and deleted
response = dia_confirm . run ( )
dia_confirm . destroy ( )
if response == gtk . RESPONSE_YES :
2009-10-13 22:53:51 +02:00
#if self.db.backend == self.fdb_lock.fdb.MYSQL_INNODB:
2009-08-16 11:46:41 +02:00
# 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()
# lock_released = True
self . db . recreate_tables ( )
#else:
# for other dbs use same connection as holds global lock
# self.fdb_lock.fdb.recreate_tables()
elif response == gtk . RESPONSE_NO :
print ' User cancelled recreating tables '
2009-07-18 23:21:29 +02:00
#if not lock_released:
self . release_global_lock ( )
2009-07-27 23:28:06 +02:00
def dia_recreate_hudcache ( self , widget , data = None ) :
if self . obtain_global_lock ( ) :
2009-09-27 00:32:44 +02:00
self . dia_confirm = gtk . MessageDialog ( parent = None , flags = 0 , type = gtk . MESSAGE_WARNING , buttons = ( gtk . BUTTONS_YES_NO ) , message_format = " Confirm recreating HUD cache " )
2009-07-31 06:15:25 +02:00
diastring = " Please confirm that you want to re-create the HUD cache. "
2009-09-27 00:32:44 +02:00
self . dia_confirm . format_secondary_text ( diastring )
hb = gtk . HBox ( True , 1 )
self . start_date = gtk . Entry ( max = 12 )
2009-09-27 02:42:26 +02:00
self . start_date . set_text ( self . db . get_hero_hudcache_start ( ) )
2009-09-27 00:32:44 +02:00
lbl = gtk . Label ( " Hero ' s cache starts: " )
btn = gtk . Button ( )
btn . set_image ( gtk . image_new_from_stock ( gtk . STOCK_INDEX , gtk . ICON_SIZE_BUTTON ) )
btn . connect ( ' clicked ' , self . __calendar_dialog , self . start_date )
hb . pack_start ( lbl , expand = True , padding = 3 )
hb . pack_start ( self . start_date , expand = True , padding = 2 )
hb . pack_start ( btn , expand = False , padding = 3 )
self . dia_confirm . vbox . add ( hb )
hb . show_all ( )
response = self . dia_confirm . run ( )
self . dia_confirm . destroy ( )
2009-07-31 06:15:25 +02:00
if response == gtk . RESPONSE_YES :
2009-09-27 00:32:44 +02:00
self . db . rebuild_hudcache ( self . start_date . get_text ( ) )
elif response == gtk . RESPONSE_NO :
2009-07-31 06:15:25 +02:00
print ' User cancelled rebuilding hud cache '
2009-09-27 00:32:44 +02:00
2009-07-27 23:28:06 +02:00
self . release_global_lock ( )
2009-09-27 00:32:44 +02:00
def __calendar_dialog ( self , widget , entry ) :
self . dia_confirm . set_modal ( False )
d = gtk . Window ( gtk . WINDOW_TOPLEVEL )
d . set_title ( ' Pick a date ' )
vb = gtk . VBox ( )
cal = gtk . Calendar ( )
vb . pack_start ( cal , expand = False , padding = 0 )
btn = gtk . Button ( ' Done ' )
btn . connect ( ' clicked ' , self . __get_date , cal , entry , d )
vb . pack_start ( btn , expand = False , padding = 4 )
d . add ( vb )
d . set_position ( gtk . WIN_POS_MOUSE )
d . show_all ( )
def __get_dates ( self ) :
t1 = self . start_date . get_text ( )
if t1 == ' ' :
t1 = ' 1970-01-01 '
return ( t1 )
def __get_date ( self , widget , calendar , entry , win ) :
# year and day are correct, month is 0..11
( year , month , day ) = calendar . get_date ( )
month + = 1
ds = ' %04d - %02d - %02d ' % ( year , month , day )
entry . set_text ( ds )
win . destroy ( )
self . dia_confirm . set_modal ( True )
2009-07-27 23:28:06 +02:00
2009-05-23 22:42:26 +02:00
def dia_regression_test ( self , widget , data = None ) :
2009-09-10 01:36:55 +02:00
self . warning_box ( " Unimplemented: Regression Test " )
2009-03-03 12:32:01 +01:00
self . obtain_global_lock ( )
2009-07-18 23:21:29 +02:00
self . release_global_lock ( )
2009-03-03 12:32:01 +01:00
2009-05-23 22:42:26 +02:00
def dia_save_profile ( self , widget , data = None ) :
2009-09-10 01:36:55 +02:00
self . warning_box ( " Unimplemented: Save Profile (try saving a HUD layout, that should do it) " )
2009-03-03 12:32:01 +01:00
def diaSetupWizard ( self , path ) :
diaSetupWizard = gtk . Dialog ( title = " Fatal Error - Config File Missing " , parent = None , flags = 0 , buttons = ( gtk . STOCK_QUIT , gtk . RESPONSE_OK ) )
label = gtk . Label ( " Please copy the config file from the docs folder to: " )
diaSetupWizard . vbox . add ( label )
label . show ( )
label = gtk . Label ( path )
diaSetupWizard . vbox . add ( label )
label . show ( )
label = gtk . Label ( " and edit it according to the install documentation at http://fpdb.sourceforge.net " )
diaSetupWizard . vbox . add ( label )
label . show ( )
response = diaSetupWizard . run ( )
sys . exit ( 1 )
def get_menu ( self , window ) :
""" returns the menu for this program """
2009-05-23 22:42:26 +02:00
fpdbmenu = """
< ui >
< menubar name = " MenuBar " >
< menu action = " main " >
< menuitem action = " LoadProf " / >
< menuitem action = " EditProf " / >
< menuitem action = " SaveProf " / >
< separator / >
< menuitem action = " Quit " / >
< / menu >
< menu action = " import " >
2009-09-10 03:26:44 +02:00
< menuitem action = " sethharchive " / >
2009-05-23 22:42:26 +02:00
< menuitem action = " bulkimp " / >
< menuitem action = " autoimp " / >
< menuitem action = " autorate " / >
< / menu >
< menu action = " viewers " >
< menuitem action = " autoimp " / >
< menuitem action = " graphs " / >
< menuitem action = " handreplay " / >
< menuitem action = " playerdetails " / >
< menuitem action = " playerstats " / >
< menuitem action = " posnstats " / >
2009-10-21 11:22:47 +02:00
< menuitem action = " sessionstats " / >
2009-05-23 22:42:26 +02:00
< menuitem action = " sessionreplay " / >
< menuitem action = " tableviewer " / >
< / menu >
< menu action = " database " >
< menuitem action = " createdb " / >
< menuitem action = " createuser " / >
< menuitem action = " createtabs " / >
2009-07-27 23:28:06 +02:00
< menuitem action = " rebuildhudcache " / >
2009-05-23 22:42:26 +02:00
< menuitem action = " stats " / >
< / menu >
< menu action = " help " >
< menuitem action = " Abbrev " / >
< separator / >
< menuitem action = " About " / >
< menuitem action = " License " / >
< / menu >
< / menubar >
< / ui > """
uimanager = gtk . UIManager ( )
accel_group = uimanager . get_accel_group ( )
actiongroup = gtk . ActionGroup ( ' UIManagerExample ' )
# Create actions
actiongroup . add_actions ( [ ( ' main ' , None , ' _Main ' ) ,
2009-05-31 22:53:10 +02:00
( ' Quit ' , gtk . STOCK_QUIT , ' _Quit ' , None , ' Quit the Program ' , self . quit ) ,
2009-05-23 22:42:26 +02:00
( ' LoadProf ' , None , ' _Load Profile (broken) ' , ' <control>L ' , ' Load your profile ' , self . dia_load_profile ) ,
( ' EditProf ' , None , ' _Edit Profile (todo) ' , ' <control>E ' , ' Edit your profile ' , self . dia_edit_profile ) ,
( ' SaveProf ' , None , ' _Save Profile (todo) ' , ' <control>S ' , ' Save your profile ' , self . dia_save_profile ) ,
( ' import ' , None , ' _Import ' ) ,
2009-09-10 03:26:44 +02:00
( ' sethharchive ' , None , ' _Set HandHistory Archive Directory ' , None , ' Set HandHistory Archive Directory ' , self . select_hhArchiveBase ) ,
2009-05-23 22:42:26 +02:00
( ' bulkimp ' , None , ' _Bulk Import ' , ' <control>B ' , ' Bulk Import ' , self . tab_bulk_import ) ,
( ' autorate ' , None , ' Auto _Rating (todo) ' , ' <control>R ' , ' Auto Rating (todo) ' , self . not_implemented ) ,
( ' viewers ' , None , ' _Viewers ' ) ,
( ' autoimp ' , None , ' _Auto Import and HUD ' , ' <control>A ' , ' Auto Import and HUD ' , self . tab_auto_import ) ,
( ' graphs ' , None , ' _Graphs ' , ' <control>G ' , ' Graphs ' , self . tabGraphViewer ) ,
( ' handreplay ' , None , ' Hand _Replayer (todo) ' , None , ' Hand Replayer (todo) ' , self . not_implemented ) ,
( ' playerdetails ' , None , ' Player _Details (todo) ' , None , ' Player Details (todo) ' , self . not_implemented ) ,
2009-05-23 23:06:41 +02:00
( ' playerstats ' , None , ' _Player Stats (tabulated view) ' , ' <control>P ' , ' Player Stats (tabulated view) ' , self . tab_player_stats ) ,
( ' posnstats ' , None , ' P_ositional Stats (tabulated view) ' , ' <control>O ' , ' Positional Stats (tabulated view) ' , self . tab_positional_stats ) ,
2009-10-21 11:22:47 +02:00
( ' sessionstats ' , None , ' Session Stats ' , None , ' Session Stats ' , self . tab_session_stats ) ,
2009-05-23 22:42:26 +02:00
( ' sessionreplay ' , None , ' _Session Replayer (todo) ' , None , ' Session Replayer (todo) ' , self . not_implemented ) ,
( ' tableviewer ' , None , ' Poker_table Viewer (mostly obselete) ' , None , ' Poker_table Viewer (mostly obselete) ' , self . tab_table_viewer ) ,
( ' database ' , None , ' _Database ' ) ,
( ' createdb ' , None , ' Create or Delete _Database (todo) ' , None , ' Create or Delete Database ' , self . dia_create_del_database ) ,
( ' createuser ' , None , ' Create or Delete _User (todo) ' , None , ' Create or Delete User ' , self . dia_create_del_user ) ,
( ' createtabs ' , None , ' Create or Recreate _Tables ' , None , ' Create or Recreate Tables ' , self . dia_recreate_tables ) ,
2009-07-27 23:28:06 +02:00
( ' rebuildhudcache ' , None , ' Rebuild HUD Cache ' , None , ' Rebuild HUD Cache ' , self . dia_recreate_hudcache ) ,
2009-05-23 22:42:26 +02:00
( ' stats ' , None , ' _Statistics (todo) ' , None , ' View Database Statistics ' , self . dia_database_stats ) ,
( ' help ' , None , ' _Help ' ) ,
( ' Abbrev ' , None , ' _Abbrevations (todo) ' , None , ' List of Abbrevations ' , self . tab_abbreviations ) ,
( ' About ' , None , ' A_bout ' , None , ' About the program ' , self . dia_about ) ,
( ' License ' , None , ' _License and Copying (todo) ' , None , ' License and Copying ' , self . dia_licensing ) ,
] )
actiongroup . get_action ( ' Quit ' ) . set_property ( ' short-label ' , ' _Quit ' )
uimanager . insert_action_group ( actiongroup , 0 )
merge_id = uimanager . add_ui_from_string ( fpdbmenu )
# Create a MenuBar
menubar = uimanager . get_widget ( ' /MenuBar ' )
2009-03-03 12:32:01 +01:00
window . add_accel_group ( accel_group )
2009-05-23 22:42:26 +02:00
return menubar
2009-03-03 12:32:01 +01:00
def load_profile ( self ) :
""" Loads profile from the provided path name. """
2009-06-23 21:45:11 +02:00
self . config = Configuration . Config ( file = options . config , dbname = options . dbname )
2009-03-03 12:32:01 +01:00
self . settings = { }
2009-07-18 23:21:29 +02:00
self . settings [ ' global_lock ' ] = self . lock
2009-03-03 12:32:01 +01:00
if ( os . sep == " / " ) :
self . settings [ ' os ' ] = " linuxmac "
else :
self . settings [ ' os ' ] = " windows "
2009-06-15 05:14:53 +02:00
self . settings . update ( { ' cl_options ' : cl_options } )
2009-03-27 15:53:44 +01:00
self . settings . update ( self . config . get_db_parameters ( ) )
2009-03-03 12:32:01 +01:00
self . settings . update ( self . config . get_tv_parameters ( ) )
self . settings . update ( self . config . get_import_parameters ( ) )
self . settings . update ( self . config . get_default_paths ( ) )
2009-06-28 20:19:32 +02:00
if self . db != None and self . db . fdb != None :
2009-03-03 12:32:01 +01:00
self . db . disconnect ( )
2009-06-28 20:19:32 +02:00
self . sql = SQL . Sql ( type = self . settings [ ' db-type ' ] , db_server = self . settings [ ' db-server ' ] )
2009-10-09 13:31:25 +02:00
try :
self . db = Database . Database ( self . config , sql = self . sql )
2009-10-13 22:53:51 +02:00
except FpdbMySQLFailedError :
2009-10-09 13:31:25 +02:00
self . warning_box ( " Unable to connect to MySQL! Is the MySQL server running?! " , " FPDB ERROR " )
exit ( )
2009-10-04 18:46:04 +02:00
except FpdbError :
2009-10-13 22:53:51 +02:00
#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 ] )
2009-10-04 18:46:04 +02:00
sys . stderr . write ( " Failed to connect to %s database with username %s . " % ( self . settings [ ' db-server ' ] , self . settings [ ' db-user ' ] ) )
except :
2009-10-13 22:53:51 +02:00
#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 ] )
2009-10-04 18:46:04 +02:00
sys . stderr . write ( " Failed to connect to %s database with username %s . " % ( self . settings [ ' db-server ' ] , self . settings [ ' db-user ' ] ) )
2009-06-28 20:19:32 +02:00
2009-10-13 22:53:51 +02:00
if self . db . wrongDbVersion :
2009-03-03 12:32:01 +01:00
diaDbVersionWarning = gtk . Dialog ( title = " Strong Warning - Invalid database version " , parent = None , flags = 0 , buttons = ( gtk . STOCK_OK , gtk . RESPONSE_OK ) )
label = gtk . Label ( " An invalid DB version or missing tables have been detected. " )
diaDbVersionWarning . vbox . add ( label )
label . show ( )
label = gtk . Label ( " This error is not necessarily fatal but it is strongly recommended that you recreate the tables by using the Database menu. " )
diaDbVersionWarning . vbox . add ( label )
label . show ( )
label = gtk . Label ( " Not doing this will likely lead to misbehaviour including fpdb crashes, corrupt data etc. " )
diaDbVersionWarning . vbox . add ( label )
label . show ( )
response = diaDbVersionWarning . run ( )
diaDbVersionWarning . destroy ( )
2009-06-23 21:45:11 +02:00
if self . status_bar == None :
2009-10-13 22:53:51 +02:00
self . status_bar = gtk . Label ( " Status: Connected to %s database named %s on host %s " % ( self . db . get_backend_name ( ) , self . db . database , self . db . host ) )
2009-06-23 21:45:11 +02:00
self . main_vbox . pack_end ( self . status_bar , False , True , 0 )
self . status_bar . show ( )
else :
2009-10-13 22:53:51 +02:00
self . status_bar . set_text ( " Status: Connected to %s database named %s on host %s " % ( self . db . get_backend_name ( ) , self . db . database , self . db . host ) )
2009-06-23 21:45:11 +02:00
2009-03-03 12:32:01 +01:00
# Database connected to successfully, load queries to pass on to other classes
2009-10-13 22:53:51 +02:00
self . db . rollback ( )
2009-09-10 01:20:50 +02:00
self . validate_config ( )
2009-03-03 12:32:01 +01:00
2009-05-25 22:30:37 +02:00
def not_implemented ( self , widget , data = None ) :
2009-09-10 01:36:55 +02:00
self . warning_box ( " Unimplemented menu entry " )
2009-03-03 12:32:01 +01:00
def obtain_global_lock ( self ) :
2009-07-18 23:21:29 +02:00
ret = self . lock . acquire ( False ) # will return false if lock is already held
if ret :
print " \n Global lock taken ... "
else :
print " \n Failed to get global lock. "
return ret
# need to release it later:
# self.lock.release()
2009-06-09 22:38:30 +02:00
def quit ( self , widget , data = None ) :
2009-03-03 12:32:01 +01:00
print " Quitting normally "
#check if current settings differ from profile, if so offer to save or abort
self . db . disconnect ( )
2009-10-26 23:26:22 +01:00
# hide icon as it doesn't go away immediately in Windows - is this ok in Linux Eric?
self . statusIcon . set_visible ( False )
2009-03-03 12:32:01 +01:00
gtk . main_quit ( )
def release_global_lock ( self ) :
2009-07-18 23:21:29 +02:00
self . lock . release ( )
2009-06-30 23:18:15 +02:00
print " Global lock released. \n "
2009-03-03 12:32:01 +01:00
2009-05-23 22:42:26 +02:00
def tab_abbreviations ( self , widget , data = None ) :
2009-03-03 12:32:01 +01:00
print " todo: implement tab_abbreviations "
2009-05-23 22:42:26 +02:00
def tab_auto_import ( self , widget , data = None ) :
2009-03-03 12:32:01 +01:00
""" opens the auto import tab """
2009-09-10 00:41:32 +02:00
new_aimp_thread = GuiAutoImport . GuiAutoImport ( self . settings , self . config , self . sql )
2009-03-03 12:32:01 +01:00
self . threads . append ( new_aimp_thread )
aimp_tab = new_aimp_thread . get_vbox ( )
self . add_and_display_tab ( aimp_tab , " Auto Import " )
2009-05-23 22:42:26 +02:00
def tab_bulk_import ( self , widget , data = None ) :
2009-03-03 12:32:01 +01:00
""" opens a tab for bulk importing """
#print "start of tab_bulk_import"
2009-07-29 07:37:06 +02:00
new_import_thread = GuiBulkImport . GuiBulkImport ( self . settings , self . config , self . sql )
2009-03-03 12:32:01 +01:00
self . threads . append ( new_import_thread )
bulk_tab = new_import_thread . get_vbox ( )
self . add_and_display_tab ( bulk_tab , " Bulk Import " )
2009-05-23 22:42:26 +02:00
def tab_player_stats ( self , widget , data = None ) :
2009-09-10 00:41:32 +02:00
new_ps_thread = GuiPlayerStats . GuiPlayerStats ( self . config , self . sql , self . window )
2009-03-03 12:32:01 +01:00
self . threads . append ( new_ps_thread )
ps_tab = new_ps_thread . get_vbox ( )
self . add_and_display_tab ( ps_tab , " Player Stats " )
2009-05-23 22:42:26 +02:00
def tab_positional_stats ( self , widget , data = None ) :
2009-07-19 19:28:13 +02:00
new_ps_thread = GuiPositionalStats . GuiPositionalStats ( self . config , self . sql )
2009-03-03 12:32:01 +01:00
self . threads . append ( new_ps_thread )
ps_tab = new_ps_thread . get_vbox ( )
2009-03-23 14:18:22 +01:00
self . add_and_display_tab ( ps_tab , " Positional Stats " )
2009-03-03 12:32:01 +01:00
2009-10-21 11:22:47 +02:00
def tab_session_stats ( self , widget , data = None ) :
new_ps_thread = GuiSessionViewer . GuiSessionViewer ( self . config , self . sql , self . window )
self . threads . append ( new_ps_thread )
ps_tab = new_ps_thread . get_vbox ( )
self . add_and_display_tab ( ps_tab , " Session Stats " )
2009-05-23 22:42:26 +02:00
def tab_main_help ( self , widget , data = None ) :
2009-03-03 12:32:01 +01:00
""" Displays a tab with the main fpdb help screen """
mh_tab = gtk . Label ( """ Welcome to Fpdb!
2008-09-01 18:32:18 +02:00
For documentation please visit our website at http : / / fpdb . sourceforge . net / or check the docs directory in the fpdb folder .
2008-11-10 03:02:12 +01:00
Please note that default . conf is no longer needed nor used , all configuration now happens in HUD_config . xml
2008-08-11 01:44:46 +02:00
This program is licensed under the AGPL3 , see docs """ +os.sep+ " agpl-3.0.txt " )
2009-03-03 12:32:01 +01:00
self . add_and_display_tab ( mh_tab , " Help " )
2009-05-23 22:42:26 +02:00
def tab_table_viewer ( self , widget , data = None ) :
2009-03-03 12:32:01 +01:00
""" opens a table viewer tab """
2009-08-06 22:12:50 +02:00
new_tv_thread = GuiTableViewer . GuiTableViewer ( self . db , self . settings , self . config )
2009-03-03 12:32:01 +01:00
self . threads . append ( new_tv_thread )
2009-09-10 00:41:32 +02:00
tv_tab = new_tv_thread . get_vbox ( )
2009-03-03 12:32:01 +01:00
self . add_and_display_tab ( tv_tab , " Table Viewer " )
2009-05-23 22:42:26 +02:00
def tabGraphViewer ( self , widget , data = None ) :
2009-03-03 12:32:01 +01:00
""" opens a graph viewer tab """
2009-07-19 22:19:58 +02:00
new_gv_thread = GuiGraphViewer . GuiGraphViewer ( self . sql , self . config )
2009-03-03 12:32:01 +01:00
self . threads . append ( new_gv_thread )
2009-09-10 00:41:32 +02:00
gv_tab = new_gv_thread . get_vbox ( )
2009-03-03 12:32:01 +01:00
self . add_and_display_tab ( gv_tab , " Graphs " )
def __init__ ( self ) :
2009-06-23 21:45:11 +02:00
self . threads = [ ]
2009-07-19 13:28:17 +02:00
# no more than 1 process can this lock at a time:
self . lock = interlocks . InterProcessLock ( name = " fpdb_global_lock " )
2009-06-23 21:45:11 +02:00
self . db = None
self . status_bar = None
2009-03-03 12:32:01 +01:00
self . window = gtk . Window ( gtk . WINDOW_TOPLEVEL )
self . window . connect ( " delete_event " , self . delete_event )
self . window . connect ( " destroy " , self . destroy )
self . window . set_title ( " Free Poker DB - v %s or higher " % ( VERSION , ) )
self . window . set_border_width ( 1 )
2009-04-10 22:28:54 +02:00
self . window . set_default_size ( 900 , 720 )
2009-03-03 12:32:01 +01:00
self . window . set_resizable ( True )
self . main_vbox = gtk . VBox ( False , 1 )
self . main_vbox . set_border_width ( 1 )
self . window . add ( self . main_vbox )
self . main_vbox . show ( )
menubar = self . get_menu ( self . window )
self . main_vbox . pack_start ( menubar , False , True , 0 )
menubar . show ( )
#done menubar
self . tabs = [ ]
self . tab_names = [ ]
self . tab_buttons = [ ]
2009-05-22 00:09:11 +02:00
self . tab_box = gtk . HBox ( True , 1 )
2009-03-03 12:32:01 +01:00
self . main_vbox . pack_start ( self . tab_box , False , True , 0 )
self . tab_box . show ( )
#done tab bar
self . current_tab = gtk . VBox ( False , 1 )
self . current_tab . set_border_width ( 1 )
self . main_vbox . add ( self . current_tab )
self . current_tab . show ( )
self . tab_main_help ( None , None )
self . window . show ( )
2009-06-23 21:45:11 +02:00
self . load_profile ( )
2009-10-26 06:36:29 +01:00
self . statusIcon = gtk . StatusIcon ( )
self . statusIcon . set_from_stock ( gtk . STOCK_HOME )
self . statusIcon . set_tooltip ( " Free Poker Database " )
self . statusIcon . connect ( ' activate ' , self . statusicon_activate )
self . statusMenu = gtk . Menu ( )
menuItem = gtk . ImageMenuItem ( gtk . STOCK_ABOUT )
menuItem . connect ( ' activate ' , self . dia_about )
self . statusMenu . append ( menuItem )
menuItem = gtk . ImageMenuItem ( gtk . STOCK_QUIT )
menuItem . connect ( ' activate ' , self . quit )
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 )
2009-05-23 22:42:26 +02:00
sys . stderr . write ( " fpdb starting ... " )
2009-10-26 06:36:29 +01:00
def window_state_event_cb ( self , window , event ) :
print " window_state_event " , event
if event . changed_mask & gtk . gdk . WINDOW_STATE_ICONIFIED :
2009-10-26 10:03:25 +01:00
# -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)
2009-10-26 06:36:29 +01:00
if event . new_window_state & gtk . gdk . WINDOW_STATE_ICONIFIED :
2009-10-26 10:03:25 +01:00
self . window . hide ( )
2009-10-26 06:36:29 +01:00
self . window . set_skip_taskbar_hint ( True )
self . window . set_skip_pager_hint ( True )
else :
self . window . set_skip_taskbar_hint ( False )
self . window . set_skip_pager_hint ( False )
2009-10-26 12:29:32 +01:00
# Tell GTK not to propagate this signal any further
return True
2009-10-26 06:36:29 +01:00
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
# didn't use any long scope variables.. which might be an alright
# idea too sometime
if button == 3 :
if data :
data . show_all ( )
data . popup ( None , None , None , 3 , time )
pass
def statusicon_activate ( self , widget , data = None ) :
2009-10-26 10:03:25 +01:00
self . window . show ( )
2009-10-26 06:36:29 +01:00
self . window . present ( )
2009-09-10 01:20:50 +02:00
def warning_box ( self , str , diatitle = " FPDB WARNING " ) :
diaWarning = gtk . Dialog ( title = diatitle , parent = None , flags = 0 , buttons = ( gtk . STOCK_OK , gtk . RESPONSE_OK ) )
label = gtk . Label ( str )
diaWarning . vbox . add ( label )
label . show ( )
response = diaWarning . run ( )
diaWarning . destroy ( )
return response
2009-09-10 03:26:44 +02:00
2009-09-10 01:20:50 +02:00
def validate_config ( self ) :
hhbase = self . config . get_import_parameters ( ) . get ( " hhArchiveBase " )
hhbase = os . path . expanduser ( hhbase )
#hhdir = os.path.join(hhbase,site)
hhdir = hhbase
if not os . path . isdir ( hhdir ) :
diapath = gtk . MessageDialog ( parent = None , flags = 0 , type = gtk . MESSAGE_WARNING , buttons = ( gtk . BUTTONS_YES_NO ) , message_format = " Setup hh dir " )
2009-09-10 01:36:55 +02:00
diastring = " WARNING: Unable to find output hh directory %s \n \n Press YES to create this directory, or NO to select a new one. " % hhdir
2009-09-10 01:20:50 +02:00
diapath . format_secondary_text ( diastring )
response = diapath . run ( )
diapath . destroy ( )
if response == gtk . RESPONSE_YES :
try :
os . makedirs ( hhdir )
except :
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 :
2009-09-10 03:26:44 +02:00
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 ( )
# TODO: We need to put in a Cancel button, and handle if the user presses that or the "Close" box without selecting anything as a cancel, and return to the prior setting
#self.warning_box("You selected %s" % fc.get_filename())
self . config . set_hhArchiveBase ( fc . get_filename ( ) )
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
2009-03-03 12:32:01 +01:00
def main ( self ) :
gtk . main ( )
return 0
2008-08-04 05:44:28 +02:00
if __name__ == " __main__ " :
2009-03-03 12:32:01 +01:00
me = fpdb ( )
me . main ( )