Add file HUD_run_me, to be run by GuiAutoImport

GuiAutoImport: run HUD_run_me instead of HUD_main
HUD_main: Add HUD_removed() function, called by HUD when an individual HUD is closed, eliminates polling them every update; use 'in' operator instead of has_key
HUD: clean up code that deals with lack of font descriptor in config, and notification of what font was selected
HUD: clean up unnecessary use of keys() functions, and 'in' vs has_key
HUD: restore previous definition of reposition_windows(), although i can't make it error at will anymore
Tables: cleanup with keys() and in operator
fpdb: no longer raise an error on duplicate tab, since we currently depend on that, it's not an error, right?
This commit is contained in:
eblade 2009-01-06 05:18:45 -05:00
parent 0f90d8a899
commit fb9d7c0af2
6 changed files with 111 additions and 46 deletions

View File

@ -128,12 +128,12 @@ class GuiAutoImport (threading.Thread):
widget.set_label(u'Stop Autoimport') widget.set_label(u'Stop Autoimport')
if self.pipe_to_hud is None: if self.pipe_to_hud is None:
if os.name == 'nt': if os.name == 'nt':
command = "python HUD_main.py" + " %s" % (self.database) command = "python HUD_run_me.py" + " %s" % (self.database)
bs = 0 # windows is not happy with line buffing here bs = 0 # windows is not happy with line buffing here
self.pipe_to_hud = subprocess.Popen(command, bufsize = bs, stdin = subprocess.PIPE, self.pipe_to_hud = subprocess.Popen(command, bufsize = bs, stdin = subprocess.PIPE,
universal_newlines=True) universal_newlines=True)
else: else:
command = self.config.execution_path('HUD_main.py') command = self.config.execution_path('HUD_run_me.py')
bs = 1 bs = 1
self.pipe_to_hud = subprocess.Popen((command, self.database), bufsize = bs, stdin = subprocess.PIPE, self.pipe_to_hud = subprocess.Popen((command, self.database), bufsize = bs, stdin = subprocess.PIPE,
universal_newlines=True) universal_newlines=True)

View File

@ -36,9 +36,6 @@ import time
import string import string
import re import re
errorfile = open('HUD-error.txt', 'w', 0)
sys.stderr = errorfile
# pyGTK modules # pyGTK modules
import pygtk import pygtk
import gtk import gtk
@ -61,11 +58,11 @@ def destroy(*args): # call back for terminating the main eventloop
gtk.main_quit() gtk.main_quit()
def create_HUD(new_hand_id, table, db_name, table_name, max, poker_game, db_connection, config, stat_dict): def create_HUD(new_hand_id, table, db_name, table_name, max, poker_game, db_connection, config, stat_dict):
global hud_dict global hud_dict, eb
global eb
def idle_func(): def idle_func():
global hud_dict global hud_dict, eb
global eb
gtk.gdk.threads_enter() gtk.gdk.threads_enter()
try: try:
newlabel = gtk.Label(table_name) newlabel = gtk.Label(table_name)
@ -98,9 +95,18 @@ def update_HUD(new_hand_id, table_name, config, stat_dict):
gtk.gdk.threads_leave() gtk.gdk.threads_leave()
gobject.idle_add(idle_func) gobject.idle_add(idle_func)
def HUD_removed(tablename):
global hud_dict, eb
if tablename in hud_dict and hud_dict[tablename].deleted:
eb.remove(hud_dict[tablename].tablehudlabel)
del(hud_dict[tablename])
return False
return True
def read_stdin(): # This is the thread function def read_stdin(): # This is the thread function
global hud_dict global hud_dict, eb
global eb
db_connection = Database.Database(config, db_name, 'temp') db_connection = Database.Database(config, db_name, 'temp')
tourny_finder = re.compile('(\d+) (\d+)') tourny_finder = re.compile('(\d+) (\d+)')
@ -112,10 +118,9 @@ def read_stdin(): # This is the thread function
destroy() destroy()
# delete hud_dict entries for any HUD destroyed since last iteration # delete hud_dict entries for any HUD destroyed since last iteration
for h in hud_dict.keys(): # for h in hud_dict:
if hud_dict[h].deleted: # HUD_removed(h)
eb.remove(hud_dict[h].tablehudlabel) # removing this function, we shouldn't need it anymore, since the hud should notify us anyway, right?
del(hud_dict[h])
# get basic info about the new hand from the db # get basic info about the new hand from the db
(table_name, max, poker_game) = db_connection.get_table_name(new_hand_id) (table_name, max, poker_game) = db_connection.get_table_name(new_hand_id)
@ -131,14 +136,14 @@ def read_stdin(): # This is the thread function
stat_dict = db_connection.get_stats_from_hand(new_hand_id) stat_dict = db_connection.get_stats_from_hand(new_hand_id)
# if a hud for this CASH table exists, just update it # if a hud for this CASH table exists, just update it
if hud_dict.has_key(table_name): if table_name in hud_dict:
# update the data for the aux_windows # update the data for the aux_windows
for aw in hud_dict[table_name].aux_windows: for aw in hud_dict[table_name].aux_windows:
aw.update_data(new_hand_id, db_connection) aw.update_data(new_hand_id, db_connection)
update_HUD(new_hand_id, table_name, config, stat_dict) update_HUD(new_hand_id, table_name, config, stat_dict)
# if a hud for this TOURNAMENT table exists, just update it # if a hud for this TOURNAMENT table exists, just update it
elif hud_dict.has_key(tour_number): elif tour_number in hud_dict:
update_HUD(new_hand_id, tour_number, config, stat_dict) update_HUD(new_hand_id, tour_number, config, stat_dict)
# otherwise create a new hud # otherwise create a new hud

