86c47c2e1d49f104f98f31b00f8bd7f976b34a2b
[sface.git] / sface / xmlrpcwindow.py
1 import re
2 from PyQt4.QtXml import QDomDocument
3 from sface.xmlwidget import XmlWindow, DomModel
4
5 class XmlrpcTracker():
6     def __init__(self):
7         self.xmlrpcWindow = XmlrpcWindow()
8
9     def getAndPrint(self, rawOutput):
10         print "RAW OUTPUT:\n" + rawOutput + "\nEND OF RAW OUT"
11         self.store(rawOutput)
12         self.extractXml()
13         self.xmlrpcWindow.setData(self.xml)
14         self.showXmlrpc()
15
16     def showXmlrpc(self):
17         self.xmlrpcWindow.show()
18         self.xmlrpcWindow.resize(500, 640)
19         self.xmlrpcWindow.raise_()
20         self.xmlrpcWindow.activateWindow()
21
22     def store(self, rawOutput):
23         self.rawOutput = rawOutput
24
25     def extractXml(self):
26         pttrnAsk = '<methodCall>.*?</methodCall>'
27         pttrnAns = '<methodResponse>.*?</methodResponse>'
28         answers = re.compile(pttrnAsk, re.DOTALL).findall(self.rawOutput)
29         replies = re.compile(pttrnAns, re.DOTALL).findall(self.rawOutput)
30         # cleaning
31         answers = map(lambda x: x.replace('\\n','\n'), answers)
32         replies = map(lambda x: x.replace('\\n','\n').replace("'\nbody: '", ''), replies)
33         replies.reverse() # so that I use pop() as popleft
34         # A well-formed XML document must have one, and only one, top-level element
35         self.xml = '<debug>'
36         for ans in answers:
37             self.xml += ans + replies.pop()
38         self.xml += '</debug>'
39
40     def stats(self):
41         # statistics: round-trip time, size of the com
42         pass
43
44 class XmlrpcWindow(XmlWindow):
45     def __init__(self, parent=None):
46         # super __init__() calls updateView,
47         # which assumes you have some data
48         self.data = '<debug/>'
49         XmlWindow.__init__(self, parent, 'XMLRPC window')
50
51     def setData(self, XmlrpcCom):
52         self.data = XmlrpcCom
53
54     def updateView(self):
55         XmlWindow.updateView(self)
56
57         self.document.setContent(self.data)