async def doInit( instance ): instance._rebind() # WTF ?: need to call this if hasattr(instance,"init"): self_init = getattr(instance, "init") if asyncio.iscoroutinefunction( self_init ): await self_init( ) else: self_init( )
async def on_message(self, message): instance = WebSocketHandler.clients.get(self, None) if instance is None: return o = jLoads(message) logger.debug("WS RECEPT: %s", o) method, args, uuid = o["command"], o.get("args"), o["uuid"] if method == "emit": event, *args = args await emit(event, *args) # emit all elif method == "return": logger.debug(" as JS Response %s : %s", uuid, args) WebSocketHandler.returns[uuid] = args else: async def execution(function, uuid, mode): logger.debug(" as Execute (%s) %s(%s)", mode, method, args) try: ret = await function() ############################################################## if type(ret) == dict and "script" in ret: #evil mode s = ret["script"] del ret["script"] r = dict(result=ret, script=s, uuid=uuid) #evil mode else: ############################################################## r = dict(result=ret, uuid=uuid) except concurrent.futures._base.CancelledError as e: r = dict(error="task cancelled", uuid=uuid) except Exception as e: r = dict(error=str(e), uuid=uuid) logger.error("================================= in %s %s", method, mode) logger.error(traceback.format_exc().strip()) logger.error("=================================") logger.debug(">>> (%s) %s", mode, r) await sockwrite(self, **r) fct = instance._getRoutage(method) if asyncio.iscoroutinefunction(fct): async def function(): return await instance(method, *args) #asyncio.create_task( execution( function, uuid, "ASYNC") ) #py37 asyncio.ensure_future(execution(function, uuid, "ASYNC")) #py35 else: async def function(): return instance(method, *args) await execution(function, uuid, "SYNC")
async def callhttp(web,path): # web: RequestHandler for name,method in https.items(): g=re.match(name,path) if g: if asyncio.iscoroutinefunction( method ): ret=await method(web,*g.groups()) else: ret=method(web,*g.groups()) if isinstance(ret,Guy): ret.parent = web.instance web.write( ret._renderHtml() ) return True
async def callhttp(web, path): # web: RequestHandler for name, method in https.items(): g = re.match(name, path) if g: if asyncio.iscoroutinefunction(method): ret = await method(web, *g.groups()) else: ret = method(web, *g.groups()) if isinstance(ret, Guy): web.instance._children[ret._name] = ret web.render(ret) return True
async def on_message(self, message): instance = WebSocketHandler.clients[self] o = jLoads(message) log("WS RECEPT:", o) method, args, uuid = o["command"], o["args"], o["uuid"] if method == "emit": event, *args = args await emit(event, *args) else: async def execution(function, uuid, mode): log("Execute (%s)" % mode, method, args) try: ret = await function() ############################################################## if type(ret) == dict and "script" in ret: #evil mode s = ret["script"] del ret["script"] r = dict(result=ret, script=s, uuid=uuid) #evil mode else: ############################################################## r = dict(result=ret, uuid=uuid) except concurrent.futures._base.CancelledError as e: r = dict(error="task cancelled", uuid=uuid) except Exception as e: r = dict(error=str(e), uuid=uuid) print("=" * 40, "in ", method, mode) print(traceback.format_exc().strip()) print("=" * 40) log(">>> (%s)" % mode, r) await sockwrite(self, **r) fct = instance._getRoutage(method) if asyncio.iscoroutinefunction(fct): async def function(): return await instance(method, *args) #asyncio.create_task( execution( function, uuid, "ASYNC") ) #py37 asyncio.ensure_future(execution(function, uuid, "ASYNC")) #py35 else: async def function(): return instance(method, *args) await execution(function, uuid, "SYNC")