Merge branch 'master' of git://git.assembla.com/fpdb-sql

This commit is contained in:
Eratosthenes 2010-02-01 18:38:23 -05:00
commit 8c81761c1b
10 changed files with 86 additions and 50 deletions

7
pyfpdb/Configuration.py Normal file → Executable file
View File

@ -38,6 +38,10 @@ from xml.dom.minidom import Node
import logging, logging.config import logging, logging.config
import ConfigParser import ConfigParser
# logging has been set up in fpdb.py or HUD_main.py, use their settings:
log = logging.getLogger("config")
############################################################################## ##############################################################################
# Functions for finding config files and setting up logging # Functions for finding config files and setting up logging
# Also used in other modules that use logging. # Also used in other modules that use logging.
@ -138,8 +142,6 @@ def check_dir(path, create = True):
else: else:
return False return False
# find a logging.conf file and set up logging
log = get_logger("logging.conf", "config")
######################################################################## ########################################################################
# application wide consts # application wide consts
@ -458,6 +460,7 @@ class Config:
self.dir_config = os.path.dirname(self.file) self.dir_config = os.path.dirname(self.file)
self.dir_log = os.path.join(self.dir_config, 'log') self.dir_log = os.path.join(self.dir_config, 'log')
self.dir_database = os.path.join(self.dir_config, 'database') self.dir_database = os.path.join(self.dir_config, 'database')
self.log_file = os.path.join(self.dir_log, 'logging.out')
log = get_logger("logging.conf", "config", log_dir=self.dir_log) log = get_logger("logging.conf", "config", log_dir=self.dir_log)
# Parse even if there was no real config file found and we are using the example # Parse even if there was no real config file found and we are using the example

View File

@ -41,6 +41,10 @@ import Queue
import codecs import codecs
import math import math
import logging
# logging has been set up in fpdb.py or HUD_main.py, use their settings:
log = logging.getLogger("db")
# pyGTK modules # pyGTK modules
@ -52,7 +56,6 @@ import Tourney
import Charset import Charset
from Exceptions import * from Exceptions import *
import Configuration import Configuration
log = Configuration.get_logger("logging.conf","db")
# Other library modules # Other library modules
@ -224,8 +227,8 @@ class Database:
def __init__(self, c, sql = None): def __init__(self, c, sql = None):
log = Configuration.get_logger("logging.conf", "db", log_dir=c.dir_log) #log = Configuration.get_logger("logging.conf", "db", log_dir=c.dir_log)
log.info("Creating Database instance, sql = %s" % sql) log.debug("Creating Database instance, sql = %s" % sql)
self.config = c self.config = c
self.__connected = False self.__connected = False
self.settings = {} self.settings = {}
@ -236,6 +239,7 @@ class Database:
self.db_server = db_params['db-server'] self.db_server = db_params['db-server']
self.database = db_params['db-databaseName'] self.database = db_params['db-databaseName']
self.host = db_params['db-host'] self.host = db_params['db-host']
self.db_path = ''
# where possible avoid creating new SQL instance by using the global one passed in # where possible avoid creating new SQL instance by using the global one passed in
if sql is None: if sql is None:
@ -385,9 +389,9 @@ class Database:
log.info("Creating directory: '%s'" % (self.config.dir_database)) log.info("Creating directory: '%s'" % (self.config.dir_database))
os.mkdir(self.config.dir_database) os.mkdir(self.config.dir_database)
database = os.path.join(self.config.dir_database, database) database = os.path.join(self.config.dir_database, database)
log.info("Connecting to SQLite: %(database)s" % {'database':database}) self.db_path = database
print "Connecting to SQLite: %(database)s" % {'database':database} log.info("Connecting to SQLite: %(database)s" % {'database':self.db_path})
self.connection = sqlite3.connect(database, detect_types=sqlite3.PARSE_DECLTYPES ) self.connection = sqlite3.connect(self.db_path, detect_types=sqlite3.PARSE_DECLTYPES )
sqlite3.register_converter("bool", lambda x: bool(int(x))) sqlite3.register_converter("bool", lambda x: bool(int(x)))
sqlite3.register_adapter(bool, lambda x: "1" if x else "0") sqlite3.register_adapter(bool, lambda x: "1" if x else "0")
self.connection.create_function("floor", 1, math.floor) self.connection.create_function("floor", 1, math.floor)
@ -788,11 +792,10 @@ class Database:
def get_player_id(self, config, site, player_name): def get_player_id(self, config, site, player_name):
c = self.connection.cursor() c = self.connection.cursor()
print "get_player_id: player_name =", player_name, type(player_name) #print "get_player_id: player_name =", player_name, type(player_name)
p_name = Charset.to_utf8(player_name) p_name = Charset.to_utf8(player_name)
c.execute(self.sql.query['get_player_id'], (p_name, site)) c.execute(self.sql.query['get_player_id'], (p_name, site))
row = c.fetchone() row = c.fetchone()
print "player id =", row
if row: if row:
return row[0] return row[0]
else: else:

