Coverage for HandHistory.py: 0%
134 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-28 16:41 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-28 16:41 +0000
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3"""HandHistory.py
5Parses HandHistory xml files and returns requested objects.
6"""
7from __future__ import print_function
8# Copyright 2008-2011, Ray E. Barker
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24########################################################################
25# Standard Library modules
27import xml.dom.minidom
28from xml.dom.minidom import Node
30class HandHistory(object):
31 def __init__(self, xml_string, elements = ('ALL')):
33 doc = xml.dom.minidom.parseString(xml_string)
34 if elements == ('ALL'):
35 elements = ('BETTING', 'AWARDS', 'POSTS', 'PLAYERS', 'GAME')
37 if 'BETTING' in elements:
38 self.BETTING = Betting(doc.getElementsByTagName('BETTING')[0])
39 if 'AWARDS' in elements:
40 self.AWARDS = Awards (doc.getElementsByTagName('AWARDS')[0])
41 if 'POSTS' in elements:
42 self.POSTS = Posts (doc.getElementsByTagName('POSTS')[0])
43 if 'GAME' in elements:
44 self.GAME = Game (doc.getElementsByTagName('GAME')[0])
45 if 'PLAYERS' in elements:
46 self.PLAYERS = {}
47 p_n = doc.getElementsByTagName('PLAYERS')[0]
48 for p in p_n.getElementsByTagName('PLAYER'):
49 a_player = Player(p)
50 self.PLAYERS[a_player.name] = a_player
52class Player(object):
53 def __init__(self, node):
54 self.name = node.getAttribute('NAME')
55 self.seat = node.getAttribute('SEAT')
56 self.stack = node.getAttribute('STACK')
57 self.showed_hand = node.getAttribute('SHOWED_HAND')
58 self.cards = node.getAttribute('CARDS')
59 self.allin = node.getAttribute('ALLIN')
60 self.sitting_out = node.getAttribute('SITTING_OUT')
61 self.hand = node.getAttribute('HAND')
62 self.start_cards = node.getAttribute('START_CARDS')
64 if self.allin == '' or \
65 self.allin == '0' or \
66 self.allin.upper() == 'FALSE': self.allin = False
67 else: self.allin = True
69 if self.sitting_out == '' or \
70 self.sitting_out == '0' or \
71 self.sitting_out.upper() == 'FALSE': self.sitting_out = False
72 else: self.sitting_out = True
74 def __str__(self):
75 temp = "%s\n seat = %s\n stack = %s\n cards = %s\n" % \
76 (self.name, self.seat, self.stack, self.cards)
77 temp = temp + " showed_hand = %s\n allin = %s\n" % \
78 (self.showed_hand, self.allin)
79 temp = temp + " hand = %s\n start_cards = %s\n" % \
80 (self.hand, self.start_cards)
81 return temp
83class Awards(object):
84 def __init__(self, node):
85 self.awards = [] # just an array of award objects
86 for a in node.getElementsByTagName('AWARD'):
87 self.awards.append(Award(a))
89 def __str__(self):
90 temp = ""
91 for a in self.awards:
92 temp = temp + "%s\n" % (a)
93 return temp
95class Award(object):
96 def __init__(self, node):
97 self.player = node.getAttribute('PLAYER')
98 self.amount = node.getAttribute('AMOUNT')
99 self.pot = node.getAttribute('POT')
101 def __str__(self):
102 return self.player + " won " + self.amount + " from " + self.pot
104class Game(object):
105 def __init__(self, node):
106 print(node)
107 self.tags = {}
108 for tag in ( ('GAME_NAME', 'game_name'), ('MAX', 'max'), ('HIGHLOW', 'high_low'),
109 ('STRUCTURE', 'structure'), ('MIXED', 'mixed') ):
110 L = node.getElementsByTagName(tag[0])
111 if (not L): continue
112 print(L)
113 for node2 in L:
114 title = ""
115 for node3 in node2.childNodes:
116 if (node3.nodeType == Node.TEXT_NODE):
117 title +=node3.data
118 self.tags[tag[1]] = title
120 def __str__(self):
121 return "%s %s %s, (%s max), %s" % (self.tags['structure'],
122 self.tags['game_name'],
123 self.tags['game_name'],
124 self.tags['max'],
125 self.tags['game_name'])
127class Posts(object):
128 def __init__(self, node):
129 self.posts = [] # just an array of post objects
130 for p in node.getElementsByTagName('POST'):
131 self.posts.append(Post(p))
133 def __str__(self):
134 temp = ""
135 for p in self.posts:
136 temp = temp + "%s\n" % (p)
137 return temp
139class Post(object):
140 def __init__(self, node):
141 self.player = node.getAttribute('PLAYER')
142 self.amount = node.getAttribute('AMOUNT')
143 self.posted = node.getAttribute('POSTED')
144 self.live = node.getAttribute('LIVE')
146 def __str__(self):
147 return ("%s posted %s %s %s") % (self.player, self.amount, self.posted, self.live)
149class Betting(object):
150 def __init__(self, node):
151 self.rounds = [] # a Betting object is just an array of rounds
152 for r in node.getElementsByTagName('ROUND'):
153 self.rounds.append(Round(r))
155 def __str__(self):
156 temp = ""
157 for r in self.rounds:
158 temp = temp + "%s\n" % (r)
159 return temp
161class Round(object):
162 def __init__(self, node):
163 self.name = node.getAttribute('ROUND_NAME')
164 self.action = []
165 for a in node.getElementsByTagName('ACTION'):
166 self.action.append(Action(a))
168 def __str__(self):
169 temp = self.name + "\n"
170 for a in self.action:
171 temp = temp + " %s\n" % (a)
172 return temp
174class Action(object):
175 def __init__(self, node):
176 self.player = node.getAttribute('PLAYER')
177 self.action = node.getAttribute('ACT')
178 self.amount = node.getAttribute('AMOUNT')
179 self.allin = node.getAttribute('ALLIN')
181 def __str__(self):
182 return self.player + " " + self.action + " " + self.amount + " " + self.allin
184if __name__== "__main__":
185 file = open('test.xml', 'r')
186 xml_string = file.read()
187 file.close()
189 print(xml_string + "\n\n\n")
190 h = HandHistory(xml_string, ('ALL'))
191 print(h.GAME)
192 print(h.POSTS)
193 print(h.BETTING)
194 print(h.AWARDS)
196 for p in list(h.PLAYERS.keys()):
197 print(h.PLAYERS[p])