2010-07-08 20:01:03 +02:00
#!/usr/bin/env python
2009-08-08 10:07:30 +02:00
# -*- coding: utf-8 -*-
2008-08-19 00:53:25 +02:00
""" Configuration.py
Handles HUD configuration files .
"""
2010-07-04 03:05:16 +02:00
# Copyright 2008-2010, Ray E. Barker
2008-08-19 00:53:25 +02:00
2009-11-22 06:00:23 +01:00
#
2008-08-19 00:53:25 +02:00
# 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.
2009-11-22 06:00:23 +01:00
#
2008-08-19 00:53:25 +02:00
# 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.
2009-11-22 06:00:23 +01:00
#
2008-08-19 00:53:25 +02:00
# 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
########################################################################
2010-09-22 17:51:56 +02:00
import L10n
_ = L10n . get_translation ( )
2008-08-19 00:53:25 +02:00
# Standard Library modules
2009-09-16 07:13:42 +02:00
from __future__ import with_statement
2008-09-27 01:21:38 +02:00
import os
import sys
2008-12-13 23:37:23 +01:00
import inspect
2008-10-05 02:22:53 +02:00
import string
2008-09-29 16:12:04 +02:00
import traceback
2008-09-15 22:31:55 +02:00
import shutil
2009-12-22 06:49:22 +01:00
import locale
2010-06-09 23:46:17 +02:00
import re
2008-08-19 00:53:25 +02:00
import xml . dom . minidom
from xml . dom . minidom import Node
2009-08-12 02:46:39 +02:00
import logging , logging . config
2009-10-21 19:24:46 +02:00
import ConfigParser
2010-02-01 22:03:51 +01:00
# logging has been set up in fpdb.py or HUD_main.py, use their settings:
log = logging . getLogger ( " config " )
2009-11-09 04:53:10 +01:00
##############################################################################
# Functions for finding config files and setting up logging
# Also used in other modules that use logging.
def get_default_config_path ( ) :
""" Returns the path where the fpdb config file _should_ be stored. """
if os . name == ' posix ' :
config_path = os . path . join ( os . path . expanduser ( " ~ " ) , ' .fpdb ' )
elif os . name == ' nt ' :
2010-07-30 00:51:47 +02:00
config_path = os . path . join ( unicode ( os . environ [ u " APPDATA " ] , " latin-1 " ) , u " fpdb " )
#print u"path after joining in get_default_config_path:",config_path
2009-11-09 04:53:10 +01:00
else : config_path = False
2010-07-30 00:51:47 +02:00
try : os . mkdir ( config_path )
except : pass
2009-11-09 04:53:10 +01:00
return config_path
def get_exec_path ( ) :
2010-01-31 12:25:24 +01:00
""" Returns the path to the fpdb(dir|.exe) file we are executing """
2009-11-09 04:53:10 +01:00
if hasattr ( sys , " frozen " ) : # compiled by py2exe
return os . path . dirname ( sys . executable )
2009-11-22 06:00:23 +01:00
else :
2010-01-31 12:25:24 +01:00
return os . path . dirname ( sys . path [ 0 ] ) # should be path to /fpdb
2009-11-09 04:53:10 +01:00
def get_config ( file_name , fallback = True ) :
""" Looks in cwd and in self.default_config_path for a config file. """
2010-08-29 13:46:22 +02:00
# look for example file even if not used here, path is returned to caller
config_found , example_found , example_copy = False , False , False
config_path , example_path = None , None
2010-01-28 00:48:39 +01:00
exec_dir = get_exec_path ( )
2010-02-12 09:18:42 +01:00
if file_name == ' logging.conf ' and not hasattr ( sys , " frozen " ) :
2010-01-31 12:25:24 +01:00
config_path = os . path . join ( exec_dir , ' pyfpdb ' , file_name )
else :
config_path = os . path . join ( exec_dir , file_name )
2009-11-22 22:40:56 +01:00
# print "config_path=", config_path
2009-11-09 04:53:10 +01:00
if os . path . exists ( config_path ) : # there is a file in the cwd
2010-08-29 13:46:22 +02:00
config_found = True # so we use it
2009-11-09 04:53:10 +01:00
else : # no file in the cwd, look where it should be in the first place
2010-01-28 00:48:39 +01:00
default_dir = get_default_config_path ( )
config_path = os . path . join ( default_dir , file_name )
2009-11-22 22:40:56 +01:00
# print "config path 2=", config_path
2009-11-09 04:53:10 +01:00
if os . path . exists ( config_path ) :
2010-08-29 13:46:22 +02:00
config_found = True
2009-11-09 04:53:10 +01:00
2010-08-17 07:18:46 +02:00
# Example configuration for debian package
if os . name == ' posix ' :
# If we're on linux, try to copy example from the place
# debian package puts it; get_default_config_path() creates
# the config directory for us so there's no need to check it
# again
example_path = ' /usr/share/python-fpdb/ ' + file_name + ' .example '
2010-08-29 13:46:22 +02:00
if not config_found and fallback :
try :
shutil . copyfile ( example_path , config_path )
example_copy = True
msg = _ ( " Config file has been created at %s . \n " ) % config_path
logging . info ( msg )
except IOError :
pass
2009-11-09 04:53:10 +01:00
# OK, fall back to the .example file, should be in the start dir
2010-08-29 13:46:22 +02:00
elif os . path . exists ( file_name + " .example " ) :
2009-11-09 04:53:10 +01:00
try :
2010-01-28 00:48:39 +01:00
print " "
2010-08-29 13:46:22 +02:00
example_path = file_name + " .example "
2010-01-31 12:25:24 +01:00
check_dir ( default_dir )
2010-08-29 13:46:22 +02:00
if not config_found and fallback :
shutil . copyfile ( example_path , config_path )
example_copy = True
msg = _ ( " No %s found \n in %s \n or %s \n " ) % ( file_name , exec_dir , default_dir ) \
+ _ ( " Config file has been created at %s . \n " ) % config_path
print msg
logging . info ( msg )
2009-11-09 04:53:10 +01:00
except :
2010-08-29 20:46:01 +02:00
print _ ( " Error copying .example config file, cannot fall back. Exiting. \n " )
sys . stderr . write ( _ ( " Error copying .example config file, cannot fall back. Exiting. \n " ) )
2010-01-28 00:48:39 +01:00
sys . stderr . write ( str ( sys . exc_info ( ) ) )
2009-11-09 04:53:10 +01:00
sys . exit ( )
2010-08-29 13:46:22 +02:00
elif fallback :
2010-08-14 02:16:34 +02:00
print _ ( " No %s found, cannot fall back. Exiting. \n " ) % file_name
sys . stderr . write ( _ ( " No %s found, cannot fall back. Exiting. \n " ) % file_name )
2010-01-26 22:54:58 +01:00
sys . exit ( )
2010-08-29 13:46:22 +02:00
#print "get_config: returning "+str( (config_path,example_copy,example_path) )
return ( config_path , example_copy , example_path )
2009-11-09 04:53:10 +01:00
2010-02-11 00:33:50 +01:00
def get_logger ( file_name , config = " config " , fallback = False , log_dir = None , log_file = None ) :
2010-08-29 13:46:22 +02:00
( conf_file , copied , example_file ) = get_config ( file_name , fallback = fallback )
2010-02-08 23:43:15 +01:00
if log_dir is None :
2010-07-30 00:51:47 +02:00
log_dir = os . path . join ( get_exec_path ( ) , u ' log ' )
2010-02-08 23:43:15 +01:00
#print "\nget_logger: checking log_dir:", log_dir
check_dir ( log_dir )
2010-02-11 00:33:50 +01:00
if log_file is None :
file = os . path . join ( log_dir , ' fpdb-log.txt ' )
else :
file = os . path . join ( log_dir , log_file )
2010-02-08 23:43:15 +01:00
2010-01-31 12:25:24 +01:00
if conf_file :
2009-11-09 04:53:10 +01:00
try :
2010-01-31 12:25:24 +01:00
file = file . replace ( ' \\ ' , ' \\ \\ ' ) # replace each \ with \\
# print " ="+file+" "+ str(type(file))+" len="+str(len(file))+"\n"
logging . config . fileConfig ( conf_file , { " logFile " : file } )
2009-11-10 01:30:23 +01:00
log = logging . getLogger ( config )
log . debug ( " %s logger initialised " % config )
2009-11-09 04:53:10 +01:00
return log
except :
pass
2010-02-08 23:43:15 +01:00
log = logging . basicConfig ( filename = file , level = logging . INFO )
2009-11-09 04:53:10 +01:00
log = logging . getLogger ( )
2010-02-08 23:43:15 +01:00
# but it looks like default is no output :-( maybe because all the calls name a module?
2010-08-14 02:16:34 +02:00
log . debug ( _ ( " Default logger initialised for " ) + file )
print _ ( " Default logger intialised for " ) + file
2009-11-09 04:53:10 +01:00
return log
2010-01-31 12:25:24 +01:00
def check_dir ( path , create = True ) :
""" Check if a dir exists, optionally creates if not. """
if os . path . exists ( path ) :
if os . path . isdir ( path ) :
return path
else :
return False
if create :
2010-08-14 02:16:34 +02:00
msg = _ ( " Creating directory: ' %s ' " ) % ( path )
2010-01-31 12:25:24 +01:00
print msg
log . info ( msg )
2010-07-30 00:51:47 +02:00
os . mkdir ( path ) #, "utf-8"))
2010-01-31 12:25:24 +01:00
else :
return False
2009-10-21 19:24:46 +02:00
2009-11-02 10:06:54 +01:00
########################################################################
# application wide consts
APPLICATION_NAME_SHORT = ' fpdb '
APPLICATION_VERSION = ' xx.xx.xx '
2009-08-12 02:46:39 +02:00
2009-11-02 10:06:54 +01:00
DATABASE_TYPE_POSTGRESQL = ' postgresql '
DATABASE_TYPE_SQLITE = ' sqlite '
DATABASE_TYPE_MYSQL = ' mysql '
DATABASE_TYPES = (
DATABASE_TYPE_POSTGRESQL ,
DATABASE_TYPE_SQLITE ,
DATABASE_TYPE_MYSQL ,
)
2009-08-12 02:46:39 +02:00
2010-01-29 02:29:43 +01:00
#LOCALE_ENCODING = locale.getdefaultlocale()[1]
LOCALE_ENCODING = locale . getpreferredencoding ( )
if LOCALE_ENCODING == " US-ASCII " :
2010-08-14 02:16:34 +02:00
print _ ( " Default encoding set to US-ASCII, defaulting to CP1252 instead -- If you ' re not on a Mac, please report this problem. " )
2010-01-29 02:29:43 +01:00
LOCALE_ENCODING = " cp1252 "
2009-11-30 07:41:07 +01:00
2010-01-30 12:05:34 +01:00
# needs LOCALE_ENCODING (above), imported for sqlite setup in Config class below
2010-02-10 00:51:37 +01:00
FROZEN = hasattr ( sys , " frozen " )
EXEC_PATH = get_exec_path ( )
2010-01-30 12:05:34 +01:00
import Charset
2009-11-02 10:06:54 +01:00
########################################################################
2009-11-02 09:09:23 +01:00
def string_to_bool ( string , default = True ) :
""" converts a string representation of a boolean value to boolean True or False
@param string : ( str ) the string to convert
@param default : value to return if the string can not be converted to a boolean value
"""
string = string . lower ( )
if string in ( ' 1 ' , ' true ' , ' t ' ) :
2009-03-18 17:32:34 +01:00
return True
2009-11-02 09:09:23 +01:00
elif string in ( ' 0 ' , ' false ' , ' f ' ) :
2009-03-18 17:32:34 +01:00
return False
2009-03-21 16:38:17 +01:00
return default
2009-03-18 17:32:34 +01:00
2008-08-19 00:53:25 +02:00
class Layout :
2009-03-07 03:46:34 +01:00
def __init__ ( self , node ) :
2009-11-02 09:38:27 +01:00
self . max = int ( node . getAttribute ( ' max ' ) )
2009-03-07 03:46:34 +01:00
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 ' ) )
2009-11-22 06:00:23 +01:00
2008-08-19 00:53:25 +02:00
self . location = [ ]
2009-03-26 23:38:13 +01:00
self . location = map ( lambda x : None , range ( self . max + 1 ) ) # fill array with max seats+1 empty entries
2009-03-09 11:22:54 +01:00
2009-03-07 03:46:34 +01:00
for location_node in node . getElementsByTagName ( ' location ' ) :
if location_node . getAttribute ( ' seat ' ) != " " :
self . location [ int ( location_node . getAttribute ( ' seat ' ) ) ] = ( int ( location_node . getAttribute ( ' x ' ) ) , int ( location_node . getAttribute ( ' y ' ) ) )
elif location_node . getAttribute ( ' common ' ) != " " :
self . common = ( int ( location_node . getAttribute ( ' x ' ) ) , int ( location_node . getAttribute ( ' y ' ) ) )
2008-08-19 00:53:25 +02:00
def __str__ ( self ) :
2009-03-07 03:46:34 +01:00
temp = " Layout = %d max, width= %d , height = %d " % ( self . max , self . width , self . height )
if hasattr ( self , ' fav_seat ' ) : temp = temp + " , fav_seat = %d \n " % self . fav_seat
else : temp = temp + " \n "
if hasattr ( self , " common " ) :
temp = temp + " Common = ( %d , %d ) \n " % ( self . common [ 0 ] , self . common [ 1 ] )
2008-08-19 00:53:25 +02:00
temp = temp + " Locations = "
2009-03-09 11:40:38 +01:00
for i in range ( 1 , len ( self . location ) ) :
2008-08-19 00:53:25 +02:00
temp = temp + " ( %d , %d ) " % self . location [ i ]
2009-11-22 06:00:23 +01:00
2008-09-15 22:31:55 +02:00
return temp + " \n "
2008-08-19 00:53:25 +02:00
2010-08-22 01:30:33 +02:00
class Email :
def __init__ ( self , node ) :
self . node = node
self . host = node . getAttribute ( " host " )
self . username = node . getAttribute ( " username " )
self . password = node . getAttribute ( " password " )
self . useSsl = node . getAttribute ( " useSsl " )
self . folder = node . getAttribute ( " folder " )
self . fetchType = node . getAttribute ( " fetchType " )
def __str__ ( self ) :
return " fetchType= %s \n host = %s \n username = %s \n password = %s \n useSsl = %s \n folder = %s \n " \
% ( self . fetchType , self . host , self . username , self . password , self . useSsl , self . folder )
2008-08-19 00:53:25 +02:00
class Site :
def __init__ ( self , node ) :
2009-08-10 18:10:39 +02:00
def normalizePath ( path ) :
" Normalized existing pathes "
if os . path . exists ( path ) :
return os . path . abspath ( path )
return path
2009-11-22 06:00:23 +01:00
2008-08-19 00:53:25 +02:00
self . site_name = node . getAttribute ( " site_name " )
self . table_finder = node . getAttribute ( " table_finder " )
self . screen_name = node . getAttribute ( " screen_name " )
2009-08-10 18:10:39 +02:00
self . site_path = normalizePath ( node . getAttribute ( " site_path " ) )
2009-11-02 09:38:27 +01:00
self . HH_path = normalizePath ( node . getAttribute ( " HH_path " ) )
self . decoder = node . getAttribute ( " decoder " )
2008-10-27 12:12:04 +01:00
self . hudopacity = node . getAttribute ( " hudopacity " )
2008-10-27 11:29:39 +01:00
self . hudbgcolor = node . getAttribute ( " bgcolor " )
self . hudfgcolor = node . getAttribute ( " fgcolor " )
2008-11-04 10:40:03 +01:00
self . converter = node . getAttribute ( " converter " )
2008-11-16 23:53:31 +01:00
self . aux_window = node . getAttribute ( " aux_window " )
2009-11-02 09:38:27 +01:00
self . font = node . getAttribute ( " font " )
2008-12-18 03:57:05 +01:00
self . font_size = node . getAttribute ( " font_size " )
2009-06-20 15:43:05 +02:00
self . use_frames = node . getAttribute ( " use_frames " )
2009-11-02 09:38:27 +01:00
self . enabled = string_to_bool ( node . getAttribute ( " enabled " ) , default = True )
2009-06-20 15:43:05 +02:00
self . xpad = node . getAttribute ( " xpad " )
self . ypad = node . getAttribute ( " ypad " )
2010-06-11 22:03:53 +02:00
self . xshift = node . getAttribute ( " xshift " )
self . yshift = node . getAttribute ( " yshift " )
2008-08-19 00:53:25 +02:00
self . layout = { }
2010-08-22 01:30:33 +02:00
self . emails = { }
2009-11-04 07:44:25 +01:00
2010-08-14 02:16:34 +02:00
print _ ( " Loading site " ) , self . site_name
2009-03-07 03:46:34 +01:00
2008-08-19 00:53:25 +02:00
for layout_node in node . getElementsByTagName ( ' layout ' ) :
2009-03-07 03:46:34 +01:00
lo = Layout ( layout_node )
2008-08-19 00:53:25 +02:00
self . layout [ lo . max ] = lo
2010-08-22 01:30:33 +02:00
for email_node in node . getElementsByTagName ( ' email ' ) :
email = Email ( email_node )
self . emails [ email . fetchType ] = email
2008-08-19 00:53:25 +02:00
2009-06-20 15:43:05 +02:00
# Site defaults
2009-11-03 19:49:16 +01:00
self . xpad = 1 if self . xpad == " " else int ( self . xpad )
self . ypad = 0 if self . ypad == " " else int ( self . ypad )
2010-06-11 22:03:53 +02:00
self . xshift = 1 if self . xshift == " " else int ( self . xshift )
self . yshift = 0 if self . yshift == " " else int ( self . yshift )
2009-11-03 19:49:16 +01:00
self . font_size = 7 if self . font_size == " " else int ( self . font_size )
2009-11-03 20:43:12 +01:00
self . hudopacity = 1.0 if self . hudopacity == " " else float ( self . hudopacity )
2009-06-20 15:43:05 +02:00
if self . use_frames == " " : self . use_frames = False
2009-11-22 06:00:23 +01:00
if self . font == " " : self . font = " Sans "
2009-10-28 16:57:54 +01:00
if self . hudbgcolor == " " : self . hudbgcolor = " #000000 "
if self . hudfgcolor == " " : self . hudfgcolor = " #FFFFFF "
2009-06-20 15:43:05 +02:00
2008-08-19 00:53:25 +02:00
def __str__ ( self ) :
2009-03-09 11:40:38 +01:00
temp = " Site = " + self . site_name + " \n "
2008-08-19 00:53:25 +02:00
for key in dir ( self ) :
if key . startswith ( ' __ ' ) : continue
if key == ' layout ' : continue
value = getattr ( self , key )
if callable ( value ) : continue
2008-10-30 03:37:05 +01:00
temp = temp + ' ' + key + " = " + str ( value ) + " \n "
2009-11-22 06:00:23 +01:00
2008-08-19 00:53:25 +02:00
for layout in self . layout :
2009-03-09 11:40:38 +01:00
temp = temp + " %s " % self . layout [ layout ]
2009-11-22 06:00:23 +01:00
2008-08-19 00:53:25 +02:00
return temp
2009-11-22 06:00:23 +01:00
2008-08-19 00:53:25 +02:00
class Stat :
def __init__ ( self ) :
pass
2009-11-22 06:00:23 +01:00
2008-08-19 00:53:25 +02:00
def __str__ ( self ) :
2008-09-15 22:31:55 +02:00
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 )
2008-08-19 00:53:25 +02:00
return temp
2009-11-22 06:00:23 +01:00
2008-08-19 00:53:25 +02:00
class Game :
def __init__ ( self , node ) :
self . game_name = node . getAttribute ( " game_name " )
2009-11-02 09:38:27 +01:00
self . rows = int ( node . getAttribute ( " rows " ) )
self . cols = int ( node . getAttribute ( " cols " ) )
self . xpad = node . getAttribute ( " xpad " )
self . ypad = node . getAttribute ( " ypad " )
2010-06-11 22:03:53 +02:00
self . xshift = node . getAttribute ( " xshift " )
self . yshift = node . getAttribute ( " yshift " )
2009-06-20 15:43:05 +02:00
# Defaults
if self . xpad == " " : self . xpad = 1
else : self . xpad = int ( self . xpad )
if self . ypad == " " : self . ypad = 0
else : self . ypad = int ( self . ypad )
2010-06-11 22:03:53 +02:00
if self . xshift == " " : self . xshift = 1
else : self . xshift = int ( self . xshift )
if self . yshift == " " : self . yshift = 0
else : self . yshift = int ( self . yshift )
2009-03-05 02:04:23 +01:00
aux_text = node . getAttribute ( " aux " )
aux_list = aux_text . split ( ' , ' )
for i in range ( 0 , len ( aux_list ) ) :
aux_list [ i ] = aux_list [ i ] . strip ( )
self . aux = aux_list
2008-08-19 00:53:25 +02:00
2009-11-02 09:38:27 +01:00
self . stats = { }
2008-08-19 00:53:25 +02:00
for stat_node in node . getElementsByTagName ( ' stat ' ) :
stat = Stat ( )
stat . stat_name = stat_node . getAttribute ( " stat_name " )
2009-11-02 09:38:27 +01:00
stat . row = int ( stat_node . getAttribute ( " row " ) )
stat . col = int ( stat_node . getAttribute ( " col " ) )
stat . tip = stat_node . getAttribute ( " tip " )
stat . click = stat_node . getAttribute ( " click " )
stat . popup = stat_node . getAttribute ( " popup " )
2008-11-02 11:20:25 +01:00
stat . hudprefix = stat_node . getAttribute ( " hudprefix " )
stat . hudsuffix = stat_node . getAttribute ( " hudsuffix " )
2008-11-25 12:47:58 +01:00
stat . hudcolor = stat_node . getAttribute ( " hudcolor " )
2009-12-23 21:15:55 +01:00
stat . stat_loth = stat_node . getAttribute ( " stat_loth " )
stat . stat_hith = stat_node . getAttribute ( " stat_hith " )
stat . stat_locolor = stat_node . getAttribute ( " stat_locolor " )
stat . stat_hicolor = stat_node . getAttribute ( " stat_hicolor " )
2009-11-22 06:00:23 +01:00
2008-08-19 00:53:25 +02:00
self . stats [ stat . stat_name ] = stat
2009-11-22 06:00:23 +01:00
2008-08-19 00:53:25 +02:00
def __str__ ( self ) :
2009-03-09 11:40:38 +01:00
temp = " Game = " + self . game_name + " \n "
temp = temp + " rows = %d \n " % self . rows
temp = temp + " cols = %d \n " % self . cols
2009-06-20 15:43:05 +02:00
temp = temp + " xpad = %d \n " % self . xpad
temp = temp + " ypad = %d \n " % self . ypad
2010-06-11 22:03:53 +02:00
temp = temp + " xshift = %d \n " % self . xshift
temp = temp + " yshift = %d \n " % self . yshift
2009-03-09 11:40:38 +01:00
temp = temp + " aux = %s \n " % self . aux
2009-11-22 06:00:23 +01:00
2008-08-19 00:53:25 +02:00
for stat in self . stats . keys ( ) :
2009-03-09 11:40:38 +01:00
temp = temp + " %s " % self . stats [ stat ]
2009-11-22 06:00:23 +01:00
2008-08-19 00:53:25 +02:00
return temp
2009-11-22 06:00:23 +01:00
2008-08-19 00:53:25 +02:00
class Database :
def __init__ ( self , node ) :
self . db_name = node . getAttribute ( " db_name " )
2010-07-19 22:36:45 +02:00
self . db_desc = node . getAttribute ( " db_desc " )
2009-11-03 15:17:48 +01:00
self . db_server = node . getAttribute ( " db_server " ) . lower ( )
2009-11-02 09:38:27 +01:00
self . db_ip = node . getAttribute ( " db_ip " )
2008-08-19 00:53:25 +02:00
self . db_user = node . getAttribute ( " db_user " )
self . db_pass = node . getAttribute ( " db_pass " )
2009-11-02 09:16:18 +01:00
self . db_selected = string_to_bool ( node . getAttribute ( " default " ) , default = False )
2009-11-03 19:18:51 +01:00
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 } )
2009-11-22 06:00:23 +01:00
2008-08-19 00:53:25 +02:00
def __str__ ( self ) :
temp = ' Database = ' + self . db_name + ' \n '
for key in dir ( self ) :
if key . startswith ( ' __ ' ) : continue
value = getattr ( self , key )
if callable ( value ) : continue
2009-08-15 23:36:14 +02:00
temp = temp + ' ' + key + " = " + repr ( value ) + " \n "
2008-08-19 00:53:25 +02:00
return temp
2008-11-16 23:53:31 +01:00
class Aux_window :
2008-08-19 00:53:25 +02:00
def __init__ ( self , node ) :
2008-11-16 23:53:31 +01:00
for ( name , value ) in node . attributes . items ( ) :
setattr ( self , name , value )
2009-03-07 03:46:34 +01:00
self . layout = { }
for layout_node in node . getElementsByTagName ( ' layout ' ) :
lo = Layout ( layout_node )
self . layout [ lo . max ] = lo
2008-08-19 00:53:25 +02:00
def __str__ ( self ) :
2008-11-16 23:53:31 +01:00
temp = ' Aux = ' + self . name + " \n "
2008-08-19 00:53:25 +02:00
for key in dir ( self ) :
if key . startswith ( ' __ ' ) : continue
2009-03-07 03:46:34 +01:00
if key == ' layout ' : continue
2008-08-19 00:53:25 +02:00
value = getattr ( self , key )
if callable ( value ) : continue
temp = temp + ' ' + key + " = " + value + " \n "
2009-03-07 03:46:34 +01:00
for layout in self . layout :
temp = temp + " %s " % self . layout [ layout ]
2008-08-19 00:53:25 +02:00
return temp
2009-03-24 14:58:45 +01:00
class HHC :
def __init__ ( self , node ) :
2009-11-02 09:38:27 +01:00
self . site = node . getAttribute ( " site " )
2009-03-24 14:58:45 +01:00
self . converter = node . getAttribute ( " converter " )
def __str__ ( self ) :
return " %s : \t %s " % ( self . site , self . converter )
2008-09-15 22:31:55 +02:00
class Popup :
def __init__ ( self , node ) :
self . name = node . getAttribute ( " pu_name " )
2009-11-02 09:38:27 +01:00
self . pu_stats = [ ]
2008-09-15 22:31:55 +02:00
for stat_node in node . getElementsByTagName ( ' pu_stat ' ) :
self . pu_stats . append ( stat_node . getAttribute ( " pu_stat_name " ) )
2009-11-22 06:00:23 +01:00
2008-09-15 22:31:55 +02:00
def __str__ ( self ) :
temp = " Popup = " + self . name + " \n "
for stat in self . pu_stats :
temp = temp + " " + stat
return temp + " \n "
2008-10-21 16:22:01 +02:00
class Import :
def __init__ ( self , node ) :
2009-09-10 03:21:27 +02:00
self . node = node
2009-11-02 09:38:27 +01:00
self . interval = node . getAttribute ( " interval " )
2008-11-08 14:01:07 +01:00
self . callFpdbHud = node . getAttribute ( " callFpdbHud " )
2009-02-19 13:15:12 +01:00
self . hhArchiveBase = node . getAttribute ( " hhArchiveBase " )
2010-03-21 13:19:55 +01:00
self . hhBulkPath = node . getAttribute ( " hhBulkPath " )
2010-09-22 08:57:51 +02:00
self . saveActions = string_to_bool ( node . getAttribute ( " saveActions " ) , default = False )
2009-11-02 09:14:57 +01:00
self . fastStoreHudCache = string_to_bool ( node . getAttribute ( " fastStoreHudCache " ) , default = False )
2010-02-20 17:55:52 +01:00
self . saveStarsHH = string_to_bool ( node . getAttribute ( " saveStarsHH " ) , default = False )
2008-10-21 16:22:01 +02:00
def __str__ ( self ) :
2009-03-15 20:54:22 +01:00
return " interval = %s \n callFpdbHud = %s \n hhArchiveBase = %s \n saveActions = %s \n fastStoreHudCache = %s \n " \
2009-11-02 09:38:27 +01:00
% ( self . interval , self . callFpdbHud , self . hhArchiveBase , self . saveActions , self . fastStoreHudCache )
2008-10-21 16:22:01 +02:00
2009-10-03 20:05:41 +02:00
class HudUI :
def __init__ ( self , node ) :
self . node = node
self . label = node . getAttribute ( ' label ' )
2009-10-14 15:04:09 +02:00
#
2009-11-17 22:47:43 +01:00
self . hud_style = node . getAttribute ( ' stat_range ' )
self . hud_days = node . getAttribute ( ' stat_days ' )
2009-11-02 09:09:23 +01:00
self . aggregate_ring = string_to_bool ( node . getAttribute ( ' aggregate_ring_game_stats ' ) )
self . aggregate_tour = string_to_bool ( node . getAttribute ( ' aggregate_tourney_stats ' ) )
2009-10-14 15:04:09 +02:00
self . agg_bb_mult = node . getAttribute ( ' aggregation_level_multiplier ' )
#
2009-11-17 22:47:43 +01:00
self . h_hud_style = node . getAttribute ( ' hero_stat_range ' )
self . h_hud_days = node . getAttribute ( ' hero_stat_days ' )
self . h_aggregate_ring = string_to_bool ( node . getAttribute ( ' aggregate_hero_ring_game_stats ' ) )
self . h_aggregate_tour = string_to_bool ( node . getAttribute ( ' aggregate_hero_tourney_stats ' ) )
2009-11-02 09:38:27 +01:00
self . h_agg_bb_mult = node . getAttribute ( ' hero_aggregation_level_multiplier ' )
2009-10-14 15:04:09 +02:00
2009-10-03 20:05:41 +02:00
def __str__ ( self ) :
return " label = %s \n " % self . label
2010-03-15 22:48:25 +01:00
class General ( dict ) :
def __init__ ( self ) :
super ( General , self ) . __init__ ( )
def add_elements ( self , node ) :
# day_start - number n where 0.0 <= n < 24.0 representing start of day for user
# e.g. user could set to 4.0 for day to start at 4am local time
2010-03-21 13:19:55 +01:00
# [ HH_bulk_path was here - now moved to import section ]
2010-03-15 22:48:25 +01:00
for ( name , value ) in node . attributes . items ( ) :
2010-08-14 02:16:34 +02:00
log . debug ( _ ( " config.general: adding %s = %s " ) % ( name , value ) )
2010-03-15 22:48:25 +01:00
self [ name ] = value
2010-08-23 03:56:07 +02:00
try :
self [ " version " ] = int ( self [ " version " ] )
except KeyError :
self [ " version " ] = 0
self [ " ui_language " ] = " system "
2010-08-26 20:08:34 +02:00
self [ " config_difficulty " ] = " expert "
2010-08-23 03:56:07 +02:00
def get_defaults ( self ) :
self [ " version " ] = 0
self [ " ui_language " ] = " system "
2010-08-26 20:08:34 +02:00
self [ " config_difficulty " ] = " expert "
2010-08-23 03:56:07 +02:00
self [ " config_wrap_len " ] = " -1 "
self [ " day_start " ] = " 5 "
2010-03-15 22:48:25 +01:00
def __str__ ( self ) :
s = " "
for k in self :
s = s + " %s = %s \n " % ( k , self [ k ] )
return ( s )
2010-07-31 21:55:29 +02:00
class GUICashStats ( list ) :
""" <gui_cash_stats>
< col col_name = " game " col_title = " Game " disp_all = " True " disp_posn = " True " field_format = " %s " field_type = " str " xalignment = " 0.0 " / >
. . .
< / gui_cash_stats >
"""
def __init__ ( self ) :
super ( GUICashStats , self ) . __init__ ( )
def add_elements ( self , node ) :
# is this needed?
for child in node . childNodes :
if child . nodeType == child . ELEMENT_NODE :
col_name , col_title , disp_all , disp_posn , field_format , field_type , xalignment = None , None , True , True , " %s " , " str " , 0.0
if child . hasAttribute ( ' col_name ' ) : col_name = child . getAttribute ( ' col_name ' )
if child . hasAttribute ( ' col_title ' ) : col_title = child . getAttribute ( ' col_title ' )
if child . hasAttribute ( ' disp_all ' ) : disp_all = string_to_bool ( child . getAttribute ( ' disp_all ' ) )
if child . hasAttribute ( ' disp_posn ' ) : disp_posn = string_to_bool ( child . getAttribute ( ' disp_posn ' ) )
if child . hasAttribute ( ' field_format ' ) : field_format = child . getAttribute ( ' field_format ' )
if child . hasAttribute ( ' field_type ' ) : field_type = child . getAttribute ( ' field_type ' )
try :
if child . hasAttribute ( ' xalignment ' ) : xalignment = float ( child . getAttribute ( ' xalignment ' ) )
except ValueError :
2010-08-14 02:16:34 +02:00
print _ ( " bad number in xalignment was ignored " )
log . info ( _ ( " bad number in xalignment was ignored " ) )
2010-07-31 21:55:29 +02:00
self . append ( [ col_name , col_title , disp_all , disp_posn , field_format , field_type , xalignment ] )
2010-08-04 11:28:38 +02:00
def get_defaults ( self ) :
2010-08-12 16:52:12 +02:00
""" A list of defaults to be called, should there be no entry in config """
defaults = [ [ u ' game ' , u ' Game ' , True , True , u ' %s ' , u ' str ' , 0.0 ] ,
2010-08-04 11:28:38 +02:00
[ u ' hand ' , u ' Hand ' , False , False , u ' %s ' , u ' str ' , 0.0 ] ,
[ u ' plposition ' , u ' Posn ' , False , False , u ' %s ' , u ' str ' , 1.0 ] ,
[ u ' pname ' , u ' Name ' , False , False , u ' %s ' , u ' str ' , 0.0 ] ,
[ u ' n ' , u ' Hds ' , True , True , u ' %1.0f ' , u ' str ' , 1.0 ] ,
[ u ' avgseats ' , u ' Seats ' , False , False , u ' %3.1f ' , u ' str ' , 1.0 ] ,
[ u ' vpip ' , u ' VPIP ' , True , True , u ' %3.1f ' , u ' str ' , 1.0 ] ,
[ u ' pfr ' , u ' PFR ' , True , True , u ' %3.1f ' , u ' str ' , 1.0 ] ,
[ u ' pf3 ' , u ' PF3 ' , True , True , u ' %3.1f ' , u ' str ' , 1.0 ] ,
[ u ' aggfac ' , u ' AggFac ' , True , True , u ' %2.2f ' , u ' str ' , 1.0 ] ,
[ u ' aggfrq ' , u ' AggFreq ' , True , True , u ' %3.1f ' , u ' str ' , 1.0 ] ,
[ u ' conbet ' , u ' ContBet ' , True , True , u ' %3.1f ' , u ' str ' , 1.0 ] ,
[ u ' rfi ' , u ' RFI ' , True , True , u ' %3.1f ' , u ' str ' , 1.0 ] ,
[ u ' steals ' , u ' Steals ' , True , True , u ' %3.1f ' , u ' str ' , 1.0 ] ,
[ u ' saw_f ' , u ' Saw_F ' , True , True , u ' %3.1f ' , u ' str ' , 1.0 ] ,
[ u ' sawsd ' , u ' SawSD ' , True , True , u ' %3.1f ' , u ' str ' , 1.0 ] ,
[ u ' wtsdwsf ' , u ' WtSDwsF ' , True , True , u ' %3.1f ' , u ' str ' , 1.0 ] ,
[ u ' wmsd ' , u ' W$SD ' , True , True , u ' %3.1f ' , u ' str ' , 1.0 ] ,
[ u ' flafq ' , u ' FlAFq ' , True , True , u ' %3.1f ' , u ' str ' , 1.0 ] ,
[ u ' tuafq ' , u ' TuAFq ' , True , True , u ' %3.1f ' , u ' str ' , 1.0 ] ,
[ u ' rvafq ' , u ' RvAFq ' , True , True , u ' %3.1f ' , u ' str ' , 1.0 ] ,
[ u ' pofafq ' , u ' PoFAFq ' , False , False , u ' %3.1f ' , u ' str ' , 1.0 ] ,
[ u ' net ' , u ' Net($) ' , True , True , u ' %6.2f ' , u ' cash ' , 1.0 ] ,
[ u ' bbper100 ' , u ' bb/100 ' , True , True , u ' %4.2f ' , u ' str ' , 1.0 ] ,
[ u ' rake ' , u ' Rake($) ' , True , True , u ' %6.2f ' , u ' cash ' , 1.0 ] ,
[ u ' bb100xr ' , u ' bbxr/100 ' , True , True , u ' %4.2f ' , u ' str ' , 1.0 ] ,
[ u ' variance ' , u ' Variance ' , True , True , u ' %5.2f ' , u ' str ' , 1.0 ]
2010-08-12 16:52:12 +02:00
]
for col in defaults :
self . append ( col )
2010-08-04 11:28:38 +02:00
2010-07-31 21:55:29 +02:00
# def __str__(self):
# s = ""
# for l in self:
# s = s + " %s = %s\n" % (k, self[k])
# return(s)
2010-08-22 04:36:23 +02:00
class RawHands :
def __init__ ( self , node = None ) :
if node == None :
self . save = " error "
self . compression = " none "
print _ ( " missing config section raw_hands " )
else :
save = node . getAttribute ( " save " )
if save in ( " none " , " error " , " all " ) :
self . save = save
else :
print _ ( " Invalid config value for raw_hands.save, defaulting to \" error \" " )
self . save = " error "
compression = node . getAttribute ( " compression " )
if save in ( " none " , " gzip " , " bzip2 " ) :
self . compression = compression
else :
print _ ( " Invalid config value for raw_hands.compression, defaulting to \" none \" " )
self . compression = " none "
#end def __init__
def __str__ ( self ) :
return " save= %s , compression= %s \n " % ( self . save , self . compression )
#end class RawHands
class RawTourneys :
def __init__ ( self , node = None ) :
if node == None :
self . save = " error "
self . compression = " none "
print _ ( " missing config section raw_tourneys " )
else :
save = node . getAttribute ( " save " )
if save in ( " none " , " error " , " all " ) :
self . save = save
else :
print _ ( " Invalid config value for raw_tourneys.save, defaulting to \" error \" " )
self . save = " error "
compression = node . getAttribute ( " compression " )
if save in ( " none " , " gzip " , " bzip2 " ) :
self . compression = compression
else :
print _ ( " Invalid config value for raw_tourneys.compression, defaulting to \" none \" " )
self . compression = " none "
#end def __init__
def __str__ ( self ) :
return " save= %s , compression= %s \n " % ( self . save , self . compression )
#end class RawTourneys
2008-08-19 00:53:25 +02:00
class Config :
2009-08-12 02:46:39 +02:00
def __init__ ( self , file = None , dbname = ' ' ) :
2008-09-27 01:21:38 +02:00
# "file" is a path to an xml file with the fpdb/HUD configuration
# we check the existence of "file" and try to recover if it doesn't exist
2009-11-09 04:53:10 +01:00
# self.default_config_path = self.get_default_config_path()
2010-01-31 13:24:32 +01:00
self . example_copy = False
2009-11-03 20:30:52 +01:00
if file is not None : # config file path passed in
2009-06-15 05:14:53 +02:00
file = os . path . expanduser ( file )
2008-09-27 01:21:38 +02:00
if not os . path . exists ( file ) :
2010-08-14 02:16:34 +02:00
print _ ( " Configuration file %s not found. Using defaults. " ) % ( file )
sys . stderr . write ( _ ( " Configuration file %s not found. Using defaults. " ) % ( file ) )
2008-09-27 01:21:38 +02:00
file = None
2010-09-01 22:20:22 +02:00
self . example_copy , example_file = True , None
2010-08-29 13:46:22 +02:00
if file is None : ( file , self . example_copy , example_file ) = get_config ( " HUD_config.xml " , True )
2008-11-03 04:58:55 +01:00
2010-01-31 12:25:24 +01:00
self . file = file
self . dir_self = get_exec_path ( )
2010-02-26 02:28:41 +01:00
# self.dir_config = os.path.dirname(self.file)
self . dir_config = get_default_config_path ( )
2010-07-30 00:51:47 +02:00
self . dir_log = os . path . join ( self . dir_config , u ' log ' )
self . dir_database = os . path . join ( self . dir_config , u ' database ' )
self . log_file = os . path . join ( self . dir_log , u ' fpdb-log.txt ' )
2010-01-31 12:25:24 +01:00
log = get_logger ( " logging.conf " , " config " , log_dir = self . dir_log )
2008-08-19 00:53:25 +02:00
self . supported_sites = { }
self . supported_games = { }
2009-11-02 08:52:46 +01:00
self . supported_databases = { } # databaseName --> Database instance
2008-11-16 23:53:31 +01:00
self . aux_windows = { }
2009-03-24 14:58:45 +01:00
self . hhcs = { }
2008-09-15 22:31:55 +02:00
self . popup_windows = { }
2010-08-31 23:34:48 +02:00
self . db_selected = None # database the user would like to use
2010-03-15 22:48:25 +01:00
self . general = General ( )
2010-08-10 03:28:57 +02:00
self . emails = { }
2010-07-31 21:55:29 +02:00
self . gui_cash_stats = GUICashStats ( )
2010-08-31 23:34:48 +02:00
self . site_ids = { } # site ID list from the database
2009-11-05 00:34:02 +01:00
2010-08-29 13:46:22 +02:00
added , n = 1 , 0 # use n to prevent infinite loop if add_missing_elements() fails somehow
while added > 0 and n < 2 :
n = n + 1
log . info ( _ ( " Reading configuration file %s " ) % file )
print _ ( " \n Reading configuration file %s \n " ) % file
try :
doc = xml . dom . minidom . parse ( file )
self . doc = doc
self . file_error = None
except :
log . error ( _ ( " Error parsing %s . See error log file. " ) % ( file ) )
traceback . print_exc ( file = sys . stderr )
self . file_error = sys . exc_info ( ) [ 1 ]
# we could add a parameter to decide whether to return or read a line and exit?
return
#print "press enter to continue"
#sys.stdin.readline()
#sys.exit()
#ExpatError: not well-formed (invalid token): line 511, column 4
#sys.exc_info = (<class 'xml.parsers.expat.ExpatError'>, ExpatError('not well-formed (invalid token): line 511,
# column 4',), <traceback object at 0x024503A0>)
if not self . example_copy and example_file is not None :
# reads example file and adds missing elements into current config
added = self . add_missing_elements ( doc , example_file )
2010-08-23 03:56:07 +02:00
if doc . getElementsByTagName ( " general " ) == [ ] :
self . general . get_defaults ( )
2010-03-15 22:48:25 +01:00
for gen_node in doc . getElementsByTagName ( " general " ) :
self . general . add_elements ( node = gen_node ) # add/overwrite elements in self.general
2008-08-19 00:53:25 +02:00
2010-08-12 16:52:12 +02:00
if doc . getElementsByTagName ( " gui_cash_stats " ) == [ ] :
self . gui_cash_stats . get_defaults ( )
2010-07-31 21:55:29 +02:00
for gcs_node in doc . getElementsByTagName ( " gui_cash_stats " ) :
self . gui_cash_stats . add_elements ( node = gcs_node ) # add/overwrite elements in self.gui_cash_stats
2008-08-19 00:53:25 +02:00
# s_sites = doc.getElementsByTagName("supported_sites")
for site_node in doc . getElementsByTagName ( " site " ) :
site = Site ( node = site_node )
self . supported_sites [ site . site_name ] = site
2008-10-21 16:22:01 +02:00
# s_games = doc.getElementsByTagName("supported_games")
2008-08-19 00:53:25 +02:00
for game_node in doc . getElementsByTagName ( " game " ) :
game = Game ( node = game_node )
self . supported_games [ game . game_name ] = game
2009-11-22 06:00:23 +01:00
2009-11-02 09:29:58 +01:00
# parse databases defined by user in the <supported_databases> section
2009-11-22 06:00:23 +01:00
# the user may select the actual database to use via commandline or by setting the selected="bool"
2009-11-02 09:29:58 +01:00
# attribute of the tag. if no database is explicitely selected, we use the first one we come across
2008-10-21 16:22:01 +02:00
# s_dbs = doc.getElementsByTagName("supported_databases")
2009-11-02 08:40:34 +01:00
#TODO: do we want to take all <database> tags or all <database> tags contained in <supported_databases>
2009-11-02 09:38:27 +01:00
# ..this may break stuff for some users. so leave it unchanged for now untill there is a decission
2008-08-19 00:53:25 +02:00
for db_node in doc . getElementsByTagName ( " database " ) :
2009-11-02 08:48:07 +01:00
db = Database ( node = db_node )
if db . db_name in self . supported_databases :
2009-11-02 09:40:16 +01:00
raise ValueError ( " Database names must be unique " )
2009-11-02 09:29:58 +01:00
if self . db_selected is None or db . db_selected :
2009-11-02 08:48:07 +01:00
self . db_selected = db . db_name
2010-08-26 22:24:40 +02:00
db_node . setAttribute ( " default " , " True " )
2009-11-02 08:48:07 +01:00
self . supported_databases [ db . db_name ] = db
2009-11-22 06:00:23 +01:00
#TODO: if the user may passes '' (empty string) as database name via command line, his choice is ignored
2009-11-02 09:55:08 +01:00
# ..when we parse the xml we allow for ''. there has to be a decission if to allow '' or not
2009-09-04 23:12:35 +02:00
if dbname and dbname in self . supported_databases :
self . db_selected = dbname
2009-11-02 09:45:53 +01:00
#NOTE: fpdb can not handle the case when no database is defined in xml, so we throw an exception for now
if self . db_selected is None :
raise ValueError ( ' There must be at least one database defined ' )
2009-09-04 23:12:35 +02:00
2009-11-02 09:38:27 +01:00
# s_dbs = doc.getElementsByTagName("mucked_windows")
2008-11-16 23:53:31 +01:00
for aw_node in doc . getElementsByTagName ( " aw " ) :
aw = Aux_window ( node = aw_node )
self . aux_windows [ aw . name ] = aw
2008-08-19 00:53:25 +02:00
2009-11-02 09:38:27 +01:00
# s_dbs = doc.getElementsByTagName("mucked_windows")
2009-03-24 14:58:45 +01:00
for hhc_node in doc . getElementsByTagName ( " hhc " ) :
hhc = HHC ( node = hhc_node )
self . hhcs [ hhc . site ] = hhc
2008-10-21 16:22:01 +02:00
# s_dbs = doc.getElementsByTagName("popup_windows")
2008-09-15 22:31:55 +02:00
for pu_node in doc . getElementsByTagName ( " pu " ) :
pu = Popup ( node = pu_node )
self . popup_windows [ pu . name ] = pu
2008-10-21 16:22:01 +02:00
for imp_node in doc . getElementsByTagName ( " import " ) :
imp = Import ( node = imp_node )
self . imp = imp
2009-10-03 20:05:41 +02:00
for hui_node in doc . getElementsByTagName ( ' hud_ui ' ) :
hui = HudUI ( node = hui_node )
self . ui = hui
2009-03-27 15:49:22 +01:00
db = self . get_db_parameters ( )
2008-11-03 04:58:55 +01:00
if db [ ' db-password ' ] == ' YOUR MYSQL PASSWORD ' :
df_file = self . find_default_conf ( )
2009-11-03 19:49:16 +01:00
if df_file is None : # this is bad
2008-11-03 04:58:55 +01:00
pass
else :
df_parms = self . read_default_conf ( df_file )
2008-11-05 11:49:05 +01:00
self . set_db_parameters ( db_name = ' fpdb ' , db_ip = df_parms [ ' db-host ' ] ,
2009-11-02 09:38:27 +01:00
db_user = df_parms [ ' db-user ' ] ,
db_pass = df_parms [ ' db-password ' ] )
2010-08-29 13:46:22 +02:00
self . save ( file = os . path . join ( self . dir_config , " HUD_config.xml " ) )
2010-08-22 04:36:23 +02:00
if doc . getElementsByTagName ( " raw_hands " ) == [ ] :
self . raw_hands = RawHands ( )
for raw_hands_node in doc . getElementsByTagName ( ' raw_hands ' ) :
self . raw_hands = RawHands ( raw_hands_node )
if doc . getElementsByTagName ( " raw_tourneys " ) == [ ] :
self . raw_tourneys = RawTourneys ( )
for raw_tourneys_node in doc . getElementsByTagName ( ' raw_tourneys ' ) :
self . raw_tourneys = RawTourneys ( raw_tourneys_node )
2009-10-04 18:46:04 +02:00
print " "
2010-08-22 04:36:23 +02:00
#end def __init__
2009-10-04 18:46:04 +02:00
2010-08-29 13:46:22 +02:00
def add_missing_elements ( self , doc , example_file ) :
""" Look through example config file and add any elements that are not in the config
May need to add some ' enabled ' attributes to turn things off - can ' t just delete a
config section now because this will add it back in """
nodes_added = 0
try :
example_doc = xml . dom . minidom . parse ( example_file )
except :
log . error ( _ ( " Error parsing example file %s . See error log file. " ) % ( example_file ) )
return nodes_added
for cnode in doc . getElementsByTagName ( " FreePokerToolsConfig " ) :
for example_cnode in example_doc . childNodes :
if example_cnode . localName == " FreePokerToolsConfig " :
for e in example_cnode . childNodes :
#print "nodetype", e.nodeType, "name", e.localName, "found", len(doc.getElementsByTagName(e.localName))
if e . nodeType == e . ELEMENT_NODE and doc . getElementsByTagName ( e . localName ) == [ ] :
new = doc . importNode ( e , True ) # True means do deep copy
t_node = self . doc . createTextNode ( " " )
cnode . appendChild ( t_node )
cnode . appendChild ( new )
t_node = self . doc . createTextNode ( " \r \n \r \n " )
cnode . appendChild ( t_node )
print " ... adding missing config section: " + e . localName
nodes_added = nodes_added + 1
if nodes_added > 0 :
print " Added %d missing config sections \n " % nodes_added
self . save ( )
return nodes_added
2009-09-10 03:21:27 +02:00
def set_hhArchiveBase ( self , path ) :
self . imp . node . setAttribute ( " hhArchiveBase " , path )
2009-11-22 06:00:23 +01:00
2008-11-03 04:58:55 +01:00
def find_default_conf ( self ) :
if os . name == ' posix ' :
config_path = os . path . join ( os . path . expanduser ( " ~ " ) , ' .fpdb ' , ' default.conf ' )
elif os . name == ' nt ' :
config_path = os . path . join ( os . environ [ " APPDATA " ] , ' fpdb ' , ' default.conf ' )
else : config_path = False
if config_path and os . path . exists ( config_path ) :
file = config_path
else :
file = None
return file
2009-11-24 20:46:57 +01:00
def get_doc ( self ) :
return self . doc
2008-09-15 22:31:55 +02:00
def get_site_node ( self , site ) :
for site_node in self . doc . getElementsByTagName ( " site " ) :
if site_node . getAttribute ( " site_name " ) == site :
return site_node
2010-08-10 04:44:35 +02:00
def getEmailNode ( self , siteName , fetchType ) :
2010-08-22 01:30:33 +02:00
siteNode = self . get_site_node ( siteName )
for emailNode in siteNode . getElementsByTagName ( " email " ) :
if emailNode . getAttribute ( " fetchType " ) == fetchType :
print " found emailNode "
2010-08-10 04:44:35 +02:00
return emailNode
2010-08-22 01:30:33 +02:00
break
2010-08-10 04:44:35 +02:00
#end def getEmailNode
2010-07-17 00:23:48 +02:00
def getGameNode ( self , gameName ) :
""" returns DOM game node for a given game """
for gameNode in self . doc . getElementsByTagName ( " game " ) :
#print "getGameNode gameNode:",gameNode
if gameNode . getAttribute ( " game_name " ) == gameName :
return gameNode
#end def getGameNode
2009-03-11 19:12:56 +01:00
def get_aux_node ( self , aux ) :
for aux_node in self . doc . getElementsByTagName ( " aw " ) :
if aux_node . getAttribute ( " name " ) == aux :
return aux_node
2008-11-05 11:49:05 +01:00
def get_db_node ( self , db_name ) :
for db_node in self . doc . getElementsByTagName ( " database " ) :
if db_node . getAttribute ( " db_name " ) == db_name :
return db_node
return None
2008-09-15 22:31:55 +02:00
def get_layout_node ( self , site_node , layout ) :
for layout_node in site_node . getElementsByTagName ( " layout " ) :
2009-11-22 06:00:23 +01:00
if layout_node . getAttribute ( " max " ) is None :
2008-10-04 22:43:50 +02:00
return None
2008-09-15 22:31:55 +02:00
if int ( layout_node . getAttribute ( " max " ) ) == int ( layout ) :
return layout_node
def get_location_node ( self , layout_node , seat ) :
2009-03-15 20:27:47 +01:00
if seat == " common " :
for location_node in layout_node . getElementsByTagName ( " location " ) :
if location_node . hasAttribute ( " common " ) :
return location_node
else :
for location_node in layout_node . getElementsByTagName ( " location " ) :
if int ( location_node . getAttribute ( " seat " ) ) == int ( seat ) :
return location_node
2008-09-15 22:31:55 +02:00
def save ( self , file = None ) :
2009-11-24 20:46:57 +01:00
if file is None :
file = self . file
2010-02-10 00:51:37 +01:00
try :
shutil . move ( file , file + " .backup " )
except :
pass
2009-11-05 00:39:42 +01:00
with open ( file , ' w ' ) as f :
2010-06-09 23:46:17 +02:00
#self.doc.writexml(f)
f . write ( self . wrap_long_lines ( self . doc . toxml ( ) ) )
def wrap_long_lines ( self , s ) :
lines = [ self . wrap_long_line ( l ) for l in s . splitlines ( ) ]
return ( ' \n ' . join ( lines ) + ' \n ' )
def wrap_long_line ( self , l ) :
if ' config_wrap_len ' in self . general :
wrap_len = int ( self . general [ ' config_wrap_len ' ] )
else :
wrap_len = - 1 # < 0 means no wrap
if wrap_len > = 0 and len ( l ) > wrap_len :
m = re . compile ( ' \ s+ \ S+ \ s+ ' )
mo = m . match ( l )
if mo :
indent_len = mo . end ( )
#print "indent = %s (%s)" % (indent_len, l[0:indent_len])
indent = ' \n ' + ' ' * indent_len
m = re . compile ( ' ( \ S+= " [^ " ]+ " \ s+) ' )
parts = [ x for x in m . split ( l [ indent_len : ] ) if x ]
if len ( parts ) > 1 :
#print "parts =", parts
l = l [ 0 : indent_len ] + indent . join ( parts )
return ( l )
else :
return ( l )
2008-09-15 22:31:55 +02:00
2010-08-10 04:44:35 +02:00
def editEmail ( self , siteName , fetchType , newEmail ) :
emailNode = self . getEmailNode ( siteName , fetchType )
emailNode . setAttribute ( " host " , newEmail . host )
emailNode . setAttribute ( " username " , newEmail . username )
emailNode . setAttribute ( " password " , newEmail . password )
emailNode . setAttribute ( " folder " , newEmail . folder )
emailNode . setAttribute ( " useSsl " , newEmail . useSsl )
#end def editEmail
2008-09-15 22:31:55 +02:00
def edit_layout ( self , site_name , max , width = None , height = None ,
fav_seat = None , locations = None ) :
site_node = self . get_site_node ( site_name )
layout_node = self . get_layout_node ( site_node , max )
2009-11-01 06:54:59 +01:00
# TODO: how do we support inserting new layouts?
2009-11-03 20:30:52 +01:00
if layout_node is None :
2009-11-01 06:54:59 +01:00
return
2009-03-09 11:40:38 +01:00
for i in range ( 1 , max + 1 ) :
2008-09-15 22:31:55 +02:00
location_node = self . get_location_node ( layout_node , i )
location_node . setAttribute ( " x " , str ( locations [ i - 1 ] [ 0 ] ) )
location_node . setAttribute ( " y " , str ( locations [ i - 1 ] [ 1 ] ) )
self . supported_sites [ site_name ] . layout [ max ] . location [ i ] = ( locations [ i - 1 ] [ 0 ] , locations [ i - 1 ] [ 1 ] )
2010-07-17 00:23:48 +02:00
def editStats ( self , gameName , statArray ) :
""" replaces stat selection for the given gameName with the given statArray """
gameNode = self . getGameNode ( gameName )
statNodes = gameNode . getElementsByTagName ( " stat " )
for node in statNodes :
gameNode . removeChild ( node )
gameNode . setAttribute ( " rows " , str ( len ( statArray ) ) )
gameNode . setAttribute ( " cols " , str ( len ( statArray [ 0 ] ) ) )
for rowNumber in range ( len ( statArray ) ) :
for columnNumber in range ( len ( statArray [ rowNumber ] ) ) :
newStat = self . doc . createElement ( " stat " )
newAttrStatName = self . doc . createAttribute ( " stat_name " )
newStat . setAttributeNode ( newAttrStatName )
newStat . setAttribute ( " stat_name " , statArray [ rowNumber ] [ columnNumber ] )
newAttrStatName = self . doc . createAttribute ( " row " )
newStat . setAttributeNode ( newAttrStatName )
newStat . setAttribute ( " row " , str ( rowNumber ) )
newAttrStatName = self . doc . createAttribute ( " col " )
newStat . setAttributeNode ( newAttrStatName )
newStat . setAttribute ( " col " , str ( columnNumber ) )
newAttrStatName = self . doc . createAttribute ( " click " )
newStat . setAttributeNode ( newAttrStatName )
newStat . setAttribute ( " click " , " tog_decorate " )
newAttrStatName = self . doc . createAttribute ( " popup " )
newStat . setAttributeNode ( newAttrStatName )
newStat . setAttribute ( " popup " , " default " )
newAttrStatName = self . doc . createAttribute ( " tip " )
newStat . setAttributeNode ( newAttrStatName )
newStat . setAttribute ( " tip " , " tip1 " )
gameNode . appendChild ( newStat )
statNodes = gameNode . getElementsByTagName ( " stat " )
#end def editStats
2009-03-11 19:12:56 +01:00
def edit_aux_layout ( self , aux_name , max , width = None , height = None , locations = None ) :
aux_node = self . get_aux_node ( aux_name )
layout_node = self . get_layout_node ( aux_node , max )
2009-11-03 20:30:52 +01:00
if layout_node is None :
2009-03-15 20:27:47 +01:00
print " aux node not found "
return
print " editing locations = " , locations
for ( i , pos ) in locations . iteritems ( ) :
2009-03-11 19:12:56 +01:00
location_node = self . get_location_node ( layout_node , i )
2009-03-15 20:27:47 +01:00
location_node . setAttribute ( " x " , str ( locations [ i ] [ 0 ] ) )
location_node . setAttribute ( " y " , str ( locations [ i ] [ 1 ] ) )
if i == " common " :
self . aux_windows [ aux_name ] . layout [ max ] . common = ( locations [ i ] [ 0 ] , locations [ i ] [ 1 ] )
else :
self . aux_windows [ aux_name ] . layout [ max ] . location [ i ] = ( locations [ i ] [ 0 ] , locations [ i ] [ 1 ] )
2009-03-11 19:12:56 +01:00
2009-11-02 10:29:10 +01:00
#NOTE: we got a nice Database class, so why map it again here?
# user input validation should be done when initializing the Database class. this allows to give appropriate feddback when something goes wrong
# try ..except is evil here. it swallows all kinds of errors. dont do this
# naming database types 2, 3, 4 on the fly is no good idea. i see this all over the code. better use some globally defined consts (see DATABASE_TYPE_*)
# i would like to drop this method entirely and replace it by get_selected_database() or better get_active_database(), returning one of our Database instances
# thus we can drop self.db_selected (holding database name) entirely and replace it with self._active_database = Database, avoiding to define the same
# thing multiple times
2009-03-27 15:49:22 +01:00
def get_db_parameters ( self ) :
2008-10-05 02:22:53 +02:00
db = { }
2009-08-12 02:46:39 +02:00
name = self . db_selected
2009-09-16 06:30:59 +02:00
# TODO: What's up with all the exception handling here?!
2009-03-17 04:34:00 +01:00
try : db [ ' db-databaseName ' ] = name
except : pass
2010-07-19 22:36:45 +02:00
try : db [ ' db-desc ' ] = self . supported_databases [ name ] . db_desc
except : pass
2009-03-17 04:34:00 +01:00
try : db [ ' db-host ' ] = self . supported_databases [ name ] . db_ip
except : pass
try : db [ ' db-user ' ] = self . supported_databases [ name ] . db_user
except : pass
try : db [ ' db-password ' ] = self . supported_databases [ name ] . db_pass
except : pass
try : db [ ' db-server ' ] = self . supported_databases [ name ] . db_server
except : pass
2010-02-20 19:59:49 +01:00
db [ ' db-backend ' ] = self . get_backend ( self . supported_databases [ name ] . db_server )
2008-10-05 02:22:53 +02:00
return db
2008-11-05 11:49:05 +01:00
def set_db_parameters ( self , db_name = ' fpdb ' , db_ip = None , db_user = None ,
2010-07-19 22:36:45 +02:00
db_pass = None , db_desc = None , db_server = None ,
default = " False " ) :
2008-11-05 11:49:05 +01:00
db_node = self . get_db_node ( db_name )
2010-07-19 22:36:45 +02:00
default = default . lower ( )
defaultb = string_to_bool ( default , False )
2009-03-26 19:57:40 +01:00
if db_node != None :
2010-07-19 22:36:45 +02:00
if db_desc is not None : db_node . setAttribute ( " db_desc " , db_desc )
2009-11-03 20:30:52 +01:00
if db_ip is not None : db_node . setAttribute ( " db_ip " , db_ip )
if db_user is not None : db_node . setAttribute ( " db_user " , db_user )
if db_pass is not None : db_node . setAttribute ( " db_pass " , db_pass )
if db_server is not None : db_node . setAttribute ( " db_server " , db_server )
2010-08-26 22:24:40 +02:00
if defaultb or self . db_selected == db_name :
2010-08-28 19:11:35 +02:00
db_node . setAttribute ( " default " , " True " )
for dbn in self . doc . getElementsByTagName ( " database " ) :
if dbn . getAttribute ( ' db_name ' ) != db_name and dbn . hasAttribute ( " default " ) :
dbn . removeAttribute ( " default " )
elif db_node . hasAttribute ( " default " ) :
db_node . removeAttribute ( " default " )
if self . supported_databases . has_key ( db_name ) :
if db_desc is not None : self . supported_databases [ db_name ] . dp_desc = db_desc
if db_ip is not None : self . supported_databases [ db_name ] . dp_ip = db_ip
if db_user is not None : self . supported_databases [ db_name ] . dp_user = db_user
if db_pass is not None : self . supported_databases [ db_name ] . dp_pass = db_pass
if db_server is not None : self . supported_databases [ db_name ] . dp_server = db_server
self . supported_databases [ db_name ] . db_selected = defaultb
if defaultb :
self . db_selected = db_name
return
def add_db_parameters ( self , db_name = ' fpdb ' , db_ip = None , db_user = None ,
db_pass = None , db_desc = None , db_server = None ,
default = " False " ) :
default = default . lower ( )
defaultb = string_to_bool ( default , False )
if db_name in self . supported_databases :
raise ValueError ( " Database names must be unique " )
db_node = self . get_db_node ( db_name )
if db_node is None :
for db_node in self . doc . getElementsByTagName ( " supported_databases " ) :
# should only be one supported_databases element, use last one if there are several
suppdb_node = db_node
t_node = self . doc . createTextNode ( " " )
suppdb_node . appendChild ( t_node )
db_node = self . doc . createElement ( " database " )
suppdb_node . appendChild ( db_node )
t_node = self . doc . createTextNode ( " \r \n " )
suppdb_node . appendChild ( t_node )
db_node . setAttribute ( " db_name " , db_name )
if db_desc is not None : db_node . setAttribute ( " db_desc " , db_desc )
if db_ip is not None : db_node . setAttribute ( " db_ip " , db_ip )
if db_user is not None : db_node . setAttribute ( " db_user " , db_user )
if db_pass is not None : db_node . setAttribute ( " db_pass " , db_pass )
if db_server is not None : db_node . setAttribute ( " db_server " , db_server )
if defaultb :
db_node . setAttribute ( " default " , " True " )
for dbn in self . doc . getElementsByTagName ( " database " ) :
if dbn . getAttribute ( ' db_name ' ) != db_name and dbn . hasAttribute ( " default " ) :
dbn . removeAttribute ( " default " )
elif db_node . hasAttribute ( " default " ) :
db_node . removeAttribute ( " default " )
else :
if db_desc is not None : db_node . setAttribute ( " db_desc " , db_desc )
if db_ip is not None : db_node . setAttribute ( " db_ip " , db_ip )
if db_user is not None : db_node . setAttribute ( " db_user " , db_user )
if db_pass is not None : db_node . setAttribute ( " db_pass " , db_pass )
if db_server is not None : db_node . setAttribute ( " db_server " , db_server )
if defaultb or self . db_selected == db_name :
db_node . setAttribute ( " default " , " True " )
2010-07-19 22:36:45 +02:00
elif db_node . hasAttribute ( " default " ) :
2010-08-26 22:24:40 +02:00
db_node . removeAttribute ( " default " )
2010-08-28 19:11:35 +02:00
2008-11-05 11:49:05 +01:00
if self . supported_databases . has_key ( db_name ) :
2010-07-19 22:36:45 +02:00
if db_desc is not None : self . supported_databases [ db_name ] . dp_desc = db_desc
2009-11-03 20:30:52 +01:00
if db_ip is not None : self . supported_databases [ db_name ] . dp_ip = db_ip
if db_user is not None : self . supported_databases [ db_name ] . dp_user = db_user
if db_pass is not None : self . supported_databases [ db_name ] . dp_pass = db_pass
if db_server is not None : self . supported_databases [ db_name ] . dp_server = db_server
2010-07-19 22:36:45 +02:00
self . supported_databases [ db_name ] . db_selected = defaultb
2010-08-28 19:11:35 +02:00
else :
db = Database ( node = db_node )
self . supported_databases [ db . db_name ] = db
2010-07-19 22:36:45 +02:00
if defaultb :
self . db_selected = db_name
2008-11-05 11:49:05 +01:00
return
2010-02-20 19:59:49 +01:00
def get_backend ( self , name ) :
""" Returns the number of the currently used backend """
if name == DATABASE_TYPE_MYSQL :
ret = 2
elif name == DATABASE_TYPE_POSTGRESQL :
ret = 3
elif name == DATABASE_TYPE_SQLITE :
ret = 4
# sqlcoder: this assignment fixes unicode problems for me with sqlite (windows, cp1252)
# feel free to remove or improve this if you understand the problems
# better than me (not hard!)
Charset . not_needed1 , Charset . not_needed2 , Charset . not_needed3 = True , True , True
else :
raise ValueError ( ' Unsupported database backend: %s ' % self . supported_databases [ name ] . db_server )
return ret
2009-11-22 06:00:23 +01:00
2009-08-08 10:07:30 +02:00
def getDefaultSite ( self ) :
" Returns first enabled site or None "
for site_name , site in self . supported_sites . iteritems ( ) :
if site . enabled :
return site_name
return None
2008-11-05 11:49:05 +01:00
2009-10-03 20:05:41 +02:00
# Allow to change the menu appearance
def get_hud_ui_parameters ( self ) :
hui = { }
2009-10-14 15:04:09 +02:00
2009-10-03 20:05:41 +02:00
default_text = ' FPDB Menu - Right click \n Left-Drag to Move '
try :
hui [ ' label ' ] = self . ui . label
2009-11-02 09:38:27 +01:00
if self . ui . label == ' ' : # Empty menu label is a big no-no
2009-10-03 20:05:41 +02:00
hui [ ' label ' ] = default_text
except :
hui [ ' label ' ] = default_text
2009-10-14 15:04:09 +02:00
2009-11-26 22:28:05 +01:00
try : hui [ ' hud_style ' ] = self . ui . hud_style
except : hui [ ' hud_style ' ] = ' A ' # default is show stats for All-time, also S(session) and T(ime)
try : hui [ ' hud_days ' ] = int ( self . ui . hud_days )
except : hui [ ' hud_days ' ] = 90
2009-10-14 15:04:09 +02:00
try : hui [ ' aggregate_ring ' ] = self . ui . aggregate_ring
except : hui [ ' aggregate_ring ' ] = False
try : hui [ ' aggregate_tour ' ] = self . ui . aggregate_tour
except : hui [ ' aggregate_tour ' ] = True
2009-11-02 09:38:27 +01:00
try : hui [ ' agg_bb_mult ' ] = self . ui . agg_bb_mult
except : hui [ ' agg_bb_mult ' ] = 1
2009-10-14 15:04:09 +02:00
2009-11-26 22:28:05 +01:00
try : hui [ ' seats_style ' ] = self . ui . seats_style
2009-11-29 19:14:04 +01:00
except : hui [ ' seats_style ' ] = ' A ' # A / C / E, use A(ll) / C(ustom) / E(xact) seat numbers
2009-10-14 15:04:09 +02:00
2009-11-26 22:28:05 +01:00
try : hui [ ' seats_cust_nums ' ] = self . ui . seats_cust_nums
except : hui [ ' seats_cust_nums ' ] = [ ' n/a ' , ' n/a ' , ( 2 , 2 ) , ( 3 , 4 ) , ( 3 , 5 ) , ( 4 , 6 ) , ( 5 , 7 ) , ( 6 , 8 ) , ( 7 , 9 ) , ( 8 , 10 ) , ( 8 , 10 ) ]
2009-10-14 15:04:09 +02:00
2009-11-26 22:28:05 +01:00
# Hero specific
2009-10-14 15:04:09 +02:00
2009-11-02 09:38:27 +01:00
try : hui [ ' h_hud_style ' ] = self . ui . h_hud_style
except : hui [ ' h_hud_style ' ] = ' S '
2009-10-14 15:04:09 +02:00
2009-11-02 09:38:27 +01:00
try : hui [ ' h_hud_days ' ] = int ( self . ui . h_hud_days )
except : hui [ ' h_hud_days ' ] = 30
2009-10-14 15:04:09 +02:00
2009-11-26 22:28:05 +01:00
try : hui [ ' h_aggregate_ring ' ] = self . ui . h_aggregate_ring
except : hui [ ' h_aggregate_ring ' ] = False
try : hui [ ' h_aggregate_tour ' ] = self . ui . h_aggregate_tour
except : hui [ ' h_aggregate_tour ' ] = True
2009-10-14 15:04:09 +02:00
try : hui [ ' h_agg_bb_mult ' ] = self . ui . h_agg_bb_mult
except : hui [ ' h_agg_bb_mult ' ] = 1
2009-11-26 22:28:05 +01:00
try : hui [ ' h_seats_style ' ] = self . ui . h_seats_style
2009-11-29 19:14:04 +01:00
except : hui [ ' h_seats_style ' ] = ' A ' # A / C / E, use A(ll) / C(ustom) / E(xact) seat numbers
2009-11-26 22:28:05 +01:00
try : hui [ ' h_seats_cust_nums ' ] = self . ui . h_seats_cust_nums
except : hui [ ' h_seats_cust_nums ' ] = [ ' n/a ' , ' n/a ' , ( 2 , 2 ) , ( 3 , 4 ) , ( 3 , 5 ) , ( 4 , 6 ) , ( 5 , 7 ) , ( 6 , 8 ) , ( 7 , 9 ) , ( 8 , 10 ) , ( 8 , 10 ) ]
2009-10-03 20:05:41 +02:00
return hui
2009-11-22 06:00:23 +01:00
2008-10-21 16:22:01 +02:00
def get_import_parameters ( self ) :
imp = { }
2009-11-02 09:38:27 +01:00
try : imp [ ' callFpdbHud ' ] = self . imp . callFpdbHud
except : imp [ ' callFpdbHud ' ] = True
2009-03-17 04:34:00 +01:00
2009-11-02 09:38:27 +01:00
try : imp [ ' interval ' ] = self . imp . interval
except : imp [ ' interval ' ] = 10
2009-03-17 04:34:00 +01:00
2010-03-21 13:19:55 +01:00
# hhArchiveBase is the temp store for part-processed hand histories - should be redundant eventually
2009-11-02 09:38:27 +01:00
try : imp [ ' hhArchiveBase ' ] = self . imp . hhArchiveBase
except : imp [ ' hhArchiveBase ' ] = " ~/.fpdb/HandHistories/ "
2009-03-17 04:34:00 +01:00
2010-03-21 13:19:55 +01:00
# hhBulkPath is the default location for bulk imports (if set)
try : imp [ ' hhBulkPath ' ] = self . imp . hhBulkPath
except : imp [ ' hhBulkPath ' ] = " "
2009-11-02 09:38:27 +01:00
try : imp [ ' saveActions ' ] = self . imp . saveActions
2010-09-22 08:57:51 +02:00
except : imp [ ' saveActions ' ] = False
2009-03-17 04:34:00 +01:00
2010-02-20 17:55:52 +01:00
try : imp [ ' saveStarsHH ' ] = self . imp . saveStarsHH
except : imp [ ' saveStarsHH ' ] = False
2009-11-02 09:38:27 +01:00
try : imp [ ' fastStoreHudCache ' ] = self . imp . fastStoreHudCache
2009-03-17 04:34:00 +01:00
except : imp [ ' fastStoreHudCache ' ] = True
2010-02-20 17:55:52 +01:00
2008-10-21 16:22:01 +02:00
return imp
2009-08-08 10:07:30 +02:00
def get_default_paths ( self , site = None ) :
if site is None : site = self . getDefaultSite ( )
2008-10-21 16:22:01 +02:00
paths = { }
try :
2009-08-08 10:07:30 +02:00
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
2010-03-21 13:19:55 +01:00
if self . imp . hhBulkPath :
paths [ ' bulkImport-defaultPath ' ] = self . imp . hhBulkPath
2009-11-22 06:00:23 +01:00
except AssertionError :
2009-08-16 11:22:22 +02:00
paths [ ' hud-defaultPath ' ] = paths [ ' bulkImport-defaultPath ' ] = " ** ERROR DEFAULT PATH IN CONFIG DOES NOT EXIST ** "
2008-10-21 16:22:01 +02:00
return paths
2009-11-22 06:00:23 +01:00
2009-02-25 21:25:58 +01:00
def get_frames ( self , site = " PokerStars " ) :
2009-08-06 05:57:26 +02:00
if site not in self . supported_sites : return False
2009-03-29 16:08:33 +02:00
return self . supported_sites [ site ] . use_frames == True
2008-10-21 16:22:01 +02:00
2008-10-30 03:37:05 +01:00
def get_default_colors ( self , site = " PokerStars " ) :
colors = { }
2009-08-06 05:57:26 +02:00
if site not in self . supported_sites or self . supported_sites [ site ] . hudopacity == " " :
2009-03-09 11:40:38 +01:00
colors [ ' hudopacity ' ] = 0.90
else :
colors [ ' hudopacity ' ] = float ( self . supported_sites [ site ] . hudopacity )
2009-08-06 05:57:26 +02:00
if site not in self . supported_sites or self . supported_sites [ site ] . hudbgcolor == " " :
2009-03-09 11:40:38 +01:00
colors [ ' hudbgcolor ' ] = " #FFFFFF "
else :
colors [ ' hudbgcolor ' ] = self . supported_sites [ site ] . hudbgcolor
2009-08-06 05:57:26 +02:00
if site not in self . supported_sites or self . supported_sites [ site ] . hudfgcolor == " " :
2009-03-09 11:40:38 +01:00
colors [ ' hudfgcolor ' ] = " #000000 "
else :
colors [ ' hudfgcolor ' ] = self . supported_sites [ site ] . hudfgcolor
2008-10-30 03:37:05 +01:00
return colors
2009-11-22 06:00:23 +01:00
2009-11-04 23:41:09 +01:00
def get_default_font ( self , site = ' PokerStars ' ) :
font = " Sans "
font_size = " 8 "
site = self . supported_sites . get ( site , None )
if site is not None :
if site . font :
font = site . font
if site . font_size :
font_size = site . font_size
return font , font_size
2008-10-30 03:37:05 +01:00
2009-11-05 01:21:32 +01:00
def get_locations ( self , site_name = " PokerStars " , max = 8 ) :
site = self . supported_sites . get ( site_name , None )
if site is not None :
location = site . layout . get ( max , None )
if location is not None :
return location . location
return (
2009-11-22 06:00:23 +01:00
( 0 , 0 ) , ( 684 , 61 ) , ( 689 , 239 ) , ( 692 , 346 ) ,
2009-11-05 01:21:32 +01:00
( 586 , 393 ) , ( 421 , 440 ) , ( 267 , 440 ) , ( 0 , 361 ) ,
2009-11-22 06:00:23 +01:00
( 0 , 280 ) , ( 121 , 280 ) , ( 46 , 30 )
2009-11-05 01:21:32 +01:00
)
2009-11-22 06:00:23 +01:00
2009-03-07 23:02:51 +01:00
def get_aux_locations ( self , aux = " mucked " , max = " 9 " ) :
2009-11-22 06:00:23 +01:00
2009-03-07 23:02:51 +01:00
try :
locations = self . aux_windows [ aux ] . layout [ max ] . location
except :
2009-11-22 06:00:23 +01:00
locations = ( ( 0 , 0 ) , ( 684 , 61 ) , ( 689 , 239 ) , ( 692 , 346 ) ,
2009-11-02 09:38:27 +01:00
( 586 , 393 ) , ( 421 , 440 ) , ( 267 , 440 ) , ( 0 , 361 ) ,
( 0 , 280 ) , ( 121 , 280 ) , ( 46 , 30 ) )
2009-03-07 23:02:51 +01:00
return locations
2009-11-05 00:11:43 +01:00
def get_supported_sites ( self , all = False ) :
2008-11-11 15:40:31 +01:00
""" Returns the list of supported sites. """
2009-11-05 00:11:43 +01:00
if all :
return self . supported_sites . keys ( )
else :
return [ site_name for ( site_name , site ) in self . supported_sites . items ( ) if site . enabled ]
2009-11-22 06:00:23 +01:00
2008-11-05 22:52:47 +01:00
def get_site_parameters ( self , site ) :
""" Returns a dict of the site parameters for the specified site """
parms = { }
parms [ " converter " ] = self . supported_sites [ site ] . converter
2009-11-02 09:38:27 +01:00
parms [ " decoder " ] = self . supported_sites [ site ] . decoder
2008-11-05 22:52:47 +01:00
parms [ " hudbgcolor " ] = self . supported_sites [ site ] . hudbgcolor
parms [ " hudfgcolor " ] = self . supported_sites [ site ] . hudfgcolor
parms [ " hudopacity " ] = self . supported_sites [ site ] . hudopacity
parms [ " screen_name " ] = self . supported_sites [ site ] . screen_name
parms [ " site_path " ] = self . supported_sites [ site ] . site_path
parms [ " table_finder " ] = self . supported_sites [ site ] . table_finder
2009-11-02 09:38:27 +01:00
parms [ " HH_path " ] = self . supported_sites [ site ] . HH_path
2008-11-11 15:40:31 +01:00
parms [ " site_name " ] = self . supported_sites [ site ] . site_name
2008-11-16 23:53:31 +01:00
parms [ " aux_window " ] = self . supported_sites [ site ] . aux_window
2009-11-02 09:38:27 +01:00
parms [ " font " ] = self . supported_sites [ site ] . font
2008-12-18 03:57:05 +01:00
parms [ " font_size " ] = self . supported_sites [ site ] . font_size
2009-11-02 09:38:27 +01:00
parms [ " enabled " ] = self . supported_sites [ site ] . enabled
parms [ " xpad " ] = self . supported_sites [ site ] . xpad
parms [ " ypad " ] = self . supported_sites [ site ] . ypad
2010-06-11 22:03:53 +02:00
parms [ " xshift " ] = self . supported_sites [ site ] . xshift
parms [ " yshift " ] = self . supported_sites [ site ] . yshift
2008-11-05 22:52:47 +01:00
return parms
def set_site_parameters ( self , site_name , converter = None , decoder = None ,
2009-11-22 06:00:23 +01:00
hudbgcolor = None , hudfgcolor = None ,
2008-11-05 22:52:47 +01:00
hudopacity = None , screen_name = None ,
site_path = None , table_finder = None ,
2008-12-18 03:57:05 +01:00
HH_path = None , enabled = None ,
font = None , font_size = None ) :
2008-11-05 22:52:47 +01:00
""" Sets the specified site parameters for the specified site. """
site_node = self . get_site_node ( site_name )
2009-11-03 20:30:52 +01:00
if db_node is not None :
if converter is not None : site_node . setAttribute ( " converter " , converter )
if decoder is not None : site_node . setAttribute ( " decoder " , decoder )
if hudbgcolor is not None : site_node . setAttribute ( " hudbgcolor " , hudbgcolor )
if hudfgcolor is not None : site_node . setAttribute ( " hudfgcolor " , hudfgcolor )
if hudopacity is not None : site_node . setAttribute ( " hudopacity " , hudopacity )
if screen_name is not None : site_node . setAttribute ( " screen_name " , screen_name )
if site_path is not None : site_node . setAttribute ( " site_path " , site_path )
if table_finder is not None : site_node . setAttribute ( " table_finder " , table_finder )
if HH_path is not None : site_node . setAttribute ( " HH_path " , HH_path )
if enabled is not None : site_node . setAttribute ( " enabled " , enabled )
if font is not None : site_node . setAttribute ( " font " , font )
if font_size is not None : site_node . setAttribute ( " font_size " , font_size )
2008-11-05 22:52:47 +01:00
return
2010-08-31 23:34:48 +02:00
def set_site_ids ( self , sites ) :
self . site_ids = dict ( sites )
def get_site_id ( self , site ) :
return ( self . site_ids [ site ] )
2008-11-16 23:53:31 +01:00
def get_aux_windows ( self ) :
""" Gets the list of mucked window formats in the configuration. """
2009-11-04 23:58:48 +01:00
return self . aux_windows . keys ( )
2008-11-16 23:53:31 +01:00
def get_aux_parameters ( self , name ) :
""" Gets a dict of mucked window parameters from the named mw. """
param = { }
2009-03-09 11:40:38 +01:00
if self . aux_windows . has_key ( name ) :
2008-11-16 23:53:31 +01:00
for key in dir ( self . aux_windows [ name ] ) :
if key . startswith ( ' __ ' ) : continue
value = getattr ( self . aux_windows [ name ] , key )
if callable ( value ) : continue
param [ key ] = value
return param
return None
2009-11-22 06:00:23 +01:00
2008-11-16 23:53:31 +01:00
def get_game_parameters ( self , name ) :
""" Get the configuration parameters for the named game. """
param = { }
2009-03-09 11:40:38 +01:00
if self . supported_games . has_key ( name ) :
2008-11-16 23:53:31 +01:00
param [ ' game_name ' ] = self . supported_games [ name ] . game_name
2009-11-02 09:38:27 +01:00
param [ ' rows ' ] = self . supported_games [ name ] . rows
param [ ' cols ' ] = self . supported_games [ name ] . cols
param [ ' xpad ' ] = self . supported_games [ name ] . xpad
param [ ' ypad ' ] = self . supported_games [ name ] . ypad
2010-06-11 22:03:53 +02:00
param [ ' xshift ' ] = self . supported_games [ name ] . xshift
param [ ' yshift ' ] = self . supported_games [ name ] . yshift
2009-11-02 09:38:27 +01:00
param [ ' aux ' ] = self . supported_games [ name ] . aux
2008-11-16 23:53:31 +01:00
return param
def get_supported_games ( self ) :
""" Get the list of supported games. """
sg = [ ]
2010-07-15 03:32:10 +02:00
for game in self . supported_games . keys ( ) :
sg . append ( self . supported_games [ game ] . game_name )
2008-11-16 23:53:31 +01:00
return sg
2008-12-13 23:37:23 +01:00
def execution_path ( self , filename ) :
""" Join the fpdb path to filename. """
2009-03-05 02:12:15 +01:00
return os . path . join ( os . path . dirname ( inspect . getfile ( sys . _getframe ( 0 ) ) ) , filename )
2008-12-13 23:37:23 +01:00
2010-03-15 22:48:25 +01:00
def get_general_params ( self ) :
return ( self . general )
2010-07-31 21:55:29 +02:00
def get_gui_cash_stat_params ( self ) :
return ( self . gui_cash_stats )
2008-08-19 00:53:25 +02:00
if __name__ == " __main__ " :
c = Config ( )
2009-11-22 06:00:23 +01:00
2008-08-19 00:53:25 +02:00
print " \n ----------- SUPPORTED SITES ----------- "
for s in c . supported_sites . keys ( ) :
print c . supported_sites [ s ]
print " ----------- END SUPPORTED SITES ----------- "
print " \n ----------- SUPPORTED GAMES ----------- "
for game in c . supported_games . keys ( ) :
print c . supported_games [ game ]
print " ----------- END SUPPORTED GAMES ----------- "
print " \n ----------- SUPPORTED DATABASES ----------- "
for db in c . supported_databases . keys ( ) :
print c . supported_databases [ db ]
print " ----------- END SUPPORTED DATABASES ----------- "
2008-11-16 23:53:31 +01:00
print " \n ----------- AUX WINDOW FORMATS ----------- "
for w in c . aux_windows . keys ( ) :
print c . aux_windows [ w ]
print " ----------- END AUX WINDOW FORMATS ----------- "
2009-03-24 14:58:45 +01:00
print " \n ----------- HAND HISTORY CONVERTERS ----------- "
for w in c . hhcs . keys ( ) :
print c . hhcs [ w ]
print " ----------- END HAND HISTORY CONVERTERS ----------- "
2009-11-22 06:00:23 +01:00
2008-09-15 22:31:55 +02:00
print " \n ----------- POPUP WINDOW FORMATS ----------- "
for w in c . popup_windows . keys ( ) :
print c . popup_windows [ w ]
2008-11-16 23:53:31 +01:00
print " ----------- END POPUP WINDOW FORMATS ----------- "
2008-09-15 22:31:55 +02:00
2008-10-21 16:22:01 +02:00
print " \n ----------- IMPORT ----------- "
2008-12-03 18:01:37 +01:00
print c . imp
2008-10-21 16:22:01 +02:00
print " ----------- END IMPORT ----------- "
2008-09-15 22:31:55 +02:00
c . edit_layout ( " PokerStars " , 6 , locations = ( ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) , ( 4 , 4 ) , ( 5 , 5 ) , ( 6 , 6 ) ) )
2008-10-05 02:22:53 +02:00
c . save ( file = " testout.xml " )
2009-11-22 06:00:23 +01:00
2009-11-02 09:38:27 +01:00
print " db = " , c . get_db_parameters ( )
2008-10-30 03:37:05 +01:00
# print "imp = ", c.get_import_parameters()
print " paths = " , c . get_default_paths ( " PokerStars " )
print " colors = " , c . get_default_colors ( " PokerStars " )
print " locs = " , c . get_locations ( " PokerStars " , 8 )
2008-11-16 23:53:31 +01:00
for mw in c . get_aux_windows ( ) :
print c . get_aux_parameters ( mw )
2009-03-07 23:02:51 +01:00
print " mucked locations = " , c . get_aux_locations ( ' mucked ' , 9 )
2009-11-22 06:00:23 +01:00
# c.edit_aux_layout('mucked', 9, locations = [(487, 113), (555, 469), (572, 276), (522, 345),
2009-03-15 20:54:22 +01:00
# (333, 354), (217, 341), (150, 273), (150, 169), (230, 115)])
# print "mucked locations =", c.get_aux_locations('mucked', 9)
2009-03-11 19:12:56 +01:00
2008-11-05 22:52:47 +01:00
for site in c . supported_sites . keys ( ) :
print " site = " , site ,
2008-11-06 06:39:49 +01:00
print c . get_site_parameters ( site )
2008-12-18 03:57:05 +01:00
print c . get_default_font ( site )
2008-11-16 23:53:31 +01:00
for game in c . get_supported_games ( ) :
print c . get_game_parameters ( game )
2008-12-13 23:37:23 +01:00
2009-11-28 22:11:52 +01:00
for hud_param , value in c . get_hud_ui_parameters ( ) . iteritems ( ) :
print " hud param %s = %s " % ( hud_param , value )
2009-02-19 13:15:12 +01:00
print " start up path = " , c . execution_path ( " " )
2009-11-11 22:20:43 +01:00
2010-07-31 21:55:29 +02:00
print " gui_cash_stats = " , c . gui_cash_stats
2009-11-28 22:11:52 +01:00
try :
from xml . dom . ext import PrettyPrint
for site_node in c . doc . getElementsByTagName ( " site " ) :
PrettyPrint ( site_node , stream = sys . stdout , encoding = " utf-8 " )
except :
print " xml.dom.ext needs PyXML to be installed! "
2010-02-10 00:51:37 +01:00
print " FROZEN = " , FROZEN
print " EXEC_PATH = " , EXEC_PATH
print " press enter to end "
sys . stdin . readline ( )