Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #155 from Roejames12/master
Browse files Browse the repository at this point in the history
A ton of updates
  • Loading branch information
ariya committed Sep 15, 2011
2 parents 4bd0ad2 + 26e49cc commit e07307e
Show file tree
Hide file tree
Showing 19 changed files with 1,395 additions and 959 deletions.
2 changes: 1 addition & 1 deletion examples/injectme.coffee
Expand Up @@ -13,7 +13,7 @@ if phantom?
console.log "* Script will 'inject' itself in a page..." console.log "* Script will 'inject' itself in a page..."
page.open "about:blank", (status) -> page.open "about:blank", (status) ->
if status is "success" if status is "success"
if page.injectJs("injectme.js") if page.injectJs("injectme.coffee")
console.log "... done injecting itself!" console.log "... done injecting itself!"
else else
console.log "... fail! Check the $PWD?!" console.log "... fail! Check the $PWD?!"
Expand Down
2 changes: 1 addition & 1 deletion examples/netsniff.coffee
Expand Up @@ -70,7 +70,7 @@ createHAR = (address, title, startTime, resources) ->
id: address id: address
title: title title: title
pageTimings: {} pageTimings: {}
] ]
entries: entries entries: entries


page = require('webpage').create() page = require('webpage').create()
Expand Down
57 changes: 57 additions & 0 deletions python/pyphantomjs/bootstrap.js
@@ -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;
18 changes: 8 additions & 10 deletions python/pyphantomjs/config.py
Expand Up @@ -20,13 +20,15 @@
import sys import sys
import codecs import codecs


from PyQt4.QtCore import QObject, QFile, qWarning from PyQt4.QtCore import QObject, qWarning
from PyQt4.QtWebKit import QWebPage from PyQt4.QtWebKit import QWebPage


from utils import QPyFile



class Config(QObject): class Config(QObject):
def __init__(self, parent, jsonFile): def __init__(self, parent, jsonFile):
QObject.__init__(self, parent) super(Config, self).__init__(parent)


with codecs.open(jsonFile, encoding='utf-8') as fd: with codecs.open(jsonFile, encoding='utf-8') as fd:
json = fd.read() json = fd.read()
Expand All @@ -38,7 +40,8 @@ def __init__(self, parent, jsonFile):
'ignoreSslErrors': { 'mapping': 'ignore_ssl_errors', 'default': False }, 'ignoreSslErrors': { 'mapping': 'ignore_ssl_errors', 'default': False },
'loadImages': { 'mapping': 'load_images', 'default': True }, 'loadImages': { 'mapping': 'load_images', 'default': True },
'loadPlugins': { 'mapping': 'load_plugins', 'default': False }, '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' }, 'outputEncoding': { 'mapping': 'output_encoding', 'default': 'System' },
'proxy': { 'mapping': 'proxy', 'default': None }, 'proxy': { 'mapping': 'proxy', 'default': None },
'scriptEncoding': { 'mapping': 'script_encoding', 'default': 'utf-8' }, 'scriptEncoding': { 'mapping': 'script_encoding', 'default': 'utf-8' },
Expand All @@ -54,13 +57,8 @@ def __init__(self, parent, jsonFile):
qWarning('Config file MUST be in JSON format!') qWarning('Config file MUST be in JSON format!')
return return


file_ = QFile(':/configurator.js') with QPyFile(':/configurator.js') as f:
if not file_.open(QFile.ReadOnly): configurator = f.readAll().data()
sys.exit('Unable to load JSON configurator!')
configurator = str(file_.readAll())
file_.close()
if not configurator:
sys.exit('Unable to set-up JSON configurator!')


webPage = QWebPage(self) webPage = QWebPage(self)


Expand Down
2 changes: 1 addition & 1 deletion python/pyphantomjs/cookiejar.py
Expand Up @@ -23,7 +23,7 @@


class CookieJar(QNetworkCookieJar): class CookieJar(QNetworkCookieJar):
def __init__(self, parent, cookieFile): def __init__(self, parent, cookieFile):
QNetworkCookieJar.__init__(self, parent) super(CookieJar, self).__init__(parent)


self.m_cookieFile = cookieFile self.m_cookieFile = cookieFile


Expand Down
14 changes: 7 additions & 7 deletions python/pyphantomjs/csconverter.py
Expand Up @@ -19,10 +19,12 @@


import sys import sys


from PyQt4.QtCore import QObject, QFile from PyQt4.QtCore import QObject
from PyQt4.QtGui import QApplication from PyQt4.QtGui import QApplication
from PyQt4.QtWebKit import QWebPage from PyQt4.QtWebKit import QWebPage


from utils import QPyFile



class CSConverter(QObject): class CSConverter(QObject):
_instance = None _instance = None
Expand All @@ -32,14 +34,12 @@ def __new__(cls, *args, **kwargs):
return cls._instance return cls._instance


def __init__(self): def __init__(self):
QObject.__init__(self, QApplication.instance()) super(CSConverter, self).__init__(QApplication.instance())

self.m_webPage = QWebPage(self) self.m_webPage = QWebPage(self)


converter = QFile(':/resources/coffee-script.js') with QPyFile(':/resources/coffee-script.js') as f:
if not converter.open(QFile.ReadOnly): script = f.readAll().data()
sys.exit('CoffeeScript compiler is not available!')
script = str(converter.readAll())
converter.close()


self.m_webPage.mainFrame().evaluateJavaScript(script) self.m_webPage.mainFrame().evaluateJavaScript(script)
self.m_webPage.mainFrame().addToJavaScriptWindowObject('converter', self) self.m_webPage.mainFrame().addToJavaScriptWindowObject('converter', self)
Expand Down
10 changes: 8 additions & 2 deletions python/pyphantomjs/filesystem.py
Expand Up @@ -35,7 +35,7 @@


class File(QObject): class File(QObject):
def __init__(self, parent, openfile): def __init__(self, parent, openfile):
QObject.__init__(self, parent) super(File, self).__init__(parent)


self.m_file = openfile self.m_file = openfile


Expand Down Expand Up @@ -111,8 +111,14 @@ def writeLine(self, data):




class FileSystem(QObject): 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): def __init__(self, parent):
QObject.__init__(self, parent) super(FileSystem, self).__init__(parent)


do_action('FileSystemInit') do_action('FileSystemInit')


Expand Down
162 changes: 162 additions & 0 deletions python/pyphantomjs/modules/fs.js
@@ -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');
};

0 comments on commit e07307e

Please sign in to comment.