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