52
pyfpdb/HUD_run_me.py Normal file
View File

@ -0,0 +1,52 @@
#!/usr/bin/env python
import sys
import os
import thread
import time
import string
import re
errorfile = open('HUD-error.txt', 'w', 0)
sys.stderr = errorfile
# pyGTK modules
import pygtk
import gtk
import gobject
# FreePokerTools modules
import Configuration
import Database
import Tables
import Hud
import HUD_main
def destroy(*args): # call back for terminating the main eventloop
gtk.main_quit()
if __name__== "__main__":
sys.stderr.write("HUD_main starting\n")
try:
HUD_main.db_name = sys.argv[1]
except:
HUD_main.db_name = 'fpdb'
sys.stderr.write("Using db name = %s\n" % (HUD_main.db_name))
HUD_main.config = Configuration.Config()
gobject.threads_init() # this is required
thread.start_new_thread(HUD_main.read_stdin, ()) # starts the thread
HUD_main.main_window = gtk.Window()
HUD_main.main_window.connect("destroy", destroy)
HUD_main.eb = gtk.VBox()
label = gtk.Label('Closing this window will exit from the HUD.')
HUD_main.eb.add(label)
HUD_main.main_window.add(HUD_main.eb)
HUD_main.main_window.set_title("HUD Main Window")
HUD_main.main_window.show_all()
gtk.main()

View File

