MCPcopy
hub / github.com/scrapinghub/splash / dispatch

Method dispatch

splash/lua_runner.py:94–208  ·  view source on GitHub ↗

Execute the script

(self, cmd_id, *args)

Source from the content-addressed store, hash-verified

92 pass
93
94 def dispatch(self, cmd_id, *args):
95 """ Execute the script """
96 args = args or ()
97
98 def truncated_repr(x):
99 return truncated("{!r}".format(x),
100 max_length=400,
101 msg="...[long arguments truncated]")
102 self.log("[lua_runner] dispatch cmd_id={}".format(cmd_id))
103
104 self.log(
105 "[lua_runner] arguments are for command %s, waiting for result of %s" % (cmd_id, self._waiting_for_result_id),
106 min_level=3,
107 )
108 if cmd_id != self._waiting_for_result_id:
109 msg = "[lua_runner] skipping an out-of-order result {}".format(truncated_repr(args))
110 self.log(msg, min_level=1)
111 if self.strict:
112 raise InternalError(msg)
113 else:
114 return
115
116 while True:
117 self.log('[lua_runner] entering dispatch/loop body, args={}'.format(truncated_repr(args)))
118 try:
119 if self._is_first_iter:
120 args = None
121 else:
122 is_python_result = (len(args) == 1 and
123 isinstance(args[0], PyResult))
124 if is_python_result:
125 args = args[0]
126 else:
127 args = PyResult(*args)
128
129 if self._is_stopped:
130 raise StopIteration
131
132 # Got arguments from an async command; send them to coroutine
133 # and wait for the next async command.
134 self.log("[lua_runner] send %s" % truncated_repr(args))
135 as_lua = self.lua.python2lua(args)
136 self.log("[lua_runner] send (lua) %s" % truncated_repr(as_lua))
137
138 cmd = self.coro.send(as_lua) # cmd is a next async command
139 if self._is_first_iter:
140 self._is_first_iter = False
141
142 # If cmd is a synchronous result, prepare it to be passed into
143 # the next coroutine step.
144 args = ensure_tuple(cmd)
145 cmd_repr = truncated(repr(cmd), max_length=400, msg='...[long result truncated]')
146 self.log("[lua_runner] got {}".format(cmd_repr))
147 self._print_instructions_used()
148
149 except StopIteration:
150 # "main" coroutine is stopped;
151 # previous result is a final result returned from "main"

Callers 4

startMethod · 0.95
return_resultMethod · 0.45
raise_errorMethod · 0.45
yield_resultMethod · 0.45

Calls 15

on_resultMethod · 0.95
on_lua_errorMethod · 0.95
on_async_commandMethod · 0.95
InternalErrorClass · 0.90
PyResultClass · 0.90
ensure_tupleFunction · 0.90
truncatedFunction · 0.90
ScriptErrorClass · 0.90
parse_error_messageFunction · 0.90
python2luaMethod · 0.80
lua2pythonMethod · 0.80

Tested by

no test coverage detected