def qurl2ascii(url): """ Convert QUrl to ASCII text suitable for logging """ url = six.text_type( url.toString()).encode('unicode-escape').decode('ascii') if url.lower().startswith('data:'): return truncated(url, 80, '...[data uri truncated]') return url
def __repr__(self): kwargs = self.kwargs.copy() if 'callback' in kwargs: kwargs['callback'] = '<a callback>' if 'errback' in kwargs: kwargs['errback'] = '<an errback>' kwargs_repr = truncated(repr(kwargs), 400, "...[long kwargs truncated]") return "%s(id=%r, name=%r, kwargs=%s)" % (self.__class__.__name__, self.id, self.name, kwargs_repr)
def __repr__(self): kwargs = self.kwargs.copy() if "callback" in kwargs: kwargs["callback"] = "<a callback>" if "errback" in kwargs: kwargs["errback"] = "<an errback>" kwargs_repr = truncated(repr(kwargs), 400, "...[long kwargs truncated]") return "%s(id=%r, name=%r, kwargs=%s)" % (self.__class__.__name__, self.id, self.name, kwargs_repr)
def truncated_repr(x): return truncated("{!r}".format(x), max_length=400, msg="...[long arguments truncated]")
def dispatch(self, cmd_id, *args): """ Execute the script """ args = args or () def truncated_repr(x): return truncated("{!r}".format(x), max_length=400, msg="...[long arguments truncated]") self.log("[lua_runner] dispatch cmd_id={}".format(cmd_id)) self.log( "[lua_runner] arguments are for command %s, waiting for result of %s" % (cmd_id, self._waiting_for_result_id), min_level=3, ) if cmd_id != self._waiting_for_result_id: self.log("[lua_runner] skipping an out-of-order result {}".format(truncated_repr(args)), min_level=1) return while True: self.log('[lua_runner] entering dispatch/loop body, args={}'.format(truncated_repr(args))) try: if self._is_first_iter: args = None else: is_python_result = (len(args) == 1 and isinstance(args[0], PyResult)) if is_python_result: args = args[0] else: args = PyResult(*args) if self._is_stopped: raise StopIteration # Got arguments from an async command; send them to coroutine # and wait for the next async command. self.log("[lua_runner] send %s" % truncated_repr(args)) as_lua = self.lua.python2lua(args) self.log("[lua_runner] send (lua) %s" % truncated_repr(as_lua)) cmd = self.coro.send(as_lua) # cmd is a next async command if self._is_first_iter: self._is_first_iter = False # If cmd is a synchronous result, prepare it to be passed into # the next coroutine step. args = ensure_tuple(cmd) cmd_repr = truncated(repr(cmd), max_length=400, msg='...[long result truncated]') self.log("[lua_runner] got {}".format(cmd_repr)) self._print_instructions_used() except StopIteration: # "main" coroutine is stopped; # previous result is a final result returned from "main" self.log("[lua_runner] returning result") try: res = self.lua.lua2python(self.result) except ValueError as e: # can't convert result to a Python object raise ScriptError({ "type": ScriptError.BAD_MAIN_ERROR, "message": "'main' returned bad result. {!s}".format( e.args[0] ) }) except lupa.LuaError as lua_ex: # Error converting result to Python # This may happen e.g. if conversion hit sandbox limits self.log("[lua_runner] caught LuaError %r" % lua_ex) info = parse_error_message(lua_ex.args[0]) error = info.get('error', '?') raise ScriptError({ "type": ScriptError.LUA_CONVERT_ERROR, "error": error, "message": "Lua error: {!s}".format(error) }) self._print_instructions_used() self.on_result(res) return except lupa.LuaError as lua_ex: # import traceback # print(traceback.format_exc()) # Lua script raised an error self._print_instructions_used() self.log("[lua_runner] caught LuaError %r" % lua_ex) # this can raise a ScriptError self.on_lua_error(lua_ex) # ScriptError is not raised, construct it ourselves info = parse_error_message(lua_ex.args[0]) info.update({ "type": ScriptError.LUA_ERROR, "message": "Lua error: {!s}".format(lua_ex) }) raise ScriptError(info) if isinstance(cmd, AsyncCommand): cmd.bind(self, next(self._command_ids)) self.log("[lua_runner] executing {!r}".format(cmd)) self._waiting_for_result_id = cmd.id self.on_async_command(cmd) return if isinstance(cmd, PyResult): self.log("[lua_runner] got result {!r}".format(cmd)) else: self.log("[lua_runner] got non-command") self.result = cmd
def dispatch(self, cmd_id, *args): """ Execute the script """ args_repr = truncated("{!r}".format(args), max_length=400, msg="...[long arguments truncated]") self.log("[lua] dispatch cmd_id={}, args={}".format(cmd_id, args_repr)) self.log( "[lua] arguments are for command %s, waiting for result of %s" % (cmd_id, self._waiting_for_result_id), min_level=3, ) if cmd_id != self._waiting_for_result_id: self.log("[lua] skipping an out-of-order result {}".format(args_repr), min_level=1) return while True: try: args = args or None # Got arguments from an async command; send them to coroutine # and wait for the next async command. self.log("[lua] send %s" % args_repr) cmd = self.main_coro.send(args) # cmd is a next async command args = None # don't re-send the same value cmd_repr = truncated(repr(cmd), max_length=400, msg='...[long result truncated]') self.log("[lua] got {}".format(cmd_repr)) self._print_instructions_used() except StopIteration: # "main" coroutine is stopped; # previous result is a final result returned from "main" self.log("[lua] returning result") try: res = self.splash.lua2python(self.result) except ValueError as e: # can't convert result to a Python object -> requets was bad raise ScriptError("'main' returned bad result. {!s}".format(e)) self._print_instructions_used() self.return_result((res, self.splash.result_content_type())) return except lupa.LuaError as e: # Lua script raised an error self._print_instructions_used() self.log("[lua] caught LuaError %r" % e) ex = self.splash.get_real_exception() if ex: self.log("[lua] LuaError is caused by %r" % ex) if isinstance(ex, ScriptError): ex.enrich_from_lua_error(e) raise ex # XXX: are Lua errors bad requests? raise ScriptError("unhandled Lua error: {!s}".format(e)) if isinstance(cmd, _AsyncBrowserCommand): self.log("[lua] executing {!r}".format(cmd)) self._waiting_for_result_id = cmd.id self.splash.run_async_command(cmd) return elif isinstance(cmd, _ImmediateResult): self.log("[lua] got result {!r}".format(cmd)) args = cmd.value continue else: self.log("[lua] got non-command") if isinstance(cmd, tuple): raise ScriptError("'main' function must return a single result") self.result = cmd
def assertStatusCode(self, response, code): msg = (response.status_code, truncated(response.text, 1000), response.url) self.assertEqual(response.status_code, code, msg)
def dispatch(self, cmd_id, *args): """ Execute the script """ args = args or None args_repr = truncated("{!r}".format(args), max_length=400, msg="...[long arguments truncated]") self.log("[lua] dispatch cmd_id={}, args={}".format(cmd_id, args_repr)) self.log( "[lua] arguments are for command %s, waiting for result of %s" % (cmd_id, self._waiting_for_result_id), min_level=3, ) if cmd_id != self._waiting_for_result_id: self.log("[lua] skipping an out-of-order result {}".format(args_repr), min_level=1) return while True: try: args = args or None # Got arguments from an async command; send them to coroutine # and wait for the next async command. self.log("[lua] send %s" % args_repr) cmd = self.coro.send(args) # cmd is a next async command args = None # don't re-send the same value cmd_repr = truncated(repr(cmd), max_length=400, msg="...[long result truncated]") self.log("[lua] got {}".format(cmd_repr)) self._print_instructions_used() except StopIteration: # "main" coroutine is stopped; # previous result is a final result returned from "main" self.log("[lua] returning result") try: res = self.lua.lua2python(self.result) except ValueError as e: # can't convert result to a Python object raise ScriptError("'main' returned bad result. {!s}".format(e)) self._print_instructions_used() self.on_result(res) return except lupa.LuaError as lua_ex: # Lua script raised an error self._print_instructions_used() self.log("[lua] caught LuaError %r" % lua_ex) self.on_lua_error(lua_ex) # this can also raise a ScriptError # XXX: are Lua errors bad requests? raise ScriptError("unhandled Lua error: {!s}".format(lua_ex)) if isinstance(cmd, AsyncCommand): cmd.bind(self, next(self._command_ids)) self.log("[lua] executing {!r}".format(cmd)) self._waiting_for_result_id = cmd.id self.on_async_command(cmd) return elif isinstance(cmd, ImmediateResult): self.log("[lua] got result {!r}".format(cmd)) args = cmd.value continue else: self.log("[lua] got non-command") if isinstance(cmd, tuple): cmd = list(cmd) self.result = cmd
def qurl2ascii(url): """ Convert QUrl to ASCII text suitable for logging """ url = unicode(url.toString()).encode("unicode-escape").decode("ascii") if url.lower().startswith("data:"): return truncated(url, 80, "...[data uri truncated]") return url
def assertStatusCode(self, response, code): msg = (response.status_code, truncated(response.content, 1000)) self.assertEqual(response.status_code, code, msg)
def dispatch(self, cmd_id, *args): """ Execute the script """ args = args or None args_repr = truncated("{!r}".format(args), max_length=400, msg="...[long arguments truncated]") self.log("[lua_runner] dispatch cmd_id={}, args={}".format( cmd_id, args_repr)) self.log( "[lua_runner] arguments are for command %s, waiting for result of %s" % (cmd_id, self._waiting_for_result_id), min_level=3, ) if cmd_id != self._waiting_for_result_id: self.log("[lua_runner] skipping an out-of-order result {}".format( args_repr), min_level=1) return while True: try: args = args or None # Got arguments from an async command; send them to coroutine # and wait for the next async command. self.log("[lua_runner] send %s" % args_repr) cmd = self.coro.send(args) # cmd is a next async command args = None # don't re-send the same value cmd_repr = truncated(repr(cmd), max_length=400, msg='...[long result truncated]') self.log("[lua_runner] got {}".format(cmd_repr)) self._print_instructions_used() except StopIteration: # "main" coroutine is stopped; # previous result is a final result returned from "main" self.log("[lua_runner] returning result") try: res = self.lua.lua2python(self.result) except ValueError as e: # can't convert result to a Python object raise ScriptError({ "type": ScriptError.BAD_MAIN_ERROR, "message": "'main' returned bad result. {!s}".format(e.args[0]) }) self._print_instructions_used() self.on_result(res) return except lupa.LuaError as lua_ex: # Lua script raised an error self._print_instructions_used() self.log("[lua_runner] caught LuaError %r" % lua_ex) # this can raise a ScriptError self.on_lua_error(lua_ex) # ScriptError is not raised, construct it ourselves info = parse_error_message(lua_ex.args[0]) info.update({ "type": ScriptError.LUA_ERROR, "message": "Lua error: {!s}".format(lua_ex) }) raise ScriptError(info) if isinstance(cmd, AsyncCommand): cmd.bind(self, next(self._command_ids)) self.log("[lua_runner] executing {!r}".format(cmd)) self._waiting_for_result_id = cmd.id self.on_async_command(cmd) return elif isinstance(cmd, ImmediateResult): self.log("[lua_runner] got result {!r}".format(cmd)) args = cmd.value continue else: self.log("[lua_runner] got non-command") if isinstance(cmd, tuple): cmd = list(cmd) self.result = cmd
def dispatch(self, cmd_id, *args): """ Execute the script """ args = args or None args_repr = truncated("{!r}".format(args), max_length=400, msg="...[long arguments truncated]") self.log("[lua_runner] dispatch cmd_id={}, args={}".format(cmd_id, args_repr)) self.log( "[lua_runner] arguments are for command %s, waiting for result of %s" % (cmd_id, self._waiting_for_result_id), min_level=3, ) if cmd_id != self._waiting_for_result_id: self.log("[lua_runner] skipping an out-of-order result {}".format(args_repr), min_level=1) return while True: try: args = args or None # Got arguments from an async command; send them to coroutine # and wait for the next async command. self.log("[lua_runner] send %s" % args_repr) cmd = self.coro.send(self.lua.python2lua(args)) # cmd is a next async command args = None # don't re-send the same value cmd_repr = truncated(repr(cmd), max_length=400, msg='...[long result truncated]') self.log("[lua_runner] got {}".format(cmd_repr)) self._print_instructions_used() except StopIteration: # "main" coroutine is stopped; # previous result is a final result returned from "main" self.log("[lua_runner] returning result") try: res = self.lua.lua2python(self.result) except ValueError as e: # can't convert result to a Python object raise ScriptError({ "type": ScriptError.BAD_MAIN_ERROR, "message": "'main' returned bad result. {!s}".format( e.args[0] ) }) self._print_instructions_used() self.on_result(res) return except lupa.LuaError as lua_ex: # import traceback # print(traceback.format_exc()) # Lua script raised an error self._print_instructions_used() self.log("[lua_runner] caught LuaError %r" % lua_ex) # this can raise a ScriptError self.on_lua_error(lua_ex) # ScriptError is not raised, construct it ourselves info = parse_error_message(lua_ex.args[0]) info.update({ "type": ScriptError.LUA_ERROR, "message": "Lua error: {!s}".format(lua_ex) }) raise ScriptError(info) if isinstance(cmd, AsyncCommand): cmd.bind(self, next(self._command_ids)) self.log("[lua_runner] executing {!r}".format(cmd)) self._waiting_for_result_id = cmd.id self.on_async_command(cmd) return elif isinstance(cmd, ImmediateResult): self.log("[lua_runner] got result {!r}".format(cmd)) args = cmd.value continue else: self.log("[lua_runner] got non-command") if isinstance(cmd, tuple): cmd = list(cmd) self.result = cmd
def qurl2ascii(url): """ Convert QUrl to ASCII text suitable for logging """ url = str(url.toString()).encode('unicode-escape').decode('ascii') if url.lower().startswith('data:'): return truncated(url, 80, '...[data uri truncated]') return url
def dispatch(self, cmd_id, *args): """ Execute the script """ args_repr = truncated("{!r}".format(args), max_length=400, msg="...[long arguments truncated]") self.log("[lua] dispatch cmd_id={}, args={}".format(cmd_id, args_repr)) self.log( "[lua] arguments are for command %s, waiting for result of %s" % (cmd_id, self._waiting_for_result_id), min_level=3, ) if cmd_id != self._waiting_for_result_id: self.log( "[lua] skipping an out-of-order result {}".format(args_repr), min_level=1) return while True: try: args = args or None # Got arguments from an async command; send them to coroutine # and wait for the next async command. self.log("[lua] send %s" % args_repr) cmd = self.main_coro.send(args) # cmd is a next async command args = None # don't re-send the same value cmd_repr = truncated(repr(cmd), max_length=400, msg='...[long result truncated]') self.log("[lua] got {}".format(cmd_repr)) self._print_instructions_used() except StopIteration: # "main" coroutine is stopped; # previous result is a final result returned from "main" self.log("[lua] returning result") try: res = self.splash.lua2python(self.result) except ValueError as e: # can't convert result to a Python object raise ScriptError( "'main' returned bad result. {!s}".format(e)) self._print_instructions_used() self.return_result((res, self.splash.result_content_type())) return except lupa.LuaError as e: # Lua script raised an error self._print_instructions_used() self.log("[lua] caught LuaError %r" % e) ex = self.splash.get_real_exception() if ex: self.log("[lua] LuaError is caused by %r" % ex) if isinstance(ex, ScriptError): ex.enrich_from_lua_error(e) raise ex # XXX: are Lua errors bad requests? raise ScriptError("unhandled Lua error: {!s}".format(e)) if isinstance(cmd, _AsyncBrowserCommand): self.log("[lua] executing {!r}".format(cmd)) self._waiting_for_result_id = cmd.id self.splash.run_async_command(cmd) return elif isinstance(cmd, _ImmediateResult): self.log("[lua] got result {!r}".format(cmd)) args = cmd.value continue else: self.log("[lua] got non-command") if isinstance(cmd, tuple): raise ScriptError( "'main' function must return a single result") self.result = cmd
def dispatch(self, cmd_id, *args): """ Execute the script """ args = args or () def truncated_repr(x): return truncated("{!r}".format(x), max_length=400, msg="...[long arguments truncated]") self.log("[lua_runner] dispatch cmd_id={}".format(cmd_id)) self.log( "[lua_runner] arguments are for command %s, waiting for result of %s" % (cmd_id, self._waiting_for_result_id), min_level=3, ) if cmd_id != self._waiting_for_result_id: msg = "[lua_runner] skipping an out-of-order result {}".format( truncated_repr(args)) self.log(msg, min_level=1) if self.strict: raise InternalError(msg) else: return while True: self.log( '[lua_runner] entering dispatch/loop body, args={}'.format( truncated_repr(args))) try: if self._is_first_iter: args = None else: is_python_result = (len(args) == 1 and isinstance(args[0], PyResult)) if is_python_result: args = args[0] else: args = PyResult(*args) if self._is_stopped: raise StopIteration # Got arguments from an async command; send them to coroutine # and wait for the next async command. self.log("[lua_runner] send %s" % truncated_repr(args)) as_lua = self.lua.python2lua(args) self.log("[lua_runner] send (lua) %s" % truncated_repr(as_lua)) cmd = self.coro.send(as_lua) # cmd is a next async command if self._is_first_iter: self._is_first_iter = False # If cmd is a synchronous result, prepare it to be passed into # the next coroutine step. args = ensure_tuple(cmd) cmd_repr = truncated(repr(cmd), max_length=400, msg='...[long result truncated]') self.log("[lua_runner] got {}".format(cmd_repr)) self._print_instructions_used() except StopIteration: # "main" coroutine is stopped; # previous result is a final result returned from "main" self.log("[lua_runner] returning result") try: res = self.lua.lua2python(self.result) except ValueError as e: # can't convert result to a Python object raise ScriptError({ "type": ScriptError.BAD_MAIN_ERROR, "message": "'main' returned bad result. {!s}".format(e.args[0]) }) except lupa.LuaError as lua_ex: # Error converting result to Python # This may happen e.g. if conversion hit sandbox limits self.log("[lua_runner] caught LuaError %r" % lua_ex) info = parse_error_message(lua_ex.args[0]) error = info.get('error', '?') raise ScriptError({ "type": ScriptError.LUA_CONVERT_ERROR, "error": error, "message": "Lua error: {!s}".format(error) }) self._print_instructions_used() self.on_result(res) return except lupa.LuaError as lua_ex: # import traceback # print(traceback.format_exc()) # Lua script raised an error self._print_instructions_used() self.log("[lua_runner] caught LuaError %r" % lua_ex) # this can raise a ScriptError self.on_lua_error(lua_ex) # ScriptError is not raised, construct it ourselves info = parse_error_message(lua_ex.args[0]) info.update({ "type": ScriptError.LUA_ERROR, "message": "Lua error: {!s}".format(lua_ex) }) raise ScriptError(info) if isinstance(cmd, AsyncCommand): cmd.bind(self, next(self._command_ids)) self.log("[lua_runner] executing {!r}".format(cmd)) self._waiting_for_result_id = cmd.id self.on_async_command(cmd) return if isinstance(cmd, PyResult): self.log("[lua_runner] got result {!r}".format(cmd)) else: self.log("[lua_runner] got non-command") self.result = cmd