Get grok site "outside" the server (in an external script)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Get grok site "outside" the server (in an external script)

Hector Blanco-2
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!
_______________________________________________
Grok-dev mailing list
[hidden email]
https://mail.zope.org/mailman/listinfo/grok-dev
Reply | Threaded
Open this post in threaded view
|

Re: Get grok site "outside" the server (in an external script)

Hector Blanco-2
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 <[hidden email]>:

> 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!
>
_______________________________________________
Grok-dev mailing list
[hidden email]
https://mail.zope.org/mailman/listinfo/grok-dev
Reply | Threaded
Open this post in threaded view
|

Re: Get grok site "outside" the server (in an external script)

Kevin Teague
To make it easier to manage the set of paths you need Python to be able to search, you can add an entry_point to your setup.py and buildout will generate a wrapper file which you can call your script from with all of the same paths as your Grok application. An example from one of my apps for creating a script called 'slurp':

    entry_points = """
    [console_scripts]
    haystack-debug = grokcore.startup:interactive_debug_prompt
    haystack-ctl = grokcore.startup:zdaemon_controller
    slurp = gsc.searchindexer.slurp:main

    [paste.app_factory]
    main = grokcore.startup:application_factory
    debug = grokcore.startup:debug_application_factory
    """


_______________________________________________
Grok-dev mailing list
[hidden email]
https://mail.zope.org/mailman/listinfo/grok-dev