/usr/share/pyshared/pyphantomjs/arguments.py is in python-pyphantomjs 1.4.0+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | '''
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/>.
'''
import argparse
import codecs
import os
import sys
from PyQt4.QtCore import qInstallMsgHandler, QObject, qWarning
from PyQt4.QtNetwork import QNetworkProxy
from PyQt4.QtWebKit import QWebPage
from __init__ import __version__
from plugincontroller import do_action
from utils import debug, MessageHandler, QPyFile
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__
defaults = {
'cookiesFile': None,
'debug': None,
'diskCache': False,
'ignoreSslErrors': False,
'loadImages': True,
'loadPlugins': False,
'localToRemoteUrlAccessEnabled': False,
'maxDiskCacheSize': -1,
'outputEncoding': 'System',
'proxy': None,
'proxyType': QNetworkProxy.HttpProxy,
'scriptEncoding': 'utf-8',
'verbose': False
}
def argParser():
class YesOrNoAction(argparse.Action):
'''Converts yes or no arguments to True/False respectively'''
def __call__(self, parser, namespace, value, option_string=None):
answer = True if value == 'yes' else False
setattr(namespace, self.dest, answer)
def proxyType(type_):
if type_ == QNetworkProxy.HttpProxy:
return 'http'
elif type_ == QNetworkProxy.Socks5Proxy:
return 'socks5'
yesOrNo = lambda d: 'yes' if d else 'no'
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='?',
help='The script to execute, and any args to pass to it'
)
parser.add_argument('-v', '--version',
action='version', version=license,
help="show this program's version and license"
)
program = parser.add_argument_group('program options')
script = parser.add_argument_group('script options')
debug = parser.add_argument_group('debug options')
program.add_argument('--config', metavar='/path/to/config',
help='Specifies path to a JSON-formatted config file'
)
program.add_argument('--disk-cache', default=defaults['diskCache'], action=YesOrNoAction,
choices=['yes', 'no'],
help='Enable disk cache (default: %s)' % yesOrNo(defaults['diskCache'])
)
program.add_argument('--ignore-ssl-errors', default=defaults['ignoreSslErrors'], action=YesOrNoAction,
choices=['yes', 'no'],
help='Ignore SSL errors (default: %s)' % yesOrNo(defaults['ignoreSslErrors'])
)
program.add_argument('--max-disk-cache-size', default=defaults['maxDiskCacheSize'], metavar='size', type=int,
help='Limits the size of disk cache (in KB)'
)
program.add_argument('--output-encoding', default=defaults['outputEncoding'], metavar='encoding',
help='Sets the encoding used for terminal output (default: %(default)s)'
)
program.add_argument('--proxy', metavar='address:port',
help='Set the network proxy'
)
program.add_argument('--proxy-type', default=defaults['proxyType'], metavar='type',
help='Set the network proxy type (default: %s)' % proxyType(defaults['proxyType'])
)
program.add_argument('--script-encoding', default=defaults['scriptEncoding'], metavar='encoding',
help='Sets the encoding used for scripts (default: %(default)s)'
)
script.add_argument('--cookies-file', metavar='/path/to/cookies.txt',
help='Sets the file name to store the persistent cookies'
)
script.add_argument('--load-images', default=defaults['loadImages'], action=YesOrNoAction,
choices=['yes', 'no'],
help='Load all inlined images (default: %s)' % yesOrNo(defaults['loadImages'])
)
script.add_argument('--load-plugins', default=defaults['loadPlugins'], action=YesOrNoAction,
choices=['yes', 'no'],
help='Load all plugins (i.e. Flash, Silverlight, ...) (default: %s)' % yesOrNo(defaults['loadPlugins'])
)
script.add_argument('--local-to-remote-url-access', default=defaults['localToRemoteUrlAccessEnabled'], action=YesOrNoAction,
choices=['yes', 'no'],
help='Local content can access remote URL (default: %s)' % yesOrNo(defaults['localToRemoteUrlAccessEnabled'])
)
debug.add_argument('--debug', choices=['exception', 'program'], metavar='option',
help=('Debug the program with pdb\n'
' exception : Start debugger when program hits exception\n'
' program : Start the program with the debugger enabled')
)
debug.add_argument('--verbose', action='store_true',
help='Show verbose debug messages'
)
do_action('ArgParser')
return parser
def parseArgs(app, args):
# Handle all command-line options
p = argParser()
arg_data = p.parse_known_args(args)
args = arg_data[0]
args.script_args = arg_data[1]
# convert script args to unicode
for i, arg in enumerate(args.script_args):
args.script_args[i] = unicode(arg, 'utf-8')
# register an alternative Message Handler
messageHandler = MessageHandler(args.verbose)
qInstallMsgHandler(messageHandler.process)
file_check = (args.cookies_file, args.config)
for file_ in file_check:
if file_ is not None and not os.path.exists(file_):
sys.exit("No such file or directory: '%s'" % file_)
if args.config:
config = Config(app, args.config)
# apply settings
for setting in config.settings:
setattr(args, config.settings[setting]['mapping'], config.property(setting))
split_check = (
(args.proxy, 'proxy'),
)
for arg, name in split_check:
if arg:
item = arg.split(':')
if len(item) < 2 or not len(item[1]):
p.print_help()
sys.exit(1)
setattr(args, name, item)
if args.proxy is not None:
if args.proxy_type == 'socks5':
args.proxy_type = QNetworkProxy.Socks5Proxy
do_action('ParseArgs', args)
if args.debug:
debug(args.debug)
# verbose flag got changed on us, so we reload the flag
if messageHandler.verbose != args.verbose:
messageHandler.verbose = args.verbose
if args.script is None:
p.print_help()
sys.exit(1)
if not os.path.exists(args.script):
sys.exit("No such file or directory: '%s'" % args.script)
return args
class Config(QObject):
def __init__(self, parent, jsonFile):
super(Config, self).__init__(parent)
with codecs.open(jsonFile, encoding='utf-8') as f:
json = f.read()
self.settings = {
'cookiesFile': { 'mapping': 'cookies_file', 'default': defaults['cookiesFile'] },
'debug': { 'mapping': 'debug', 'default': defaults['debug'] },
'diskCache': { 'mapping': 'disk_cache', 'default': defaults['diskCache'] },
'ignoreSslErrors': { 'mapping': 'ignore_ssl_errors', 'default': defaults['ignoreSslErrors'] },
'loadImages': { 'mapping': 'load_images', 'default': defaults['loadImages'] },
'loadPlugins': { 'mapping': 'load_plugins', 'default': defaults['loadPlugins'] },
'localToRemoteUrlAccessEnabled': { 'mapping': 'local_to_remote_url_access', 'default': defaults['localToRemoteUrlAccessEnabled'] },
'maxDiskCacheSize': { 'mapping': 'max_disk_cache_size', 'default': defaults['maxDiskCacheSize'] },
'outputEncoding': { 'mapping': 'output_encoding', 'default': defaults['outputEncoding'] },
'proxy': { 'mapping': 'proxy', 'default': defaults['proxy'] },
'proxyType': { 'mapping': 'proxy_type', 'default': defaults['proxyType'] },
'scriptEncoding': { 'mapping': 'script_encoding', 'default': defaults['scriptEncoding'] },
'verbose': { 'mapping': 'verbose', 'default': defaults['verbose'] }
}
do_action('ConfigInit', self.settings)
# generate dynamic properties
for setting in self.settings:
self.setProperty(setting, self.settings[setting]['default'])
# now it's time to parse our JSON file
if not json.lstrip().startswith('{') or not json.rstrip().endswith('}'):
qWarning('Config file MUST be in JSON format!')
return
webPage = QWebPage(self)
with QPyFile(':/configurator.js') as f:
# add config object
webPage.mainFrame().addToJavaScriptWindowObject('config', self)
# apply settings
webPage.mainFrame().evaluateJavaScript(f.readAll().replace('%1', json))
do_action('Config')
|