Remove slice tags
[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         self.store(rawOutput)
11         self.extractXml()
12         self.xmlrpcWindow.setData(self.xml)
13         self.showXmlrpc()
14
15     def showXmlrpc(self):
16         self.xmlrpcWindow.show()
17         self.xmlrpcWindow.resize(500, 640)
18         self.xmlrpcWindow.raise_()
19         self.xmlrpcWindow.activateWindow()
20
21     def store(self, rawOutput):
22         self.rawOutput = rawOutput
23
24     def extractXml(self):
25         pttrnAsk = '<methodCall>.*?</methodCall>'
26         pttrnAns = '<methodResponse>.*?</methodResponse>'
27         answers = re.compile(pttrnAsk, re.DOTALL).findall(self.rawOutput)
28         replies = re.compile(pttrnAns, re.DOTALL).findall(self.rawOutput)
29         # cleaning
30         answers = map(lambda x: x.replace('\\n','\n'), answers)
31         replies = map(lambda x: x.replace('\\n','\n').replace("'\nbody: '", ''), replies)
32         replies.reverse() # so that I use pop() as popleft
33         # A well-formed XML document must have one, and only one, top-level element
34         self.xml = '<debug>'
35         for ans in answers:
36             self.xml += ans + replies.pop()
37         self.xml += '</debug>'
38
39     def stats(self):
40         # statistics: round-trip time, size of the com
41         pass
42
43 class XmlrpcWindow(XmlWindow):
44     def __init__(self, parent=None):
45         # super __init__() calls updateView,
46         # which assumes you have some data
47         self.data = '<debug/>'
48         XmlWindow.__init__(self, parent, 'XMLRPC window')
49
50     def setData(self, XmlrpcCom):
51         self.data = XmlrpcCom
52
53     def updateView(self):
54         XmlWindow.updateView(self)
55
56         self.document.setContent(self.data)