2011-04-12 01:32:07 -07:00
|
|
|
'''
|
|
|
|
|
This file is part of the PyPhantomJS project.
|
|
|
|
|
|
|
|
|
|
Copyright (C) 2011 James Roe <roejames12@hotmail.com>
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-04-12 01:37:47 -07:00
|
|
|
'''
|
2011-04-12 01:32:07 -07:00
|
|
|
|
2011-06-12 01:23:15 -07:00
|
|
|
import os
|
2011-05-31 16:23:06 -07:00
|
|
|
import sys
|
2011-04-12 01:32:07 -07:00
|
|
|
|
2011-06-20 23:51:57 -07:00
|
|
|
import sip
|
2011-06-03 21:15:59 -07:00
|
|
|
from PyQt4.QtCore import pyqtProperty, pyqtSlot, QObject, \
|
2011-06-12 00:38:52 -07:00
|
|
|
QFile
|
2011-06-03 21:15:59 -07:00
|
|
|
from PyQt4.QtGui import QApplication
|
|
|
|
|
from PyQt4.QtNetwork import QNetworkProxy, QNetworkProxyFactory
|
|
|
|
|
|
2011-06-12 01:23:15 -07:00
|
|
|
from utils import version_major, version_minor, version_patch, \
|
|
|
|
|
injectJsInFrame
|
2011-05-06 23:46:51 -07:00
|
|
|
from plugincontroller import Bunch, do_action
|
2011-04-12 01:32:07 -07:00
|
|
|
from webpage import WebPage
|
2011-04-12 01:38:19 -07:00
|
|
|
from networkaccessmanager import NetworkAccessManager
|
2011-04-12 01:32:07 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Phantom(QObject):
|
2011-05-03 16:43:31 -07:00
|
|
|
def __init__(self, args, parent=None):
|
2011-04-12 01:32:07 -07:00
|
|
|
QObject.__init__(self, parent)
|
|
|
|
|
|
|
|
|
|
# variable declarations
|
2011-05-31 16:23:06 -07:00
|
|
|
self.m_defaultPageSettings = {}
|
2011-04-12 01:34:20 -07:00
|
|
|
self.m_verbose = args.verbose
|
2011-04-12 01:32:07 -07:00
|
|
|
self.m_page = WebPage(self)
|
2011-05-31 23:06:30 -07:00
|
|
|
self.m_returnValue = 0
|
2011-05-31 23:17:50 -07:00
|
|
|
self.m_terminated = False
|
2011-04-12 01:32:07 -07:00
|
|
|
# setup the values from args
|
2011-06-12 01:23:15 -07:00
|
|
|
self.m_scriptFile = args.script
|
2011-04-13 08:12:43 -07:00
|
|
|
self.m_args = args.script_args
|
2011-04-12 01:32:07 -07:00
|
|
|
|
2011-05-06 23:46:51 -07:00
|
|
|
do_action('PhantomInitPre', Bunch(locals()))
|
2011-05-04 11:11:37 -07:00
|
|
|
|
2011-04-12 01:32:07 -07:00
|
|
|
if not args.proxy:
|
|
|
|
|
QNetworkProxyFactory.setUseSystemConfiguration(True)
|
|
|
|
|
else:
|
|
|
|
|
proxy = QNetworkProxy(QNetworkProxy.HttpProxy, args.proxy[0], int(args.proxy[1]))
|
|
|
|
|
QNetworkProxy.setApplicationProxy(proxy)
|
|
|
|
|
|
2011-05-31 16:23:06 -07:00
|
|
|
# Provide WebPage with a non-standard Network Access Manager
|
|
|
|
|
self.m_netAccessMan = NetworkAccessManager(args.disk_cache, args.ignore_ssl_errors, self)
|
|
|
|
|
self.m_page.setNetworkAccessManager(self.m_netAccessMan)
|
2011-04-12 01:32:07 -07:00
|
|
|
|
2011-05-31 16:23:06 -07:00
|
|
|
self.m_page.javaScriptConsoleMessageSent.connect(self.printConsoleMessage)
|
2011-04-12 01:32:07 -07:00
|
|
|
|
2011-06-08 03:07:46 -07:00
|
|
|
self.m_defaultPageSettings['loadImages'] = args.load_images
|
|
|
|
|
self.m_defaultPageSettings['loadPlugins'] = args.load_plugins
|
2011-05-31 16:23:06 -07:00
|
|
|
self.m_defaultPageSettings['userAgent'] = self.m_page.userAgent()
|
|
|
|
|
self.m_page.applySettings(self.m_defaultPageSettings)
|
2011-04-12 01:38:19 -07:00
|
|
|
|
2011-06-17 20:17:58 -07:00
|
|
|
self.libraryPath = os.path.dirname(os.path.abspath(self.m_scriptFile))
|
2011-06-12 14:38:34 -07:00
|
|
|
|
2011-04-12 01:32:07 -07:00
|
|
|
# inject our properties and slots into javascript
|
2011-05-31 16:23:06 -07:00
|
|
|
self.m_page.mainFrame().addToJavaScriptWindowObject('phantom', self)
|
|
|
|
|
|
|
|
|
|
bootstrap = QFile(':/bootstrap.js')
|
|
|
|
|
if not bootstrap.open(QFile.ReadOnly):
|
2011-06-12 00:38:52 -07:00
|
|
|
sys.exit('Can not bootstrap!')
|
2011-05-31 16:23:06 -07:00
|
|
|
bootstrapper = str(bootstrap.readAll())
|
|
|
|
|
bootstrap.close()
|
|
|
|
|
if not bootstrapper:
|
2011-06-12 00:38:52 -07:00
|
|
|
sys.exit('Can not bootstrap!')
|
2011-05-31 16:23:06 -07:00
|
|
|
self.m_page.mainFrame().evaluateJavaScript(bootstrapper)
|
2011-04-12 01:32:07 -07:00
|
|
|
|
2011-05-06 23:46:51 -07:00
|
|
|
do_action('PhantomInitPost', Bunch(locals()))
|
2011-05-04 10:47:28 -07:00
|
|
|
|
2011-04-12 01:32:07 -07:00
|
|
|
def execute(self):
|
2011-06-19 01:18:26 -07:00
|
|
|
injectJsInFrame(self.m_scriptFile, os.path.dirname(os.path.abspath(__file__)), self.m_page.mainFrame(), True)
|
2011-05-31 23:17:50 -07:00
|
|
|
return not self.m_terminated
|
2011-04-12 01:32:07 -07:00
|
|
|
|
2011-06-19 22:33:48 -07:00
|
|
|
def printConsoleMessage(self, message, lineNumber, source):
|
|
|
|
|
if source:
|
|
|
|
|
message = '%s:%d %s' % (source, lineNumber, message)
|
|
|
|
|
print message
|
2011-04-12 01:32:07 -07:00
|
|
|
|
|
|
|
|
def returnValue(self):
|
|
|
|
|
return self.m_returnValue
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Properties and methods exposed to JavaScript
|
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
@pyqtProperty('QStringList')
|
|
|
|
|
def args(self):
|
|
|
|
|
return self.m_args
|
|
|
|
|
|
2011-05-31 16:23:06 -07:00
|
|
|
@pyqtSlot(result=WebPage)
|
|
|
|
|
def createWebPage(self):
|
|
|
|
|
page = WebPage(self)
|
|
|
|
|
page.applySettings(self.m_defaultPageSettings)
|
|
|
|
|
page.setNetworkAccessManager(self.m_netAccessMan)
|
2011-06-17 20:17:58 -07:00
|
|
|
page.libraryPath = os.path.dirname(os.path.abspath(self.m_scriptFile))
|
2011-05-31 16:23:06 -07:00
|
|
|
return page
|
|
|
|
|
|
2011-04-12 01:32:07 -07:00
|
|
|
@pyqtProperty('QVariantMap')
|
2011-05-31 16:23:06 -07:00
|
|
|
def defaultPageSettings(self):
|
|
|
|
|
return self.m_defaultPageSettings
|
2011-04-12 01:32:07 -07:00
|
|
|
|
2011-06-20 10:14:08 -07:00
|
|
|
@pyqtSlot()
|
|
|
|
|
@pyqtSlot(int)
|
|
|
|
|
def exit(self, code=0):
|
|
|
|
|
self.m_terminated = True
|
|
|
|
|
self.m_returnValue = code
|
2011-06-20 23:51:57 -07:00
|
|
|
|
|
|
|
|
# stop javascript execution; delete C++ object first,
|
|
|
|
|
# then delete the Python reference
|
|
|
|
|
sip.delete(self.m_page)
|
|
|
|
|
del self.m_page
|
|
|
|
|
|
2011-06-20 10:14:08 -07:00
|
|
|
QApplication.instance().exit(code)
|
|
|
|
|
|
2011-06-12 01:23:15 -07:00
|
|
|
@pyqtSlot(str, result=bool)
|
|
|
|
|
def injectJs(self, filePath):
|
2011-06-17 20:17:58 -07:00
|
|
|
return injectJsInFrame(filePath, self.libraryPath, self.m_page.mainFrame())
|
2011-06-12 14:38:34 -07:00
|
|
|
|
|
|
|
|
@pyqtProperty(str)
|
2011-06-17 20:17:58 -07:00
|
|
|
def libraryPath(self):
|
|
|
|
|
return self.m_page.libraryPath
|
2011-06-12 14:38:34 -07:00
|
|
|
|
2011-06-17 20:17:58 -07:00
|
|
|
@libraryPath.setter
|
|
|
|
|
def libraryPath(self, dirPath):
|
|
|
|
|
self.m_page.libraryPath = dirPath
|
2011-06-12 01:23:15 -07:00
|
|
|
|
2011-06-15 14:30:31 -07:00
|
|
|
@pyqtProperty(str)
|
|
|
|
|
def scriptName(self):
|
|
|
|
|
return os.path.basename(self.m_scriptFile)
|
|
|
|
|
|
2011-04-12 01:32:07 -07:00
|
|
|
@pyqtProperty('QVariantMap')
|
|
|
|
|
def version(self):
|
|
|
|
|
version = {
|
|
|
|
|
'major': version_major,
|
|
|
|
|
'minor': version_minor,
|
|
|
|
|
'patch': version_patch
|
|
|
|
|
}
|
|
|
|
|
return version
|
|
|
|
|
|
2011-05-06 23:46:51 -07:00
|
|
|
do_action('Phantom', Bunch(locals()))
|