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
|
2011-09-23 04:40:56 -07:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2011-04-12 01:32:07 -07:00
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2011-09-23 04:40:56 -07:00
|
|
|
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-09-13 14:45:28 -07:00
|
|
|
from PyQt4.QtCore import pyqtProperty, pyqtSlot, QObject
|
2011-06-03 21:15:59 -07:00
|
|
|
from PyQt4.QtGui import QApplication
|
|
|
|
|
from PyQt4.QtNetwork import QNetworkProxy, QNetworkProxyFactory
|
|
|
|
|
|
2011-09-01 13:04:28 -07:00
|
|
|
from __init__ import __version_info__
|
2011-08-31 15:43:29 -07:00
|
|
|
from encoding import Encode
|
2011-09-15 17:18:08 -07:00
|
|
|
from filesystem import FileSystem
|
|
|
|
|
from plugincontroller import do_action
|
2011-11-27 10:56:12 -08:00
|
|
|
from utils import QPyFile
|
|
|
|
|
from webpage import injectJsInFrame, WebPage
|
2011-12-02 16:02:52 -08:00
|
|
|
from webserver import WebServer
|
2011-04-12 01:32:07 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Phantom(QObject):
|
2011-08-18 14:22:19 -07:00
|
|
|
def __init__(self, parent, args):
|
2011-09-12 22:27:04 -07:00
|
|
|
super(Phantom, self).__init__(parent)
|
2011-04-12 01:32:07 -07:00
|
|
|
|
|
|
|
|
# variable declarations
|
2011-05-31 16:23:06 -07:00
|
|
|
self.m_defaultPageSettings = {}
|
2011-06-28 18:39:29 -07:00
|
|
|
self.m_pages = []
|
2011-12-02 16:02:52 -08:00
|
|
|
self.m_servers = []
|
2011-04-12 01:34:20 -07:00
|
|
|
self.m_verbose = args.verbose
|
2011-09-15 16:13:57 -07:00
|
|
|
self.m_page = WebPage(self, args)
|
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-09-15 16:13:57 -07:00
|
|
|
self.app_args = 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-08-31 15:43:29 -07:00
|
|
|
self.m_scriptEncoding = Encode(args.script_encoding, 'utf-8')
|
|
|
|
|
self.m_outputEncoding = Encode(args.output_encoding, sys.stdout.encoding_sys)
|
2011-04-12 01:32:07 -07:00
|
|
|
|
2011-06-28 18:39:29 -07:00
|
|
|
self.m_pages.append(self.m_page)
|
|
|
|
|
|
2011-07-04 02:37:18 -07:00
|
|
|
do_action('PhantomInitPre')
|
2011-05-04 11:11:37 -07:00
|
|
|
|
2011-08-24 14:23:51 -07:00
|
|
|
if args.proxy is None:
|
2011-04-12 01:32:07 -07:00
|
|
|
QNetworkProxyFactory.setUseSystemConfiguration(True)
|
|
|
|
|
else:
|
2011-11-25 11:27:03 -08:00
|
|
|
proxy = QNetworkProxy(args.proxy_type, args.proxy[0], int(args.proxy[1]))
|
2011-04-12 01:32:07 -07:00
|
|
|
QNetworkProxy.setApplicationProxy(proxy)
|
|
|
|
|
|
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-08-20 16:49:36 -07:00
|
|
|
self.m_defaultPageSettings['javascriptEnabled'] = True
|
2011-08-21 14:08:14 -07:00
|
|
|
self.m_defaultPageSettings['XSSAuditingEnabled'] = False
|
2011-05-31 16:23:06 -07:00
|
|
|
self.m_defaultPageSettings['userAgent'] = self.m_page.userAgent()
|
2011-09-07 20:52:22 -07:00
|
|
|
self.m_defaultPageSettings['localToRemoteUrlAccessEnabled'] = args.local_to_remote_url_access
|
2011-05-31 16:23:06 -07:00
|
|
|
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)
|
|
|
|
|
|
2011-09-13 14:45:28 -07:00
|
|
|
with QPyFile(':/bootstrap.js') as f:
|
2011-10-17 22:44:19 -07:00
|
|
|
self.m_page.mainFrame().evaluateJavaScript(f.readAll())
|
2011-04-12 01:32:07 -07:00
|
|
|
|
2011-07-04 02:37:18 -07:00
|
|
|
do_action('PhantomInitPost')
|
2011-05-04 10:47:28 -07:00
|
|
|
|
2011-04-12 01:32:07 -07:00
|
|
|
def execute(self):
|
2011-08-31 15:43:29 -07:00
|
|
|
injectJsInFrame(self.m_scriptFile, self.m_scriptEncoding.encoding, 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-09-08 15:24:50 -07:00
|
|
|
@pyqtSlot(result=FileSystem)
|
|
|
|
|
def createFilesystem(self):
|
2011-09-09 00:52:49 -07:00
|
|
|
return FileSystem(self)
|
2011-09-08 15:24:50 -07:00
|
|
|
|
2011-05-31 16:23:06 -07:00
|
|
|
@pyqtSlot(result=WebPage)
|
|
|
|
|
def createWebPage(self):
|
2011-09-15 16:13:57 -07:00
|
|
|
page = WebPage(self, self.app_args)
|
2011-06-28 18:39:29 -07:00
|
|
|
self.m_pages.append(page)
|
2011-05-31 16:23:06 -07:00
|
|
|
page.applySettings(self.m_defaultPageSettings)
|
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-12-02 16:02:52 -08:00
|
|
|
@pyqtSlot(result=WebServer)
|
|
|
|
|
def createWebServer(self):
|
|
|
|
|
server = WebServer(self)
|
|
|
|
|
self.m_servers.append(server)
|
|
|
|
|
# :TODO:
|
|
|
|
|
# page.applySettings(self.m_defaultPageSettings)
|
|
|
|
|
# page.libraryPath = os.path.dirname(os.path.abspath(self.m_scriptFile)
|
|
|
|
|
return server
|
|
|
|
|
|
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
|
|
|
|
2011-07-02 17:25:04 -07:00
|
|
|
# stop javascript execution in start script;
|
2011-12-02 16:02:52 -08:00
|
|
|
# release all pages, then clear
|
2011-07-02 17:25:04 -07:00
|
|
|
# the page list, and empty the Phantom page
|
2011-06-28 18:39:29 -07:00
|
|
|
for page in self.m_pages:
|
2011-12-02 16:02:52 -08:00
|
|
|
page.release()
|
2011-07-02 17:25:04 -07:00
|
|
|
del self.m_pages[:]
|
|
|
|
|
self.m_page = None
|
2011-06-20 23:51:57 -07:00
|
|
|
|
2011-12-02 16:02:52 -08:00
|
|
|
for server in self.m_servers:
|
|
|
|
|
server.release()
|
2011-12-03 17:16:10 -08:00
|
|
|
del self.m_servers[:] # not needed, but I'd rather be thorough
|
2011-12-02 16:02:52 -08:00
|
|
|
|
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-08-31 15:43:29 -07:00
|
|
|
return injectJsInFrame(filePath, self.m_scriptEncoding.encoding, self.libraryPath, self.m_page.mainFrame())
|
2011-06-12 14:38:34 -07:00
|
|
|
|
2011-09-13 14:45:28 -07:00
|
|
|
@pyqtSlot(str, result=str)
|
|
|
|
|
def loadModuleSource(self, name):
|
|
|
|
|
moduleSourceFilePath = ':/modules/%s.js' % name
|
|
|
|
|
|
|
|
|
|
with QPyFile(moduleSourceFilePath) as f:
|
2011-10-17 22:44:19 -07:00
|
|
|
return f.readAll()
|
2011-09-13 14:45:28 -07:00
|
|
|
|
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-08-24 16:39:04 -07:00
|
|
|
@pyqtProperty(str)
|
|
|
|
|
def outputEncoding(self):
|
2011-08-31 15:43:29 -07:00
|
|
|
return self.m_outputEncoding.name
|
2011-08-24 16:39:04 -07:00
|
|
|
|
|
|
|
|
@outputEncoding.setter
|
|
|
|
|
def outputEncoding(self, encoding):
|
2011-08-31 15:43:29 -07:00
|
|
|
self.m_outputEncoding = Encode(encoding, self.m_outputEncoding.encoding)
|
2011-08-24 16:39:04 -07:00
|
|
|
|
2011-08-31 15:43:29 -07:00
|
|
|
sys.stdout.encoding = self.m_outputEncoding.encoding
|
|
|
|
|
sys.stdout.encode_to = self.m_outputEncoding.encoding
|
|
|
|
|
sys.stderr.encoding = self.m_outputEncoding.encoding
|
|
|
|
|
sys.stdout.encode_to = self.m_outputEncoding.encoding
|
2011-08-24 16:39:04 -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 = {
|
2011-09-01 13:04:28 -07:00
|
|
|
'major': __version_info__[0],
|
|
|
|
|
'minor': __version_info__[1],
|
|
|
|
|
'patch': __version_info__[2]
|
2011-04-12 01:32:07 -07:00
|
|
|
}
|
|
|
|
|
return version
|
|
|
|
|
|
2011-07-04 02:37:18 -07:00
|
|
|
do_action('Phantom')
|