View File

@ -26,16 +26,21 @@ from time import *
import gobject import gobject
#import pokereval #import pokereval
import logging
# logging has been set up in fpdb.py or HUD_main.py, use their settings:
log = logging.getLogger("filter")
import Configuration import Configuration
import Database import Database
import SQL import SQL
import Charset import Charset
class Filters(threading.Thread): class Filters(threading.Thread):
def __init__(self, db, config, qdict, display = {}, debug=True): def __init__(self, db, config, qdict, display = {}, debug=True):
# config and qdict are now redundant # config and qdict are now redundant
self.debug = debug self.debug = debug
#print "start of GraphViewer constructor"
self.db = db self.db = db
self.cursor = db.cursor self.cursor = db.cursor
self.sql = db.sql self.sql = db.sql
@ -268,10 +273,10 @@ class Filters(threading.Thread):
self.callback['button2'] = callback self.callback['button2'] = callback
def cardCallback(self, widget, data=None): def cardCallback(self, widget, data=None):
print "DEBUG: %s was toggled %s" % (data, ("OFF", "ON")[widget.get_active()]) log.debug( "%s was toggled %s" % (data, ("OFF", "ON")[widget.get_active()]) )
def createPlayerLine(self, hbox, site, player): def createPlayerLine(self, hbox, site, player):
print 'DEBUG :: add:"%s"' % player log.debug('add:"%s"' % player)
label = gtk.Label(site +" id:") label = gtk.Label(site +" id:")
hbox.pack_start(label, False, False, 3) hbox.pack_start(label, False, False, 3)
@ -300,14 +305,14 @@ class Filters(threading.Thread):
# get_text() returns a str but we want internal variables to be unicode: # get_text() returns a str but we want internal variables to be unicode:
_guiname = unicode(_name) _guiname = unicode(_name)
self.heroes[site] = _guiname self.heroes[site] = _guiname
# print "DEBUG: setting heroes[%s]: %s"%(site, self.heroes[site]) # log.debug("setting heroes[%s]: %s"%(site, self.heroes[site]))
def __set_num_hands(self, w, val): def __set_num_hands(self, w, val):
try: try:
self.numHands = int(w.get_text()) self.numHands = int(w.get_text())
except: except:
self.numHands = 0 self.numHands = 0
# print "DEBUG: setting numHands:", self.numHands # log.debug("setting numHands:", self.numHands)
def createSiteLine(self, hbox, site): def createSiteLine(self, hbox, site):
cb = gtk.CheckButton(site) cb = gtk.CheckButton(site)
@ -332,17 +337,17 @@ class Filters(threading.Thread):
def __set_site_select(self, w, site): def __set_site_select(self, w, site):
#print w.get_active() #print w.get_active()
self.sites[site] = w.get_active() self.sites[site] = w.get_active()
print "self.sites[%s] set to %s" %(site, self.sites[site]) log.debug("self.sites[%s] set to %s" %(site, self.sites[site]))
def __set_game_select(self, w, game): def __set_game_select(self, w, game):
#print w.get_active() #print w.get_active()
self.games[game] = w.get_active() self.games[game] = w.get_active()
print "self.games[%s] set to %s" %(game, self.games[game]) log.debug("self.games[%s] set to %s" %(game, self.games[game]))
def __set_limit_select(self, w, limit): def __set_limit_select(self, w, limit):
#print w.get_active() #print w.get_active()
self.limits[limit] = w.get_active() self.limits[limit] = w.get_active()
print "self.limit[%s] set to %s" %(limit, self.limits[limit]) log.debug("self.limit[%s] set to %s" %(limit, self.limits[limit]))
if limit.isdigit() or (len(limit) > 2 and (limit[-2:] == 'nl' or limit[-2:] == 'fl' or limit[-2:] == 'pl')): if limit.isdigit() or (len(limit) > 2 and (limit[-2:] == 'nl' or limit[-2:] == 'fl' or limit[-2:] == 'pl')):
if self.limits[limit]: if self.limits[limit]:
if self.cbNoLimits is not None: if self.cbNoLimits is not None:
@ -428,9 +433,11 @@ class Filters(threading.Thread):
if self.limits[limit]: if self.limits[limit]:
if not found[self.type]: if not found[self.type]:
if self.type == 'ring': if self.type == 'ring':
self.rb['tour'].set_active(True) if 'tour' in self.rb:
self.rb['tour'].set_active(True)
elif self.type == 'tour': elif self.type == 'tour':
self.rb['ring'].set_active(True) if 'ring' in self.rb:
self.rb['ring'].set_active(True)
elif limit == "pl": elif limit == "pl":
if not self.limits[limit]: if not self.limits[limit]:
# only toggle all nl limits off if they are all currently on # only toggle all nl limits off if they are all currently on
@ -452,11 +459,13 @@ class Filters(threading.Thread):
if self.limits[limit]: if self.limits[limit]:
if not found[self.type]: if not found[self.type]:
if self.type == 'ring': if self.type == 'ring':
self.rb['tour'].set_active(True) if 'tour' in self.rb:
self.rb['tour'].set_active(True)
elif self.type == 'tour': elif self.type == 'tour':
self.rb['ring'].set_active(True) if 'ring' in self.rb:
self.rb['ring'].set_active(True)
elif limit == "ring": elif limit == "ring":
print "set", limit, "to", self.limits[limit] log.debug("set", limit, "to", self.limits[limit])
if self.limits[limit]: if self.limits[limit]:
self.type = "ring" self.type = "ring"
for cb in self.cbLimits.values(): for cb in self.cbLimits.values():
@ -464,7 +473,7 @@ class Filters(threading.Thread):
if self.types[cb.get_children()[0].get_text()] == 'tour': if self.types[cb.get_children()[0].get_text()] == 'tour':
cb.set_active(False) cb.set_active(False)
elif limit == "tour": elif limit == "tour":
print "set", limit, "to", self.limits[limit] log.debug( "set", limit, "to", self.limits[limit] )
if self.limits[limit]: if self.limits[limit]:
self.type = "tour" self.type = "tour"
for cb in self.cbLimits.values(): for cb in self.cbLimits.values():
@ -475,12 +484,12 @@ class Filters(threading.Thread):
def __set_seat_select(self, w, seat): def __set_seat_select(self, w, seat):
#print "__set_seat_select: seat =", seat, "active =", w.get_active() #print "__set_seat_select: seat =", seat, "active =", w.get_active()
self.seats[seat] = w.get_active() self.seats[seat] = w.get_active()
print "self.seats[%s] set to %s" %(seat, self.seats[seat]) log.debug( "self.seats[%s] set to %s" %(seat, self.seats[seat]) )
def __set_group_select(self, w, group): def __set_group_select(self, w, group):
#print "__set_seat_select: seat =", seat, "active =", w.get_active() #print "__set_seat_select: seat =", seat, "active =", w.get_active()
self.groups[group] = w.get_active() self.groups[group] = w.get_active()
print "self.groups[%s] set to %s" %(group, self.groups[group]) log.debug( "self.groups[%s] set to %s" %(group, self.groups[group]) )
def fillPlayerFrame(self, vbox, display): def fillPlayerFrame(self, vbox, display):
top_hbox = gtk.HBox(False, 0) top_hbox = gtk.HBox(False, 0)
@ -579,6 +588,7 @@ class Filters(threading.Thread):
self.createGameLine(hbox, line[0]) self.createGameLine(hbox, line[0])
else: else:
print "INFO: No games returned from database" print "INFO: No games returned from database"
log.info("No games returned from database")
def fillLimitsFrame(self, vbox, display): def fillLimitsFrame(self, vbox, display):
top_hbox = gtk.HBox(False, 0) top_hbox = gtk.HBox(False, 0)
@ -660,6 +670,7 @@ class Filters(threading.Thread):
dest = vbox2 # for ring/tour buttons dest = vbox2 # for ring/tour buttons
else: else:
print "INFO: No games returned from database" print "INFO: No games returned from database"
log.info("No games returned from database")
if "Type" in display and display["Type"] == True and found['ring'] and found['tour']: if "Type" in display and display["Type"] == True and found['ring'] and found['tour']:
rb1 = gtk.RadioButton(None, self.filterText['ring']) rb1 = gtk.RadioButton(None, self.filterText['ring'])

