2 # -*- coding: utf-8 -*-
5 Parses HandHistory xml files and returns requested objects.
7 # Copyright 2008-2011, Ray E. Barker
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 ########################################################################
24 # Standard Library modules
25 import xml
.dom
.minidom
26 from xml
.dom
.minidom
import Node
29 def __init__(self
, xml_string
, elements
= ('ALL')):
31 doc
= xml
.dom
.minidom
.parseString(xml_string
)
32 if elements
== ('ALL'):
33 elements
= ('BETTING', 'AWARDS', 'POSTS', 'PLAYERS', 'GAME')
35 if 'BETTING' in elements
:
36 self
.BETTING
= Betting(doc
.getElementsByTagName('BETTING')[0])
37 if 'AWARDS' in elements
:
38 self
.AWARDS
= Awards (doc
.getElementsByTagName('AWARDS')[0])
39 if 'POSTS' in elements
:
40 self
.POSTS
= Posts (doc
.getElementsByTagName('POSTS')[0])
41 if 'GAME' in elements
:
42 self
.GAME
= Game (doc
.getElementsByTagName('GAME')[0])
43 if 'PLAYERS' in elements
:
45 p_n
= doc
.getElementsByTagName('PLAYERS')[0]
46 for p
in p_n
.getElementsByTagName('PLAYER'):
48 self
.PLAYERS
[a_player
.name
] = a_player
51 def __init__(self
, node
):
52 self
.name
= node
.getAttribute('NAME')
53 self
.seat
= node
.getAttribute('SEAT')
54 self
.stack
= node
.getAttribute('STACK')
55 self
.showed_hand
= node
.getAttribute('SHOWED_HAND')
56 self
.cards
= node
.getAttribute('CARDS')
57 self
.allin
= node
.getAttribute('ALLIN')
58 self
.sitting_out
= node
.getAttribute('SITTING_OUT')
59 self
.hand
= node
.getAttribute('HAND')
60 self
.start_cards
= node
.getAttribute('START_CARDS')
62 if self
.allin
== '' or \
63 self
.allin
== '0' or \
64 self
.allin
.upper() == 'FALSE': self
.allin
= False
65 else: self
.allin
= True
67 if self
.sitting_out
== '' or \
68 self
.sitting_out
== '0' or \
69 self
.sitting_out
.upper() == 'FALSE': self
.sitting_out
= False
70 else: self
.sitting_out
= True
73 temp
= "%s\n seat = %s\n stack = %s\n cards = %s\n" % \
74 (self
.name
, self
.seat
, self
.stack
, self
.cards
)
75 temp
= temp
+ " showed_hand = %s\n allin = %s\n" % \
76 (self
.showed_hand
, self
.allin
)
77 temp
= temp
+ " hand = %s\n start_cards = %s\n" % \
78 (self
.hand
, self
.start_cards
)
82 def __init__(self
, node
):
83 self
.awards
= [] # just an array of award objects
84 for a
in node
.getElementsByTagName('AWARD'):
85 self
.awards
.append(Award(a
))
90 temp
= temp
+ "%s\n" % (a
)
94 def __init__(self
, node
):
95 self
.player
= node
.getAttribute('PLAYER')
96 self
.amount
= node
.getAttribute('AMOUNT')
97 self
.pot
= node
.getAttribute('POT')
100 return self
.player
+ " won " + self
.amount
+ " from " + self
.pot
103 def __init__(self
, node
):
106 for tag
in ( ('GAME_NAME', 'game_name'), ('MAX', 'max'), ('HIGHLOW', 'high_low'),
107 ('STRUCTURE', 'structure'), ('MIXED', 'mixed') ):
108 L
= node
.getElementsByTagName(tag
[0])
113 for node3
in node2
.childNodes
:
114 if (node3
.nodeType
== Node
.TEXT_NODE
):
116 self
.tags
[tag
[1]] = title
119 return "%s %s %s, (%s max), %s" % (self
.tags
['structure'],
120 self
.tags
['game_name'],
121 self
.tags
['game_name'],
123 self
.tags
['game_name'])
126 def __init__(self
, node
):
127 self
.posts
= [] # just an array of post objects
128 for p
in node
.getElementsByTagName('POST'):
129 self
.posts
.append(Post(p
))
134 temp
= temp
+ "%s\n" % (p
)
138 def __init__(self
, node
):
139 self
.player
= node
.getAttribute('PLAYER')
140 self
.amount
= node
.getAttribute('AMOUNT')
141 self
.posted
= node
.getAttribute('POSTED')
142 self
.live
= node
.getAttribute('LIVE')
145 return ("%s posted %s %s %s") % (self
.player
, self
.amount
, self
.posted
, self
.live
)
148 def __init__(self
, node
):
149 self
.rounds
= [] # a Betting object is just an array of rounds
150 for r
in node
.getElementsByTagName('ROUND'):
151 self
.rounds
.append(Round(r
))
155 for r
in self
.rounds
:
156 temp
= temp
+ "%s\n" % (r
)
160 def __init__(self
, node
):
161 self
.name
= node
.getAttribute('ROUND_NAME')
163 for a
in node
.getElementsByTagName('ACTION'):
164 self
.action
.append(Action(a
))
167 temp
= self
.name
+ "\n"
168 for a
in self
.action
:
169 temp
= temp
+ " %s\n" % (a
)
173 def __init__(self
, node
):
174 self
.player
= node
.getAttribute('PLAYER')
175 self
.action
= node
.getAttribute('ACT')
176 self
.amount
= node
.getAttribute('AMOUNT')
177 self
.allin
= node
.getAttribute('ALLIN')
180 return self
.player
+ " " + self
.action
+ " " + self
.amount
+ " " + self
.allin
182 if __name__
== "__main__":
183 file = open('test.xml', 'r')
184 xml_string
= file.read()
187 print xml_string
+ "\n\n\n"
188 h
= HandHistory(xml_string
, ('ALL'))
194 for p
in h
.PLAYERS
.keys():