-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRushNotesMerge.py
184 lines (151 loc) · 6.17 KB
/
RushNotesMerge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""RushNotesMerge.py
EXPERIMENTAL - USE WITH CARE
Merge .queue file with hero's note to generate fresh .merge file
normal usage
$> ./RushNotesMerge.py "/home/foo/.wine/drive_c/Program Files/Full Tilt Poker/heroname.xml"
The generated file can then replace heroname.xml (if all is well).
"""
# Copyright 2010-2011, "Gimick" of the FPDB project fpdb.sourceforge.net
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU Affero General Public License as published by
#the Free Software Foundation, version 3 of the License.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU Affero General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
#In the "official" distribution you can find the license in agpl-3.0.txt.
########################################################################
#TODO gettextify
# Standard Library modules
import os
import sys
from xml.dom import minidom
#
# overload minidom methods to fix bug where \n is parsed as " ".
# described here: http://bugs.python.org/issue7139
#
def _write_data(writer, data, isAttrib=False):
"Writes datachars to writer."
if isAttrib:
data = data.replace("\r", "
").replace("\n", "
")
data = data.replace("\t", "	")
writer.write(data)
minidom._write_data = _write_data
def writexml(self, writer, indent="", addindent="", newl=""):
# indent = current indentation
# addindent = indentation to add to higher levels
# newl = newline string
writer.write(indent+"<" + self.tagName)
attrs = self._get_attributes()
a_names = attrs.keys()
a_names.sort()
for a_name in a_names:
writer.write(" %s=\"" % a_name)
_write_data(writer, attrs[a_name].value, isAttrib=True)
writer.write("\"")
if self.childNodes:
writer.write(">%s"%(newl))
for node in self.childNodes:
node.writexml(writer,indent+addindent,addindent,newl)
writer.write("%s</%s>%s" % (indent,self.tagName,newl))
else:
writer.write("/>%s"%(newl))
# For an introduction to overriding instance methods, see
# http://irrepupavel.com/documents/python/instancemethod/
instancemethod = type(minidom.Element.writexml)
minidom.Element.writexml = instancemethod(
writexml, None, minidom.Element)
statqueue=0
statupdated=0
statadded=0
def cleannote(textin):
if textin.find("~fpdb~") == -1: return textin
if textin.find("~ends~") == -1: return textin
if textin.find("~fpdb~") > textin.find("~ends~"): return textin
return textin[0:textin.find("~fpdb~")] + textin[textin.find("~ends~")+6:]
# get out now if parameter not passed
try:
sys.argv[1] <> ""
except:
print "A parameter is required, quitting now"
print "normal usage is something like:"
print '$> ./RushNotesMerge.py "/home/foo/.wine/drive_c/Program Files/Full Tilt Poker/myhero.xml"'
quit()
if not os.path.isfile(sys.argv[1]):
print "Hero notes file not found, quitting"
print "normal usage is something like:"
print '$> ./RushNotesMerge.py "/home/foo/.wine/drive_c/Program Files/Full Tilt Poker/myhero.xml"'
quit()
if not os.path.isfile((sys.argv[1]+".queue")):
print "Nothing found to merge, quitting"
quit()
print "***************************************************************"
print "IMPORTANT: *** Before running this merge: ***"
print "Closedown the FullTiltClient and wait for it to completely stop"
print "If FullTiltClient was running, run the merge again once it"
print "has stopped completely"
print "***************************************************************"
print
print "read from: ", sys.argv[1]
print "updated with: ", sys.argv[1]+".queue"
#read queue and turn into a dict
queuedict = {}
xmlqueue = minidom.parse(sys.argv[1]+".queue")
notelist = xmlqueue.getElementsByTagName('NOTE')
for noteentry in notelist:
noteplayer = noteentry.getAttribute("PlayerId")
notetext = noteentry.getAttribute("Text")
queuedict[noteplayer] = notetext
statqueue = statqueue + 1
#read existing player note file
xmlnotefile = minidom.parse(sys.argv[1])
notelist = xmlnotefile.getElementsByTagName('NOTE')
#
#for existing players, empty out existing fpdbtext and refill
#
for noteentry in notelist:
noteplayer = noteentry.getAttribute("PlayerId")
if noteplayer in queuedict:
existingnote = noteentry.getAttribute("Text")
newnote=cleannote(existingnote)
newnote = newnote + queuedict[noteplayer]
noteentry.setAttribute("Text",newnote)
statupdated = statupdated + 1
del queuedict[noteplayer]
#
#create entries for new players (those remaining in the dictionary)
#
if len(queuedict) > 0:
playerdata=xmlnotefile.lastChild #move to the PLAYERDATA node (assume last one in the list)
notesnode=playerdata.childNodes[1] #Find NOTES node
for newplayer in queuedict:
newentry = xmlnotefile.createElement("NOTE")
newentry.setAttribute("PlayerId", newplayer)
newentry.setAttribute("Text", queuedict[newplayer])
notesnode.insertBefore(newentry,None)
newentry = xmlnotefile.createTextNode("\n")
notesnode.insertBefore(newentry,None)
statadded=statadded+1
#print xmlnotefile.toprettyxml()
mergednotes = open(sys.argv[1]+".merged", 'w')
xmlnotefile.writexml(mergednotes)
mergednotes.close()
xmlnotefile.unlink
print "written to: ", sys.argv[1]+".merged"
print ""
print "number in queue: ", statqueue
print "existing players updated: ", statupdated
print "new players added: ", statadded
print "\n"
print "Use a viewer to check the contents of the merge file."
print "If you are happy, carry out the following steps:"
print "1 Rename or delete the existing notes file (normally <heroname>.xml)"
print "2 Rename the .merged file to become the new notes file"
print "3 Delete the .queue file (it will be created at the next rush autoimport)"