Files
phantomjs/python/pyphantomjs/config.py
T

72 lines
2.8 KiB
Python

'''
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 sys
import codecs
from PyQt4.QtCore import QObject, QFile, qWarning
from PyQt4.QtWebKit import QWebPage
class Config(QObject):
def __init__(self, parent, jsonFile):
QObject.__init__(self, parent)
with codecs.open(jsonFile, encoding='utf-8') as fd:
json = fd.read()
self.settings = {
'auth': { 'mapping': 'auth', 'default': None },
'cookies': { 'mapping': 'cookies', 'default': None },
'diskCache': { 'mapping': 'disk_cache', 'default': False },
'ignoreSslErrors': { 'mapping': 'ignore_ssl_errors', 'default': False },
'loadImages': { 'mapping': 'load_images', 'default': True },
'loadPlugins': { 'mapping': 'load_plugins', 'default': False },
'localToRemoteUrlAccessEnabled': { 'mapping': 'local_to_remote_url_access', 'default': False },
'maxDiskCacheSize': { 'mapping': 'max_disk_cache_size', 'default': -1 },
'outputEncoding': { 'mapping': 'output_encoding', 'default': 'System' },
'proxy': { 'mapping': 'proxy', 'default': None },
'scriptEncoding': { 'mapping': 'script_encoding', 'default': 'utf-8' },
'verbose': { 'mapping': 'verbose', 'default': False }
}
# 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
file_ = QFile(':/configurator.js')
if not file_.open(QFile.ReadOnly):
sys.exit('Unable to load JSON configurator!')
configurator = file_.readAll().data()
file_.close()
if not configurator:
sys.exit('Unable to set-up JSON configurator!')
webPage = QWebPage(self)
# add config object
webPage.mainFrame().addToJavaScriptWindowObject('config', self)
# apply settings
webPage.mainFrame().evaluateJavaScript(configurator.replace('%1', json))