Exemplo n.º 1
0
def log_splash_version():
    import twisted
    from twisted.python import log
    import sip
    from PyQt4.QtCore import PYQT_VERSION_STR, QT_VERSION_STR
    from PyQt4.QtWebKit import qWebKitVersion
    from splash import lua

    log.msg("Splash version: %s" % __version__)

    versions = [
        "Qt %s" % QT_VERSION_STR,
        "PyQt %s" % PYQT_VERSION_STR,
        "WebKit %s" % qWebKitVersion(),
        "sip %s" % sip.SIP_VERSION_STR,
        "Twisted %s" % twisted.version.short(),
    ]

    if lua.is_supported():
        versions.append(lua.get_version())

    log.msg(", ".join(versions))
Exemplo n.º 2
0
def log_splash_version():
    import twisted
    from twisted.python import log
    from splash import lua
    from splash.qtutils import get_versions

    log.msg("Splash version: %s" % __version__)

    verdict = get_versions()
    versions = [
        "Qt %s" % verdict['qt'],
        "PyQt %s" % verdict['pyqt'],
        "WebKit %s" % verdict['webkit'],
        "sip %s" % verdict['sip'],
        "Twisted %s" % twisted.version.short(),
    ]

    if lua.is_supported():
        versions.append(lua.get_version())

    log.msg(", ".join(versions))
    log.msg("Python %s" % sys.version.replace("\n", ""))
Exemplo n.º 3
0
def log_splash_version():
    import twisted
    from twisted.python import log
    from splash import lua
    from splash.qtutils import get_versions

    log.msg("Splash version: %s" % __version__)

    verdict = get_versions()
    versions = [
        "Qt %s" % verdict['qt'],
        "PyQt %s" % verdict['pyqt'],
        "WebKit %s" % verdict['webkit'],
        "sip %s" % verdict['sip'],
        "Twisted %s" % twisted.version.short(),
    ]

    if lua.is_supported():
        versions.append(lua.get_version())

    log.msg(", ".join(versions))
    log.msg("Python %s" % sys.version.replace("\n", ""))
Exemplo n.º 4
0
def log_splash_version():
    import twisted
    from twisted.python import log
    import sip
    from PyQt4.QtCore import PYQT_VERSION_STR, QT_VERSION_STR
    from PyQt4.QtWebKit import qWebKitVersion
    from splash import lua

    log.msg("Splash version: %s" % __version__)

    versions = [
        "Qt %s" % QT_VERSION_STR,
        "PyQt %s" % PYQT_VERSION_STR,
        "WebKit %s" % qWebKitVersion(),
        "sip %s" % sip.SIP_VERSION_STR,
        "Twisted %s" % twisted.version.short(),
    ]

    if lua.is_supported():
        versions.append(lua.get_version())

    log.msg(", ".join(versions))
