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