Exemple #1
0
 def exit():
     logger.debug("USE %s: EXIT (%s)",o._name,o._json)
     try:
         asyncio.create_task(self.emitMe(eventExit,o._json)) #  py37
     except:
         asyncio.ensure_future(self.emitMe(eventExit,o._json)) # py35
     del INST[o._id]
Exemple #2
0
    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 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")
Exemple #4
0
    def __call__(self,method,*args):
        function = self._getRoutage(method)

        ret= function(*args)

        if isinstance(ret,Guy):
            ################################################################
            o=ret

            routes=[k for k in o._routes.keys() if not k.startswith("_")]

            eventExit="event-"+o._id+".exit"
            def exit():
                logger.debug("USE %s: EXIT (%s)",o._name,o._json)
                try:
                    asyncio.create_task(self.emitMe(eventExit,o._json)) #  py37
                except:
                    asyncio.ensure_future(self.emitMe(eventExit,o._json)) # py35
                del INST[o._id]



            html=o._renderHtml( includeGuyJs=False )
            scripts=";".join(re.findall('(?si)<script>(.*?)</script>', html))

            o.parent = self
            o._callbackExit=exit
            asyncio.ensure_future( doInit(o) )

            obj=dict(
                id=o._id,
                name=o._name,
                html=html,
                routes=routes,
                event=eventExit,
                scripts=scripts,

                script="guy._instanciateWindow(x.result)"
            )
            return obj
            ################################################################

        return ret
Exemple #5
0
 def _connect(self,wsock):
     self._wsock = wsock # save the current socket for this instance !!!
     asyncio.ensure_future( doInit(self) )
 def exit():
     log("USE %s: EXIT" % o._name, o._json)
     # asyncio.create_task(self.emitMe(eventExit,o._json)) #  py37
     asyncio.ensure_future(self.emitMe(eventExit, o._json))  # py35
Exemple #7
0
    CMD ["/usr/local/bin/python","app.py"]
    """

    f = BytesIO(dockerfile.encode("utf-8"))
    tar_obj = utils.mktar_from_dockerfile(f)
    await docker.images.build(fileobj=tar_obj, encoding="gzip", tag=name)
    tar_obj.close()
    image = await docker.images.inspect(name=name)
    assert image
'''

def run_tornado():
    print("running tornado")
    define("port", default=9999, help="run ont he given port", type=int)
    tornado.options.parse_command_line()
    application = tornado.web.Application([(r"/", MainHandler)])
    application.listen(options.port)
    print("Listening on http://localhost:{}".format(options.port))


async def run_discordbot():
    print('bot running')
    await bot.run(discordbot['token'])


if __name__ == "__main__":
    run_tornado()
    loop = asyncio.get_event_loop()
    asyncio.ensure_future(run_discordbot())
    asyncio.ensure_future(run_docker())
    loop.run_forever()