Coverage for HandHistory.py: 0%

137 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-18 00:10 +0000

1#!/usr/bin/env python 

2# -*- coding: utf-8 -*- 

3"""HandHistory.py 

4 

5Parses HandHistory xml files and returns requested objects. 

6""" 

7 

8from __future__ import print_function 

9# Copyright 2008-2011, Ray E. Barker 

10# 

11# This program is free software; you can redistribute it and/or modify 

12# it under the terms of the GNU General Public License as published by 

13# the Free Software Foundation; either version 2 of the License, or 

14# (at your option) any later version. 

15# 

16# This program is distributed in the hope that it will be useful, 

17# but WITHOUT ANY WARRANTY; without even the implied warranty of 

18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

19# GNU General Public License for more details. 

20# 

21# You should have received a copy of the GNU General Public License 

22# along with this program; if not, write to the Free Software 

23# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

24 

25######################################################################## 

26# Standard Library modules 

27 

28import xml.dom.minidom 

29from xml.dom.minidom import Node 

30 

31 

32class HandHistory(object): 

33 def __init__(self, xml_string, elements=("ALL")): 

34 doc = xml.dom.minidom.parseString(xml_string) 

35 if elements == ("ALL"): 

36 elements = ("BETTING", "AWARDS", "POSTS", "PLAYERS", "GAME") 

37 

38 if "BETTING" in elements: 

39 self.BETTING = Betting(doc.getElementsByTagName("BETTING")[0]) 

40 if "AWARDS" in elements: 

41 self.AWARDS = Awards(doc.getElementsByTagName("AWARDS")[0]) 

42 if "POSTS" in elements: 

43 self.POSTS = Posts(doc.getElementsByTagName("POSTS")[0]) 

44 if "GAME" in elements: 

45 self.GAME = Game(doc.getElementsByTagName("GAME")[0]) 

46 if "PLAYERS" in elements: 

47 self.PLAYERS = {} 

48 p_n = doc.getElementsByTagName("PLAYERS")[0] 

49 for p in p_n.getElementsByTagName("PLAYER"): 

50 a_player = Player(p) 

51 self.PLAYERS[a_player.name] = a_player 

52 

53 

54class Player(object): 

55 def __init__(self, node): 

56 self.name = node.getAttribute("NAME") 

57 self.seat = node.getAttribute("SEAT") 

58 self.stack = node.getAttribute("STACK") 

59 self.showed_hand = node.getAttribute("SHOWED_HAND") 

60 self.cards = node.getAttribute("CARDS") 

61 self.allin = node.getAttribute("ALLIN") 

62 self.sitting_out = node.getAttribute("SITTING_OUT") 

63 self.hand = node.getAttribute("HAND") 

64 self.start_cards = node.getAttribute("START_CARDS") 

65 

66 if self.allin == "" or self.allin == "0" or self.allin.upper() == "FALSE": 

67 self.allin = False 

68 else: 

69 self.allin = True 

70 

71 if self.sitting_out == "" or self.sitting_out == "0" or self.sitting_out.upper() == "FALSE": 

72 self.sitting_out = False 

73 else: 

74 self.sitting_out = True 

75 

76 def __str__(self): 

77 temp = "%s\n seat = %s\n stack = %s\n cards = %s\n" % (self.name, self.seat, self.stack, self.cards) 

78 temp = temp + " showed_hand = %s\n allin = %s\n" % (self.showed_hand, self.allin) 

79 temp = temp + " hand = %s\n start_cards = %s\n" % (self.hand, self.start_cards) 

80 return temp 

81 

82 

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)) 

88 

89 def __str__(self): 

90 temp = "" 

91 for a in self.awards: 

92 temp = temp + "%s\n" % (a) 

93 return temp 

94 

95 

96class Award(object): 

97 def __init__(self, node): 

98 self.player = node.getAttribute("PLAYER") 

99 self.amount = node.getAttribute("AMOUNT") 

100 self.pot = node.getAttribute("POT") 

101 

102 def __str__(self): 

103 return self.player + " won " + self.amount + " from " + self.pot 

104 

105 

106class Game(object): 

107 def __init__(self, node): 

108 print(node) 

109 self.tags = {} 

110 for tag in ( 

111 ("GAME_NAME", "game_name"), 

112 ("MAX", "max"), 

113 ("HIGHLOW", "high_low"), 

114 ("STRUCTURE", "structure"), 

115 ("MIXED", "mixed"), 

116 ): 

117 L = node.getElementsByTagName(tag[0]) 

118 if not L: 

119 continue 

120 print(L) 

121 for node2 in L: 

122 title = "" 

123 for node3 in node2.childNodes: 

124 if node3.nodeType == Node.TEXT_NODE: 

125 title += node3.data 

126 self.tags[tag[1]] = title 

127 

128 def __str__(self): 

129 return "%s %s %s, (%s max), %s" % ( 

130 self.tags["structure"], 

131 self.tags["game_name"], 

132 self.tags["game_name"], 

133 self.tags["max"], 

134 self.tags["game_name"], 

135 ) 

136 

137 

138class Posts(object): 

139 def __init__(self, node): 

140 self.posts = [] # just an array of post objects 

141 for p in node.getElementsByTagName("POST"): 

142 self.posts.append(Post(p)) 

143 

144 def __str__(self): 

145 temp = "" 

146 for p in self.posts: 

147 temp = temp + "%s\n" % (p) 

148 return temp 

149 

150 

151class Post(object): 

152 def __init__(self, node): 

153 self.player = node.getAttribute("PLAYER") 

154 self.amount = node.getAttribute("AMOUNT") 

155 self.posted = node.getAttribute("POSTED") 

156 self.live = node.getAttribute("LIVE") 

157 

158 def __str__(self): 

159 return ("%s posted %s %s %s") % (self.player, self.amount, self.posted, self.live) 

160 

161 

162class Betting(object): 

163 def __init__(self, node): 

164 self.rounds = [] # a Betting object is just an array of rounds 

165 for r in node.getElementsByTagName("ROUND"): 

166 self.rounds.append(Round(r)) 

167 

168 def __str__(self): 

169 temp = "" 

170 for r in self.rounds: 

171 temp = temp + "%s\n" % (r) 

172 return temp 

173 

174 

175class Round(object): 

176 def __init__(self, node): 

177 self.name = node.getAttribute("ROUND_NAME") 

178 self.action = [] 

179 for a in node.getElementsByTagName("ACTION"): 

180 self.action.append(Action(a)) 

181 

182 def __str__(self): 

183 temp = self.name + "\n" 

184 for a in self.action: 

185 temp = temp + " %s\n" % (a) 

186 return temp 

187 

188 

189class Action(object): 

190 def __init__(self, node): 

191 self.player = node.getAttribute("PLAYER") 

192 self.action = node.getAttribute("ACT") 

193 self.amount = node.getAttribute("AMOUNT") 

194 self.allin = node.getAttribute("ALLIN") 

195 

196 def __str__(self): 

197 return self.player + " " + self.action + " " + self.amount + " " + self.allin 

198 

199 

200if __name__ == "__main__": 

201 file = open("test.xml", "r") 

202 xml_string = file.read() 

203 file.close() 

204 

205 print(xml_string + "\n\n\n") 

206 h = HandHistory(xml_string, ("ALL")) 

207 print(h.GAME) 

208 print(h.POSTS) 

209 print(h.BETTING) 

210 print(h.AWARDS) 

211 

212 for p in list(h.PLAYERS.keys()): 

213 print(h.PLAYERS[p])