I am trying to make a class that can take either a list of urls or a single url and render them.
In the list case it renders them all and makes available a dictionary containing all the htmls. This works fine.
In the single case it takes a url, renders it and makes the html available as an attribute, then quits. This works fine when I run it once, but when I try it 2 or more times it locks up when it calls app.exec_().
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import QWebPage, QWebFrame
class Renderer(QWebPage):
def __init__(self):
self.app = QCoreApplication.instance()
if self.app == None:
self.app = QApplication(sys.argv)
self.app.Type(QApplication.Tty)
QWebPage.__init__(self)
self.pages = []
def start(self, urls):
#for lists
try:
self.loadFinished.disconnect()
except Exception:
pass
self.loadFinished.connect(self.listFinished)
self._urls = iter(urls)
self.fetchNext()
self.app.exec_()
def fetchNext(self):
#for lists
try:
url = next(self._urls)
except StopIteration:
return False
else:
self.mainFrame().load(QUrl(url))
return True
def listFinished(self):
#for lists
html = self.processCurrentPage()
self.pages.append(html)
if not self.fetchNext():
self.app.quit()
def processCurrentPage(self):
url = self.mainFrame().url().toString()
html = self.mainFrame().toHtml()
return html
def render(self, url):
try:
self.loadFinished.disconnect()
except Exception:
pass
self.loadFinished.connect(self.singleFinished)
self._url = url
self.mainFrame().load(QUrl(url))
self.app.exec_()
def singleFinished(self):
print "singleFinished"
html = self.processCurrentPage()
self._html = html
self.app.quit()
Is what I'm trying to do possible? How can I fix this code so I can call render() multiple times? Should I just use the list-based version?
The same problem occurs when I try the list case and then the single case. I'm pretty sure it doesn't like me calling exec_() after quit(), but I haven't found any documentation on this.