View File

@ -26,13 +26,13 @@ import gtk
import gobject import gobject
import pango import pango
import Configuration import logging
# logging has been set up in fpdb.py or HUD_main.py, use their settings:
log = logging.getLogger("logview")
log = Configuration.get_logger("logging.conf", "logview")
MAX_LINES = 100000 # max lines to display in window MAX_LINES = 100000 # max lines to display in window
EST_CHARS_PER_LINE = 150 # used to guesstimate number of lines in log file EST_CHARS_PER_LINE = 150 # used to guesstimate number of lines in log file
logfile = 'logging.out' # name of logfile
class GuiLogView: class GuiLogView:
@ -41,6 +41,7 @@ class GuiLogView:
self.main_window = mainwin self.main_window = mainwin
self.closeq = closeq self.closeq = closeq
self.logfile = self.config.log_file # name of logfile
self.dia = gtk.Dialog(title="Log Messages" self.dia = gtk.Dialog(title="Log Messages"
,parent=None ,parent=None
,flags=gtk.DIALOG_DESTROY_WITH_PARENT ,flags=gtk.DIALOG_DESTROY_WITH_PARENT
@ -117,10 +118,10 @@ class GuiLogView:
self.listcols = [] self.listcols = []
# guesstimate number of lines in file # guesstimate number of lines in file
if os.path.exists(logfile): if os.path.exists(self.logfile):
stat_info = os.stat(logfile) stat_info = os.stat(self.logfile)
lines = stat_info.st_size / EST_CHARS_PER_LINE lines = stat_info.st_size / EST_CHARS_PER_LINE
print "logview: size =", stat_info.st_size, "lines =", lines #print "logview: size =", stat_info.st_size, "lines =", lines
# set startline to line number to start display from # set startline to line number to start display from
startline = 0 startline = 0
@ -129,7 +130,7 @@ class GuiLogView:
startline = lines - MAX_LINES startline = lines - MAX_LINES
l = 0 l = 0
for line in open(logfile): for line in open(self.logfile):
# eg line: # eg line:
# 2009-12-02 15:23:21,716 - config DEBUG config logger initialised # 2009-12-02 15:23:21,716 - config DEBUG config logger initialised
l = l + 1 l = l + 1

View File

@ -51,9 +51,6 @@ import gobject
# FreePokerTools modules # FreePokerTools modules
import Configuration import Configuration
log = Configuration.get_logger("logging.conf", config = 'hud')
log.debug("%s logger initialized." % "hud")
import Database import Database
from HandHistoryConverter import getTableTitleRe from HandHistoryConverter import getTableTitleRe
@ -66,6 +63,10 @@ elif os.name == 'nt':
import Hud import Hud
# logger is set up in __init__, create temp logger here
log = Configuration.get_logger("logging.conf", config = 'hud')
class HUD_main(object): class HUD_main(object):
"""A main() object to own both the read_stdin thread and the gui.""" """A main() object to own both the read_stdin thread and the gui."""
# This class mainly provides state for controlling the multiple HUDs. # This class mainly provides state for controlling the multiple HUDs.
@ -74,9 +75,10 @@ class HUD_main(object):
try: try:
print "HUD_main: starting ..." print "HUD_main: starting ..."
self.db_name = db_name self.db_name = db_name
self.config = Configuration.Config(file=options.config, dbname=options.dbname) self.config = Configuration.Config(file=options.config, dbname=db_name)
log = Configuration.get_logger("logging.conf", "hud", log_dir=self.config.dir_log) log = Configuration.get_logger("logging.conf", "hud", log_dir=self.config.dir_log)
log.debug("starting ...") log.info("HUD_main starting")
log.info("Using db name = %s" % (db_name))
self.hud_dict = {} self.hud_dict = {}
self.hud_params = self.config.get_hud_ui_parameters() self.hud_params = self.config.get_hud_ui_parameters()
@ -295,9 +297,6 @@ class HUD_main(object):
if __name__== "__main__": if __name__== "__main__":
log.info("HUD_main starting")
log.info("Using db name = %s" % (options.dbname))
# start the HUD_main object # start the HUD_main object
hm = HUD_main(db_name = options.dbname) hm = HUD_main(db_name = options.dbname)

View File

@ -21,7 +21,6 @@
import re import re
import sys import sys
import traceback import traceback
import logging
import os import os
import os.path import os.path
from decimal import Decimal from decimal import Decimal
@ -30,12 +29,16 @@ import time,datetime
from copy import deepcopy from copy import deepcopy
import pprint import pprint
import logging
# logging has been set up in fpdb.py or HUD_main.py, use their settings:
log = logging.getLogger("parser")
import Configuration import Configuration
from Exceptions import * from Exceptions import *
import DerivedStats import DerivedStats
import Card import Card
log = Configuration.get_logger("logging.conf", "parser")
class Hand(object): class Hand(object):
@ -50,7 +53,7 @@ class Hand(object):
def __init__(self, config, sitename, gametype, handText, builtFrom = "HHC"): def __init__(self, config, sitename, gametype, handText, builtFrom = "HHC"):
self.config = config self.config = config
log = Configuration.get_logger("logging.conf", "db", log_dir=self.config.dir_log) #log = Configuration.get_logger("logging.conf", "db", log_dir=self.config.dir_log)
self.sitename = sitename self.sitename = sitename
self.siteId = self.SITEIDS[sitename] self.siteId = self.SITEIDS[sitename]
self.stats = DerivedStats.DerivedStats(self) self.stats = DerivedStats.DerivedStats(self)

View File

@ -30,6 +30,11 @@ from xml.dom.minidom import Node
import time import time
import datetime import datetime
import logging
# logging has been set up in fpdb.py or HUD_main.py, use their settings:
log = logging.getLogger("parser")
import Hand import Hand
import Tourney import Tourney
from Exceptions import FpdbParseError from Exceptions import FpdbParseError
@ -38,7 +43,6 @@ import Configuration
import gettext import gettext
gettext.install('fpdb') gettext.install('fpdb')
log = Configuration.get_logger("logging.conf", "parser")
import pygtk import pygtk
import gtk import gtk
@ -65,7 +69,7 @@ out_path (default '-' = sys.stdout)
follow : whether to tail -f the input""" follow : whether to tail -f the input"""
self.config = config self.config = config
log = Configuration.get_logger("logging.conf", "parser", log_dir=self.config.dir_log) #log = Configuration.get_logger("logging.conf", "parser", log_dir=self.config.dir_log)
log.info("HandHistory init - %s subclass, in_path '%s'; out_path '%s'" % (self.sitename, in_path, out_path) ) log.info("HandHistory init - %s subclass, in_path '%s'; out_path '%s'" % (self.sitename, in_path, out_path) )
self.index = index self.index = index

View File

@ -117,7 +117,6 @@ import Exceptions
VERSION = "0.12" VERSION = "0.12"
log = Configuration.get_logger("logging.conf", "fpdb")
class fpdb: class fpdb:
def tab_clicked(self, widget, tab_name): def tab_clicked(self, widget, tab_name):
@ -696,6 +695,7 @@ class fpdb:
"""Loads profile from the provided path name.""" """Loads profile from the provided path name."""
self.config = Configuration.Config(file=options.config, dbname=options.dbname) self.config = Configuration.Config(file=options.config, dbname=options.dbname)
log = Configuration.get_logger("logging.conf", "fpdb", log_dir=self.config.dir_log) log = Configuration.get_logger("logging.conf", "fpdb", log_dir=self.config.dir_log)
print "Logfile is " + os.path.join(self.config.dir_log, 'logging.out') + "\n"
if self.config.example_copy: if self.config.example_copy:
self.info_box( "Config file" self.info_box( "Config file"
, "has been created at:\n%s.\n" % self.config.file , "has been created at:\n%s.\n" % self.config.file
@ -721,6 +721,9 @@ class fpdb:
err_msg = None err_msg = None
try: try:
self.db = Database.Database(self.config, sql = self.sql) self.db = Database.Database(self.config, sql = self.sql)
if self.db.get_backend_name() == 'SQLite':
# tell sqlite users where the db file is
print "Connected to SQLite: %(database)s" % {'database':self.db.db_path}
except Exceptions.FpdbMySQLAccessDenied: except Exceptions.FpdbMySQLAccessDenied:
err_msg = "MySQL Server reports: Access denied. Are your permissions set correctly?" err_msg = "MySQL Server reports: Access denied. Are your permissions set correctly?"
except Exceptions.FpdbMySQLNoDatabase: except Exceptions.FpdbMySQLNoDatabase:

7
pyfpdb/fpdb_import.py Normal file → Executable file
View File

@ -30,6 +30,10 @@ import Queue
from collections import deque # using Queue for now from collections import deque # using Queue for now
import threading import threading
import logging
# logging has been set up in fpdb.py or HUD_main.py, use their settings:
log = logging.getLogger("importer")
import pygtk import pygtk
import gtk import gtk
@ -39,7 +43,6 @@ import Database
import Configuration import Configuration
import Exceptions import Exceptions
log = Configuration.get_logger("logging.conf", "importer")
# database interface modules # database interface modules
try: try:
@ -65,7 +68,7 @@ class Importer:
self.config = config self.config = config
self.sql = sql self.sql = sql
log = Configuration.get_logger("logging.conf", "importer", log_dir=self.config.dir_log) #log = Configuration.get_logger("logging.conf", "importer", log_dir=self.config.dir_log)
self.filelist = {} self.filelist = {}
self.dirlist = {} self.dirlist = {}
self.siteIds = {} self.siteIds = {}

View File

@ -1,5 +1,5 @@
[loggers] [loggers]
keys=root,fpdb,logview,parser,importer,config,db,hud keys=root,fpdb,logview,parser,importer,config,db,hud,filter
[handlers] [handlers]
keys=consoleHandler,rotatingFileHandler keys=consoleHandler,rotatingFileHandler
@ -53,6 +53,12 @@ handlers=consoleHandler,rotatingFileHandler
qualname=hud qualname=hud
propagate=0 propagate=0
[logger_filter]
level=INFO
handlers=consoleHandler,rotatingFileHandler
qualname=filter
propagate=0
[handler_consoleHandler] [handler_consoleHandler]
class=StreamHandler class=StreamHandler
level=ERROR level=ERROR