Exemplo n.º 5
0
class SplashKernel(Kernel):
    implementation = 'Splash'
    implementation_version = splash.__version__
    language = 'Lua'
    language_version = get_version()
    language_info = {
        'name': 'Splash',
        'mimetype': 'application/x-lua',
        'display_name': 'Splash',
        'language': 'lua',
        'codemirror_mode': {
            "name": "text/x-lua",
        },
        'file_extension': '.lua',
        'pygments_lexer': 'lua',
        'version': get_version(),
    }
    banner = "Splash kernel - write browser automation scripts interactively"
    help_links = [
        {
            'text':
            "Splash Tutorial",
            'url':
            'http://splash.readthedocs.org/en/latest/scripting-tutorial.html'
        },
        {
            'text': "Splash Reference",
            'url': 'http://splash.readthedocs.org/en/latest/scripting-ref.html'
        },
        {
            'text': "Programming in Lua",
            'url': 'http://www.lua.org/pil/contents.html'
        },
        {
            'text': "Lua 5.2 Manual",
            'url': 'http://www.lua.org/manual/5.2/'
        },
    ]

    sandboxed = False

    def __init__(self, **kwargs):
        super(SplashKernel, self).__init__(**kwargs)
        self.tab = init_browser(SplashKernel.network_manager_factory)

        self.lua = SplashLuaRuntime(self.sandboxed, "", ())
        self.exceptions = StoredExceptions()
        self.splash = Splash(lua=self.lua,
                             exceptions=self.exceptions,
                             tab=self.tab)
        self.lua.add_to_globals("splash", self.splash.get_wrapped())
        self.extras = Extras(self.lua, self.exceptions)
        self.extras.inject_to_globals()

        self.runner = DeferredSplashRunner(self.lua, self.splash,
                                           self.sandboxed)  #, self.log_msg)
        self.completer = Completer(self.lua)
        self.inspector = Inspector(self.lua)
        #
        # try:
        #     sys.stdout.write = self._print
        #     sys.stderr.write = self._print
        # except:
        #     pass # Can't change stdout

    def send_execute_reply(self, stream, ident, parent, md, reply_content):
        def done(result):
            reply, result, ct = result
            if result:
                data = {
                    'text/plain':
                    result
                    if isinstance(result, six.text_type) else str(result),
                }
                if isinstance(result, BinaryCapsule):
                    if result.content_type in {'image/png', 'image/jpeg'}:
                        data[result.content_type] = result.as_b64()
                self._publish_execute_result(parent, data, {},
                                             self.execution_count)

            super(SplashKernel,
                  self).send_execute_reply(stream, ident, parent, md, reply)

        assert isinstance(reply_content, defer.Deferred)
        reply_content.addCallback(done)

    def do_execute(self,
                   code,
                   silent,
                   store_history=True,
                   user_expressions=None,
                   allow_stdin=False):
        def success(res):
            result, content_type, headers, status_code = res
            reply = {
                'status': 'ok',
                'execution_count': self.execution_count,
                'payload': [],
                'user_expressions': {},
            }
            return reply, result, content_type or 'text/plain'

        def error(failure):
            text = "<unknown error>"
            try:
                failure.raiseException()
            except Exception as e:
                text = error_repr(e)
            reply = {
                'status': 'error',
                'execution_count': self.execution_count,
                'ename': '',
                'evalue': text,
                'traceback': []
            }
            return reply, text, 'text/plain'

        try:
            try:
                # XXX: this ugly formatting is important for exception
                # line numbers to be displayed properly!
                lua_source = 'local repr = require("repr"); function main(splash) return repr(%s) end' % code
                main_coro = self._get_main(lua_source)
            except lupa.LuaSyntaxError:
                try:
                    lines = code.splitlines(False)
                    lua_source = '''local repr = require("repr"); function main(splash) %s
                        return repr(%s)
                    end
                    ''' % ("\n".join(lines[:-1]), lines[-1])
                    main_coro = self._get_main(lua_source)
                except lupa.LuaSyntaxError:
                    lua_source = "function main(splash) %s end" % code
                    main_coro = self._get_main(lua_source)

        except (lupa.LuaSyntaxError, lupa.LuaError) as e:
            d = defer.Deferred()
            d.addCallbacks(success, error)
            d.errback(e)
            return d
        except Exception:
            d = defer.Deferred()
            d.addCallbacks(success, error)
            d.errback()
            return d

        d = self.runner.run(main_coro)
        d.addCallbacks(success, error)
        return d

    def do_complete(self, code, cursor_pos):
        return self.completer.complete(code, cursor_pos)

    def do_inspect(self, code, cursor_pos, detail_level=0):
        return self.inspector.help(code, cursor_pos, detail_level)

    def _publish_execute_result(self, parent, data, metadata, execution_count):
        msg = {
            u'data': data,
            u'metadata': metadata,
            u'execution_count': execution_count
        }
        self.session.send(self.iopub_socket,
                          u'execute_result',
                          msg,
                          parent=parent,
                          ident=self._topic('execute_result'))

    def log_msg(self, text, min_level=2):
        self._print(text + "\n")

    def _print(self, message):
        stream_content = {
            'name': 'stdout',
            'text': message,
            'metadata': dict()
        }
        self.log.debug('Write: %s' % message)
        self.send_response(self.iopub_socket, 'stream', stream_content)

    def _get_main(self, lua_source):
        if self.sandboxed:
            main, env = get_main_sandboxed(self.lua, lua_source)
        else:
            main, env = get_main(self.lua, lua_source)
        return self.lua.create_coroutine(main)