Permalink
Browse files

Merge pull request #155 from Roejames12/master

A ton of updates
  • Loading branch information...
2 parents 4bd0ad2 + 26e49cc commit e07307ec1d3911003ba957897d2ab470bc1874db @ariya committed Sep 15, 2011
@@ -13,7 +13,7 @@ if phantom?
console.log "* Script will 'inject' itself in a page..."
page.open "about:blank", (status) ->
if status is "success"
- if page.injectJs("injectme.js")
+ if page.injectJs("injectme.coffee")
console.log "... done injecting itself!"
else
console.log "... fail! Check the $PWD?!"
@@ -70,7 +70,7 @@ createHAR = (address, title, startTime, resources) ->
id: address
title: title
pageTimings: {}
- ]
+ ]
entries: entries
page = require('webpage').create()
@@ -0,0 +1,57 @@
+/*jslint sloppy: true, nomen: true */
+/*global window:true,phantom:true */
+
+/*
+ This file is part of the PhantomJS project from Ofi Labs.
+
+ Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
+ Copyright (C) 2011 Ivan De Marino <ivan.de.marino@gmail.com>
+ Copyright (C) 2011 James Roe <roejames12@hotmail.com>
+ Copyright (C) 2011 execjosh, http://execjosh.blogspot.com
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of the <organization> nor the
+ names of its contributors may be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+function require(name) {
+
+ var code, func, exports;
+
+ if (name === 'webpage' || name === 'fs') {
+ code = phantom.loadModuleSource(name);
+ func = new Function("exports", "window", code);
+ exports = {};
+ if (name === 'fs') {
+ exports = phantom.createFilesystem();
+ }
+ func.call({}, exports, {});
+ return exports;
+ }
+
+ if (typeof exports === 'undefined') {
+ throw 'Unknown module ' + name + ' for require()';
+ }
+}
+
+// Legacy way to use WebPage
+window.WebPage = require('webpage').create;
@@ -20,13 +20,15 @@
import sys
import codecs
-from PyQt4.QtCore import QObject, QFile, qWarning
+from PyQt4.QtCore import QObject, qWarning
from PyQt4.QtWebKit import QWebPage
+from utils import QPyFile
+
class Config(QObject):
def __init__(self, parent, jsonFile):
- QObject.__init__(self, parent)
+ super(Config, self).__init__(parent)
with codecs.open(jsonFile, encoding='utf-8') as fd:
json = fd.read()
@@ -38,7 +40,8 @@ def __init__(self, parent, jsonFile):
'ignoreSslErrors': { 'mapping': 'ignore_ssl_errors', 'default': False },
'loadImages': { 'mapping': 'load_images', 'default': True },
'loadPlugins': { 'mapping': 'load_plugins', 'default': False },
- 'localAccessRemote': { 'mapping': 'local_access_remote', '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' },
@@ -54,13 +57,8 @@ def __init__(self, parent, jsonFile):
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 = str(file_.readAll())
- file_.close()
- if not configurator:
- sys.exit('Unable to set-up JSON configurator!')
+ with QPyFile(':/configurator.js') as f:
+ configurator = f.readAll().data()
webPage = QWebPage(self)
@@ -23,7 +23,7 @@
class CookieJar(QNetworkCookieJar):
def __init__(self, parent, cookieFile):
- QNetworkCookieJar.__init__(self, parent)
+ super(CookieJar, self).__init__(parent)
self.m_cookieFile = cookieFile
@@ -19,10 +19,12 @@
import sys
-from PyQt4.QtCore import QObject, QFile
+from PyQt4.QtCore import QObject
from PyQt4.QtGui import QApplication
from PyQt4.QtWebKit import QWebPage
+from utils import QPyFile
+
class CSConverter(QObject):
_instance = None
@@ -32,14 +34,12 @@ def __new__(cls, *args, **kwargs):
return cls._instance
def __init__(self):
- QObject.__init__(self, QApplication.instance())
+ super(CSConverter, self).__init__(QApplication.instance())
+
self.m_webPage = QWebPage(self)
- converter = QFile(':/resources/coffee-script.js')
- if not converter.open(QFile.ReadOnly):
- sys.exit('CoffeeScript compiler is not available!')
- script = str(converter.readAll())
- converter.close()
+ with QPyFile(':/resources/coffee-script.js') as f:
+ script = f.readAll().data()
self.m_webPage.mainFrame().evaluateJavaScript(script)
self.m_webPage.mainFrame().addToJavaScriptWindowObject('converter', self)
@@ -35,7 +35,7 @@
class File(QObject):
def __init__(self, parent, openfile):
- QObject.__init__(self, parent)
+ super(File, self).__init__(parent)
self.m_file = openfile
@@ -111,8 +111,14 @@ def writeLine(self, data):
class FileSystem(QObject):
+ _instance = None
+ def __new__(cls, *args, **kwargs):
+ if cls._instance is None:
+ cls._instance = super(FileSystem, cls).__new__(cls, *args, **kwargs)
+ return cls._instance
+
def __init__(self, parent):
- QObject.__init__(self, parent)
+ super(FileSystem, self).__init__(parent)
do_action('FileSystemInit')
@@ -0,0 +1,162 @@
+/*jslint sloppy: true, nomen: true */
+/*global exports:true */
+
+/*
+ This file is part of the PhantomJS project from Ofi Labs.
+
+ Copyright (C) 2011 Ivan De Marino <ivan.de.marino@gmail.com>
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of the <organization> nor the
+ names of its contributors may be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+// JavaScript "shim" to throw exceptions in case a critical operation fails.
+
+/** Open and return a "file" object.
+ * It will throw exception if it fails.
+ *
+ * @param path Path of the file to open
+ * @param mode Open Mode. A string made of 'r', 'w', 'a/+' characters.
+ * @return "file" object
+ */
+exports.open = function (path, mode) {
+ var file = exports._open(path, mode);
+ if (file) {
+ return file;
+ }
+ throw "Unable to open file '" + path + "'";
+};
+
+/** Open, read and return content of a file.
+ * It will throw an exception if it fails.
+ *
+ * @param path Path of the file to read from
+ * @return file content
+ */
+exports.read = function (path) {
+ var f = exports.open(path, 'r'),
+ content = f.read();
+
+ f.close();
+ return content;
+};
+
+/** Open and write content to a file
+ * It will throw an exception if it fails.
+ *
+ * @param path Path of the file to read from
+ * @param content Content to write to the file
+ * @param mode Open Mode. A string made of 'w' or 'a / +' characters.
+ */
+exports.write = function (path, content, mode) {
+ var f = exports.open(path, mode);
+
+ f.write(content);
+ f.close();
+};
+
+/** Return the size of a file, in bytes.
+ * It will throw an exception if it fails.
+ *
+ * @param path Path of the file to read the size of
+ * @return File size in bytes
+ */
+exports.size = function (path) {
+ var size = exports._size(path);
+ if (size !== -1) {
+ return size;
+ }
+ throw "Unable to read file '" + path + "' size";
+};
+
+/** Copy a file.
+ * It will throw an exception if it fails.
+ *
+ * @param source Path of the source file
+ * @param destination Path of the destination file
+ */
+exports.copy = function (source, destination) {
+ if (!exports._copy(source, destination)) {
+ throw "Unable to copy file '" + source + "' at '" + destination + "'";
+ }
+};
+
+/** Copy a directory tree.
+ * It will throw an exception if it fails.
+ *
+ * @param source Path of the source directory tree
+ * @param destination Path of the destination directory tree
+ */
+exports.copyTree = function (source, destination) {
+ if (!exports._copyTree(source, destination)) {
+ throw "Unable to copy directory tree '" + source + "' at '" + destination + "'";
+ }
+};
+
+/** Move a file.
+ * It will throw an exception if it fails.
+ *
+ * @param source Path of the source file
+ * @param destination Path of the destination file
+ */
+exports.move = function (source, destination) {
+ exports.copy(source, destination);
+ exports.remove(source);
+};
+
+/** Removes a file.
+ * It will throw an exception if it fails.
+ *
+ * @param path Path of the file to remove
+ */
+exports.remove = function (path) {
+ if (!exports._remove(path)) {
+ throw "Unable to remove file '" + path + "'";
+ }
+};
+
+/** Removes a directory.
+ * It will throw an exception if it fails.
+ *
+ * @param path Path of the directory to remove
+ */
+exports.removeDirectory = function (path) {
+ if (!exports._removeDirectory(path)) {
+ throw "Unable to remove directory '" + path + "'";
+ }
+};
+
+/** Removes a directory tree.
+ * It will throw an exception if it fails.
+ *
+ * @param path Path of the directory tree to remove
+ */
+exports.removeTree = function (path) {
+ if (!exports._removeTree(path)) {
+ throw "Unable to remove directory tree '" + path + "'";
+ }
+};
+
+exports.touch = function (path) {
+ exports.write(path, "", 'a');
+};
Oops, something went wrong. Retry.

0 comments on commit e07307e

Please sign in to comment.