Files

174 lines
5.6 KiB
Python
Raw Permalink Normal View History

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
import os
2011-06-01 00:26:06 -07:00
import sys
import codecs
2011-06-01 00:26:06 -07:00
import argparse
from PyQt4.QtCore import QDateTime, Qt, QtDebugMsg, QtWarningMsg, \
QtCriticalMsg, QtFatalMsg, qWarning
2011-06-03 21:15:59 -07:00
from csconverter import CSConverter
from plugincontroller import Bunch, do_action
2011-04-12 01:32:07 -07:00
2011-05-31 16:23:06 -07:00
version_major, version_minor, version_patch = (1, 2, 0)
2011-04-12 01:32:07 -07:00
version = '%d.%d.%d' % (version_major, version_minor, version_patch)
license = '''
PyPhantomJS Version %s
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/>.
''' % version
2011-05-31 16:23:06 -07:00
2011-04-12 01:32:07 -07:00
def argParser():
parser = argparse.ArgumentParser(
description='Minimalistic headless WebKit-based JavaScript-driven tool',
usage='%(prog)s [options] script.[js|coffee] [script argument [script argument ...]]',
formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument('script', metavar='script.[js|coffee]', nargs='?',
2011-04-12 19:36:25 -07:00
help='The script to execute, and any args to pass to it'
)
2011-05-03 16:49:13 -07:00
parser.add_argument('--disk-cache', default='no',
choices=['yes', 'no'],
help='Enable disk cache (default: %(default)s)'
)
2011-05-03 16:49:34 -07:00
parser.add_argument('--ignore-ssl-errors', default='no',
choices=['yes', 'no'],
help='Ignore SSL errors (default: %(default)s)'
)
2011-04-12 01:32:07 -07:00
parser.add_argument('--load-images', default='yes',
choices=['yes', 'no'],
help='Load all inlined images (default: %(default)s)'
)
parser.add_argument('--load-plugins', default='no',
choices=['yes', 'no'],
2011-04-27 02:45:41 -07:00
help='Load all plugins (i.e. Flash, Silverlight, ...) (default: %(default)s)'
2011-04-12 01:32:07 -07:00
)
parser.add_argument('--proxy', metavar='address:port',
help='Set the network proxy'
)
parser.add_argument('-v', '--verbose', action='store_true',
help='Show verbose debug messages'
)
2011-04-12 01:32:07 -07:00
parser.add_argument('--version',
action='version', version=license,
help='show this program\'s version and license'
)
do_action('ArgParser', Bunch(locals()))
2011-04-12 01:32:07 -07:00
return parser
2011-05-31 16:23:06 -07:00
coffeeScriptConverter = None
def coffee2js(script):
global coffeeScriptConverter
# We need only one instance of the CSConverter to survive for the whole life of PyPhantomJS
if not coffeeScriptConverter:
coffeeScriptConverter = CSConverter()
return coffeeScriptConverter.convert(script)
def injectJsInFrame(filePath, libraryPath, targetFrame, startingScript=False):
try:
# if file doesn't exist in the CWD, use the lookup
if not os.path.exists(filePath):
2011-06-17 20:17:58 -07:00
filePath = os.path.join(libraryPath, filePath)
with codecs.open(filePath, encoding='utf-8') as f:
script = f.read()
if script.startswith('#!') and not filePath.lower().endswith('.coffee'):
script = '//' + script
2011-06-19 13:37:03 -07:00
if filePath.lower().endswith('.coffee'):
result = coffee2js(script)
if result[0] is False:
if startingScript:
sys.exit(result[1])
else:
qWarning(result[1])
script = ''
else:
script = result[1]
targetFrame.evaluateJavaScript(script)
return True
except IOError:
qWarning('No such file or directory: \'%s\'' % filePath)
return False
class MessageHandler:
def __init__(self, verbose):
self.verbose = verbose
def process(self, msgType, msg):
now = QDateTime.currentDateTime().toString(Qt.ISODate)
if msgType == QtDebugMsg:
if self.verbose:
print '%s [DEBUG] %s' % (now, msg)
elif msgType == QtWarningMsg:
print >> sys.stderr, '%s [WARNING] %s' % (now, msg)
elif msgType == QtCriticalMsg:
print >> sys.stderr, '%s [CRITICAL] %s' % (now, msg)
elif msgType == QtFatalMsg:
print >> sys.stderr, '%s [FATAL] %s' % (now, msg)
2011-05-03 16:45:53 -07:00
2011-05-31 16:23:06 -07:00
2011-05-03 16:45:53 -07:00
class SafeStreamFilter(object):
2011-05-04 16:15:51 -07:00
'''Convert string to something safe'''
2011-05-03 16:45:53 -07:00
def __init__(self, target):
self.target = target
self.encoding = 'utf-8'
self.errors = 'replace'
self.encode_to = self.target.encoding or 'utf-8'
2011-05-03 16:45:53 -07:00
def write(self, s):
s = self.encode(s)
self.target.write(s)
2011-05-04 12:32:17 -07:00
def flush(self):
self.target.flush()
2011-05-03 16:45:53 -07:00
def encode(self, s):
return s.encode(self.encode_to, self.errors).decode(self.encode_to)