Thank you Michael,

in fact, my command-line script does use pyramid.paster.get_appsettings,
and I can read the setting just fine. My problem is with the SQLAlchemy
event listener in my model:

- The reindex_after_update function instantiates an IndexEngine object
- The IndexEngine attempts to read the settings using get_current_request

(see snippet below)

If the "after_update" event is fired from the Pyramid app itself,
get_current_request works. If it's fired from my command line script, it
fails.

I've also considered trying pyramid.testing.DummyRequest, but, if I
remember correctly, it doesn't contain the necessary info.



model.py
========

from my_indexation    import IndexEngine
from .meta            import Base
from sqlalchemy.event import listen

solr = IndexEngine()


class MyContentClass(Base):
    ...
    ...

# after_update Listener
def reindex_after_update(mapper, connection, target):
    solr.indexObject(target)

listen(MyContentClass, "after_update", reindex_after_update)


myIndexation.py
===============

class IndexEngine(object):

    def __init__(self, settings=None, request=None):

      if not settings:
          if not request:
              request = get_current_request()
          settings = request.registry.settings

    # Read Solr URL
    ...




Le 26/06/13 09:28, Michael Merickel a écrit :
> You can create a request-like environment by using the bootstrap api
> which will allow you to use get_current_request if you like, among other
> things you would normally do in your app.
> 
> Another option is to simply parse the settings from your ini file using
> p.paster.get_appsettings('foo.ini').
> 
> http://docs.pylonsproject.org/projects/pyramid/en/1.4-branch/narr/commandline.html#writing-a-script
> http://docs.pylonsproject.org/projects/pyramid/en/1.4-branch/api/paster.html#pyramid.paster.get_appsettings
> 
> 
> On Wed, Jun 26, 2013 at 2:22 AM, Laurent DAVERIO <ldave...@gmail.com
> <mailto:ldave...@gmail.com>> wrote:
> 
>     Hello everybody,
> 
>     my last questions to the list have all remained unanswered /
>     unacknowledged :'-(, but I want to give it another try. This one should
>     be simpler than the rest.
> 
>     Basically, I have a data model in which an SQLAlchemy "after_update"
>     listener automatically reindexes object attributes in Apache Solr after
>     every update.
> 
>     (To make an analogy with Zope CMF, you could see it as implementing
>     CatalogAware classes).
> 
>     The URL to Solr is stored in the app's INI file.
> 
>     This works fine as long as the objects are updated from the Pyramid app
>     itself. The listener can access the request via
>     pyramyd.threadlocal.get_current_request(). But when I run an
>     import/update script from the command line, no request is defined, and I
>     can't get the URL to Solr.
> 
>     So, my question would be: what would be the best way to access the app's
>     settings (or the path to the app's INI file) when not in a request?
> 
>     Thanks in advance,
> 
>     Laurent.
> 
>     --
>     You received this message because you are subscribed to the Google
>     Groups "pylons-discuss" group.
>     To unsubscribe from this group and stop receiving emails from it,
>     send an email to pylons-discuss+unsubscr...@googlegroups.com
>     <mailto:pylons-discuss%2bunsubscr...@googlegroups.com>.
>     To post to this group, send email to pylons-discuss@googlegroups.com
>     <mailto:pylons-discuss@googlegroups.com>.
>     Visit this group at http://groups.google.com/group/pylons-discuss.
>     For more options, visit https://groups.google.com/groups/opt_out.
> 
> 
> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to pylons-discuss+unsubscr...@googlegroups.com.
> To post to this group, send email to pylons-discuss@googlegroups.com.
> Visit this group at http://groups.google.com/group/pylons-discuss.
> For more options, visit https://groups.google.com/groups/opt_out.
>  
>  


-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pylons-discuss+unsubscr...@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to