Re: [PyQt] Monkeypatching QWebPage.userAgentForUrl
On Fri May 13 02:23:30 BST 2011, Kay Hayen wrote: > >> thanks in advance for any explanations or suggestions how to change the > >> user agent 'globally' > > > > Why do you try to monkeypatch it? Subclassing QWebView and implementing > > just the overridden method is much cleaner and doesn't look like a hack. > > Also if you my message about the SIP issue, it appears that the PyQt > core will not consider any overload it doesn't detect as being such at > class creation time. I think this QWebPage issue is just a misunderstanding about what kind of objects are being passed around. The objects people create from within Python are subclasses of Qt widgets with extra hooks to enable method reimplementation from within Python, and this includes support for monkey-patching of both classes and instances. > I almost clearly doubt, that attribute assignment will modify the class > later on. Here's a working example: from PyQt4.QtGui import * app = QApplication([]) def paintEvent(self, event): painter = QPainter() painter.begin(self) painter.fillRect(event.rect(), QBrush(QColor(255,0,0))) painter.end() QWidget.paintEvent = paintEvent w = QWidget() w.show() app.exec_() > In general, it appears, that to overload you must use a class and for me > unfortunately, it must be a pure Python function or method object at > that point. I haven't looked at this in any detail, but you may be correct in saying that the hooks that allow method overriding are specifically looking for certain kinds of objects; in this case, Python callables. > Enhancing sip to be able to track at run time the changes to the class > appears possible. For consistency with Python it should be done, but the > benefit, other than consistency with every other Python class, I admit, > is dubious. I haven't looked at this, either. I think sip already does part of what you want. I'll re-read your earlier message and try to understand what you were looking at. David ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Monkeypatching QWebPage.userAgentForUrl
On Wed May 11 22:41:13 BST 2011, Gelonida G wrote: > def patch_agent(): > old_userAgentForUrl = QWebPage.userAgentForUrl > def new_userAgentForUrl(self, url): > """ for demo purposes I just added logging """ > rslt = old_userAgentForUrl(self, url) > print "agent is %s" % rslt > return rslt > QWebPage.userAgentForUrl = new_userAgentForUrl [...] > app = PyQt4.QtGui.QApplication(sys.argv) > webview = QWebView() > webview.setHtml("""http://www.google.com";>Search""") > webview.show() > sys.exit(app.exec_()) > > Somehow it seems, that the my patched method is never called. > > What could be the reason? My intuition is that the monkey-patching will only work for QWebPage instances created by PyQt. Since the QWebPage instance shown in the QWebView is created by Qt, you get a regular, unpatched instance instead. If you want to use your patched QWebPage, call QWebView's setPage() method with one of your patched instances. David ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Monkeypatching QWebPage.userAgentForUrl
Hello everybody, Somehow it seems, that the my patched method is never called. What could be the reason? thanks in advance for any explanations or suggestions how to change the user agent 'globally' Why do you try to monkeypatch it? Subclassing QWebView and implementing just the overridden method is much cleaner and doesn't look like a hack. Also if you my message about the SIP issue, it appears that the PyQt core will not consider any overload it doesn't detect as being such at class creation time. I almost clearly doubt, that attribute assignment will modify the class later on. In general, it appears, that to overload you must use a class and for me unfortunately, it must be a pure Python function or method object at that point. Enhancing sip to be able to track at run time the changes to the class appears possible. For consistency with Python it should be done, but the benefit, other than consistency with every other Python class, I admit, is dubious. Yours, Kay ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Monkeypatching QWebPage.userAgentForUrl
On Mittwoch, 11. Mai 2011, Gelonida G wrote: > Hi, > > I would like to send a custom useragent for any http access done from a > certain python script. > > So I assumed the way to go would be monkeypatching userAgentForUrl > > I'm not sure, whether this is impossible to achieve or whether I made a > mistake in patching. > > This is my approach in my code before the creation of any QWebview. > > from PyQt4.QtWebKit import QWebPage > import PyQt4.QtGui > > def patch_agent(): > old_userAgentForUrl = QWebPage.userAgentForUrl > def new_userAgentForUrl(self, url): > """ for demo purposes I just added logging """ > rslt = old_userAgentForUrl(self, url) > print "agent is %s" % rslt > return rslt > QWebPage.userAgentForUrl = new_userAgentForUrl > > > patch_agent() > print "PATCHED" > > app = PyQt4.QtGui.QApplication(sys.argv) > webview = QWebView() > webview.setHtml("""http://www.google.com";>Search""") > webview.show() > sys.exit(app.exec_()) > > Somehow it seems, that the my patched method is never called. > > What could be the reason? > > thanks in advance for any explanations or suggestions how to change the > user agent 'globally' Why do you try to monkeypatch it? Subclassing QWebView and implementing just the overridden method is much cleaner and doesn't look like a hack. Detlev -- Detlev Offenbach det...@die-offenbachs.de ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
[PyQt] Monkeypatching QWebPage.userAgentForUrl
Hi, I would like to send a custom useragent for any http access done from a certain python script. So I assumed the way to go would be monkeypatching userAgentForUrl I'm not sure, whether this is impossible to achieve or whether I made a mistake in patching. This is my approach in my code before the creation of any QWebview. from PyQt4.QtWebKit import QWebPage import PyQt4.QtGui def patch_agent(): old_userAgentForUrl = QWebPage.userAgentForUrl def new_userAgentForUrl(self, url): """ for demo purposes I just added logging """ rslt = old_userAgentForUrl(self, url) print "agent is %s" % rslt return rslt QWebPage.userAgentForUrl = new_userAgentForUrl patch_agent() print "PATCHED" app = PyQt4.QtGui.QApplication(sys.argv) webview = QWebView() webview.setHtml("""http://www.google.com";>Search""") webview.show() sys.exit(app.exec_()) Somehow it seems, that the my patched method is never called. What could be the reason? thanks in advance for any explanations or suggestions how to change the user agent 'globally' ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt