[Grok-dev] Get grok site "outside" the server (in an external script)

Hector Blanco white.lists at gmail.com
Tue Jul 26 19:06:31 EDT 2011


Got it..

from ZODB.FileStorage import FileStorage
from ZODB.DB import DB
storage = FileStorage('/home/zignage/Projects/my_server/var/filestorage/Data.fs')
db=DB(storage)
connection=db.open()
site = connection.root()['Application']['myserver']
site['MyManager1'].superCoolMethod()

I had tried this, but I was getting a

connection.root()['Application']['myserver']
<persistent broken my_server.app.MyServer instance '\x00\x00\x00\x00\x00\x0035'>

see the broken?... well... it was true: it was broken. But then I went
to the server control page (127.0.0.1:8080), took a look to the path
that was set in there and realized that I wasn't adding all the Python
files to the $PYTHONPATH. Adding these missing folders fixed it, and
now

connection.root()['Application']['MyServer']
<my_server.app.My_server object at 0x9a0606c>

the "MyServer" is not broken anymore :)



2011/7/26 Hector Blanco <white.lists at gmail.com>:
> Hello everyone
>
> I have built a typical grok application:
>
> -------------- app.py --------------
> class MyServer(grok.Application, grok.Container):
>        def __init__(self):
>                try:
>                        super(MyServer, self).__init__()
>                        self["MyManager1"] = MyManager1.MyManager1()
>                        self["MyManager2"] = MyManager2.MyManager2()
> ------------------------------------------
>
> I would like to access that "application" (MyServer) in a python
> script outside the web server so I can use the Managers and its
> functions.
>
> Ideally, what I need is to be able to open a python console and do
> something like:
>
>>>> import grok
>>>> site = grok.getSite()
>>>> site['MyManager1'].superCoolMethod()
>
> (where superCoolMethod is, as you may have suspected, a function of
> the elements of MyManager1 type)
>
> I've been able to create an script that puts in the PYTHOPATH all the
> required eggs (the ones found under $HOME/.buildout) but if I try what
> I described above, grok.getSite() returns None. It works fine when I
> do grok.getSite() inside the server, but not outside.
>
> Is there any way to "manually" connect to the ZopeDB (I'm guessing to
> the Data.fs) when the server is not running?
>
> I've also tried something that I seem to remember it worked before,
> but without luck today:
>
>>>> from ZODB.interfaces import IDatabase
>>>> import grok
>>>> from zope.component import getUtility
>>>> db = getUtility(IDatabase)
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>  File "/home/zignage/.buildout/eggs/zope.component-3.10.0-py2.6.egg/zope/component/_api.py",
> line 169, in getUtility
>    raise ComponentLookupError(interface, name)
> zope.component.interfaces.ComponentLookupError: (<InterfaceClass
> ZODB.interfaces.IDatabase>, '')
>
> Basically, I want to reach the "MyServer" instance from an external
> script and without the server running. Is that possible?
>
> Thank you very much!
>


More information about the Grok-dev mailing list