durusmail: quixote-users: setting the current running python script as the quixote publisher namespace
setting the current running python script as the quixote publisher namespace
2004-06-24
Re: setting the current running python script as the quixote publisher namespace
2004-06-25
setting the current running python script as the quixote publisher namespace
Jon Dyte
2004-06-24
Hi
consider the script below, which is using twisted, twisted_http and quixote:

from quixote.server.twisted_http import Server
from twisted.internet import reactor
import sys,quixote

quixote.enable_ptl()

_q_exports=[]

def _q_index(request):
        request.response.set_content_type("text/plain")
        return "This is the _q_index function .\n"

class MyApp():
        def start(self):
                Server(sys.modules["__main__"],9999)


app = MyApp()
app.start()
reactor.run()


Having fiddled around a bit, I found that using sys.modules["__main__'] was
a way to pass to the Publisher the current script namespace.
This seemed a bit hackish to me, if it were a class you could use

from quixote.server.twisted_http import Server
from twisted.internet import reactor
import sys,quixote

quixote.enable_ptl()

class MyApp2:
        _q_exports =[]
        def _q_index(self,request):
                request.response.set_content_type("text/plain")
                return "This is the _q_index method .\n"
        def start(self):
                Server(self,9999)


app = MyApp()
app.start()
reactor.run()

Does anyone have any better solutions?
(Note this is very silly example, in the real case I have twisted code which
does a load of various things, and afterwards must now start serving quixote
and periodically update the various things it did before serving quixote).

Don't ask why this has to be all in one script!!!!

Jon


reply