@ -61,11 +61,13 @@ class Hud:
self.stat_windows = {} self.stat_windows = {}
self.popup_windows = {} self.popup_windows = {}
self.aux_windows = [] self.aux_windows = []
(font, font_size) = config.get_default_font(self.table.site) (font, font_size) = config.get_default_font(self.table.site)
print "font = ", font, "size = ", font_size if font == None:
if font == None or font_size == None: font = "Sans"
self.font = pango.FontDescription("Sans 7") if font_size == None:
else: font_size = "8"
print "Setting font to ", font + " " + font_size print "Setting font to ", font + " " + font_size
self.font = pango.FontDescription(font + " " + font_size) self.font = pango.FontDescription(font + " " + font_size)
@ -122,6 +124,9 @@ class Hud:
self.main_window.show_all() self.main_window.show_all()
# TODO: fold all uses of this type of 'topify' code into a single function, if the differences between the versions don't
# create adverse effects?
if os.name == 'nt': if os.name == 'nt':
self.topify_window(self.main_window) self.topify_window(self.main_window)
else: else:
@ -145,7 +150,7 @@ class Hud:
loc = self.config.get_locations(self.table.site, self.max) loc = self.config.get_locations(self.table.site, self.max)
for i in range(1, self.max + 1): for i in range(1, self.max + 1):
(x, y) = loc[adj[i]] (x, y) = loc[adj[i]]
if self.stat_windows.has_key(i): if i in self.stat_windows:
self.stat_windows[i].relocate(x, y) self.stat_windows[i].relocate(x, y)
return True return True
@ -159,13 +164,15 @@ class Hud:
return False return False
def kill_hud(self, *args): def kill_hud(self, *args):
for k in self.stat_windows.keys(): for k in self.stat_windows:
self.stat_windows[k].window.destroy() self.stat_windows[k].window.destroy()
self.main_window.destroy() self.main_window.destroy()
self.deleted = True self.deleted = True
HUD_main.HUD_removed(self.table.name)
def reposition_windows(self, *args): def reposition_windows(self, *args):
self.update_table_position() for w in self.stat_windows:
self.stat_windows[w].window.move(self.stat_windows[w].x, self.stat_windows[w].y)
return True return True
def debug_stat_windows(self, *args): def debug_stat_windows(self, *args):
@ -213,7 +220,7 @@ class Hud:
# create the stat windows # create the stat windows
for i in range(1, self.max + 1): for i in range(1, self.max + 1):
(x, y) = loc[adj[i]] (x, y) = loc[adj[i]]
if self.stat_windows.has_key(i): if i in self.stat_windows:
self.stat_windows[i].relocate(x, y) self.stat_windows[i].relocate(x, y)
else: else:
self.stat_windows[i] = Stat_Window(game = config.supported_games[self.poker_game], self.stat_windows[i] = Stat_Window(game = config.supported_games[self.poker_game],
@ -230,7 +237,7 @@ class Hud:
for i in range(0, config.supported_games[self.poker_game].rows + 1): for i in range(0, config.supported_games[self.poker_game].rows + 1):
row_list = [''] * config.supported_games[self.poker_game].cols row_list = [''] * config.supported_games[self.poker_game].cols
self.stats.append(row_list) self.stats.append(row_list)
for stat in config.supported_games[self.poker_game].stats.keys(): for stat in config.supported_games[self.poker_game].stats:
self.stats[config.supported_games[self.poker_game].stats[stat].row] \ self.stats[config.supported_games[self.poker_game].stats[stat].row] \
[config.supported_games[self.poker_game].stats[stat].col] = \ [config.supported_games[self.poker_game].stats[stat].col] = \
config.supported_games[self.poker_game].stats[stat].stat_name config.supported_games[self.poker_game].stats[stat].stat_name
@ -246,7 +253,7 @@ class Hud:
def update(self, hand, config, stat_dict): def update(self, hand, config, stat_dict):
self.hand = hand # this is the last hand, so it is available later self.hand = hand # this is the last hand, so it is available later
self.update_table_position() self.update_table_position()
for s in stat_dict.keys(): for s in stat_dict:
try: try:
self.stat_windows[stat_dict[s]['seat']].player_id = stat_dict[s]['player_id'] self.stat_windows[stat_dict[s]['seat']].player_id = stat_dict[s]['player_id']
except: # omg, we have more seats than stat windows .. damn poker sites with incorrect max seating info .. let's force 10 here except: # omg, we have more seats than stat windows .. damn poker sites with incorrect max seating info .. let's force 10 here
@ -267,7 +274,7 @@ class Hud:
self.stat_windows[stat_dict[s]['seat']].label[r][c].set_text(statstring) self.stat_windows[stat_dict[s]['seat']].label[r][c].set_text(statstring)
if statstring != "xxx": # is there a way to tell if this particular stat window is visible already, or no? if statstring != "xxx": # is there a way to tell if this particular stat window is visible already, or no?
self.stat_windows[stat_dict[s]['seat']].window.show_all() self.stat_windows[stat_dict[s]['seat']].window.show_all()
self.reposition_windows() # self.reposition_windows()
tip = stat_dict[s]['screen_name'] + "\n" + number[5] + "\n" + \ tip = stat_dict[s]['screen_name'] + "\n" + number[5] + "\n" + \
number[3] + ", " + number[4] number[3] + ", " + number[4]
Stats.do_tip(self.stat_windows[stat_dict[s]['seat']].e_box[r][c], tip) Stats.do_tip(self.stat_windows[stat_dict[s]['seat']].e_box[r][c], tip)
@ -449,14 +456,14 @@ class Popup_window:
# figure out what popup format we're using # figure out what popup format we're using
popup_format = "default" popup_format = "default"
for stat in stat_window.game.stats.keys(): for stat in stat_window.game.stats:
if stat_window.game.stats[stat].row == row and stat_window.game.stats[stat].col == col: if stat_window.game.stats[stat].row == row and stat_window.game.stats[stat].col == col:
popup_format = stat_window.game.stats[stat].popup popup_format = stat_window.game.stats[stat].popup
break break
# get the list of stats to be presented from the config # get the list of stats to be presented from the config
stat_list = [] stat_list = []
for w in stat_window.parent.config.popup_windows.keys(): for w in stat_window.parent.config.popup_windows:
if w == popup_format: if w == popup_format:
stat_list = stat_window.parent.config.popup_windows[w].pu_stats stat_list = stat_window.parent.config.popup_windows[w].pu_stats
break break

View File

@ -58,15 +58,15 @@ import Configuration
class Table_Window: class Table_Window:
def __init__(self, info = {}): def __init__(self, info = {}):
if info.has_key('number'): self.number = info['number'] if 'number' in info: self.number = info['number']
if info.has_key('exe'): self.exe = info['exe'] if 'exe' in info: self.exe = info['exe']
if info.has_key('width'): self.width = info['width'] if 'width' in info: self.width = info['width']
if info.has_key('height'): self.height = info['height'] if 'height' in info: self.height = info['height']
if info.has_key('x'): self.x = info['x'] if 'x' in info: self.x = info['x']
if info.has_key('y'): self.y = info['y'] if 'y' in info: self.y = info['y']
if info.has_key('site'): self.site = info['site'] if 'site' in info: self.site = info['site']
if info.has_key('title'): self.title = info['title'] if 'title' in info: self.title = info['title']
if info.has_key('name'): self.name = info['name'] if 'name' in info: self.name = info['name']
def __str__(self): def __str__(self):
# __str__ method for testing # __str__ method for testing
@ -225,7 +225,7 @@ def discover_nt_by_name(c, tablename):
"""Finds poker client window with the given table name.""" """Finds poker client window with the given table name."""
titles = {} titles = {}
win32gui.EnumWindows(win_enum_handler, titles) win32gui.EnumWindows(win_enum_handler, titles)
for hwnd in titles.keys(): for hwnd in titles:
if titles[hwnd].find(tablename) == -1: continue if titles[hwnd].find(tablename) == -1: continue
if titles[hwnd].find("History for table:") > -1: continue if titles[hwnd].find("History for table:") > -1: continue
if titles[hwnd].find("HUD:") > -1: continue if titles[hwnd].find("HUD:") > -1: continue
@ -239,7 +239,7 @@ def discover_nt_tournament(c, tour_number, tab_number):
titles ={} titles ={}
win32gui.EnumWindows(win_enum_handler, titles) win32gui.EnumWindows(win_enum_handler, titles)
for hwnd in titles.keys(): for hwnd in titles:
if re.search(search_string, titles[hwnd]): if re.search(search_string, titles[hwnd]):
return decode_windows(c, titles[hwnd], hwnd) return decode_windows(c, titles[hwnd], hwnd)
return None return None

View File

@ -62,7 +62,8 @@ class fpdb:
#print "start of add_tab" #print "start of add_tab"
for i in self.tab_names: #todo: check this is valid for i in self.tab_names: #todo: check this is valid
if i==new_tab_name: if i==new_tab_name:
raise fpdb_simple.FpdbError("duplicate tab_name not permitted") return # we depend on this to not create duplicate tabs, there's no reason to raise an error here?
# raise fpdb_simple.FpdbError("duplicate tab_name not permitted")
self.tabs.append(new_tab) self.tabs.append(new_tab)
self.tab_names.append(new_tab_name) self.tab_names.append(new_tab_name)