Queries on Pyramid API

89 views
Skip to first unread message

Biswas, Pinakee

unread,
Jun 21, 2012, 12:52:34 PM6/21/12
to pylons-...@googlegroups.com

Hi,

 

I have following queries on Pyramid:

 

1.       In Pylons, there is a config API which could be used to read the fields in the .ini (development.ini or deployment.ini) file. Is there something similar in Pyramid?

2.       In Pylons, there is util containing forward, abort etc. Is there something similar in Pyramid?

 

I am sorry to raise these queries without patiently going through the documentation.

 

But it would be really helpful if there was a way to directly locate the APIs I would be interested in J. The list of modules/APIs is there but I am not able to figure which contains what without going through all of them. J

 

Looking forward to your help…

 

Thanks,

Pinakee

 

P Please don't print this e-mail unless you really need to, this will preserve trees on planet earth.

----------------------------Disclaimer-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The information contained in this message (including any attachments) is confidential and may be privileged. If you have received it by mistake please notify the sender by return e-mail and permanently delete this message and any attachments from your system. Please note that e-mails are susceptible to change and malwares. VVIDIA COMMUNICATIONS PVT LTD. (including its group companies) shall not be liable for the improper or incomplete transmission of the information contained in this communication nor for any delay in its receipt or damage to your system.

-------------------------------------------------------------------------------------------------------------------------Disclaimer----------------------------------------------------------------------------------------------------------

 

image001.png

chandrakant kumar

unread,
Jun 21, 2012, 1:07:06 PM6/21/12
to pylons-...@googlegroups.com
1. from pyramid.paster get_appsettings
settings = get_appsettings(config_uri)
where config_uri = your .ini file

2. Don't know what are you talking about.

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To post to this group, send email to pylons-...@googlegroups.com.
To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.



--
regards

image001.png

Jonathan Vanasco

unread,
Jun 21, 2012, 1:55:15 PM6/21/12
to pylons-discuss


> 1.       In Pylons, there is a config API which could be used to read the
> fields in the .ini (development.ini or deployment.ini) file. Is there
> something similar in Pyramid?

all of that information is stored in the request object under:
request.registry.settings


> 2.       In Pylons, there is util containing forward, abort etc. Is there
> something similar in Pyramid?

from pyramid.httpexceptions import *
from pyramid.httpexceptions import HTTPFound , HTTPNotFound

http://docs.pylonsproject.org/projects/pyramid/en/1.0-branch/api/httpexceptions.html

you can read more about them here:

http://pyramid.readthedocs.org/en/latest/narr/views.html#http-exceptions

you can raise or return those objects from a view.

they behave slightly differently when you `raise` than `return`.
IIRC, if you `return` you still have the request object in
subscribers, but if you `raise`, then pyramid's exeception handling
system takes control and you no longer have the request.

Mike Orr

unread,
Jun 21, 2012, 2:51:37 PM6/21/12
to pylons-...@googlegroups.com
On Thu, Jun 21, 2012 at 10:55 AM, Jonathan Vanasco <jona...@findmeon.com> wrote:


> 1.       In Pylons, there is a config API which could be used to read the
> fields in the .ini (development.ini or deployment.ini) file. Is there
> something similar in Pyramid?

all of that information is stored in the request object under:
request.registry.settings

Yes, request.registry.settings is equivalent to pylons.config. (At least in the basic sense that settings parsed from "[app:main]" will be in this dict.)
 


> 2.       In Pylons, there is util containing forward, abort etc. Is there
> something similar in Pyramid?

from pyramid.httpexceptions import *
from pyramid.httpexceptions import HTTPFound , HTTPNotFound

http://docs.pylonsproject.org/projects/pyramid/en/1.0-branch/api/httpexceptions.html

you can read more about them here:

http://pyramid.readthedocs.org/en/latest/narr/views.html#http-exceptions

you can raise or return those objects from a view.


There's also a function that behaves closer to abort.

return pyramid.httpexceptions.exception_response(404)
 
they behave slightly differently when you `raise` than `return`.
IIRC, if you `return` you still have the request object in
subscribers, but if you `raise`, then pyramid's exeception handling
system takes control and you no longer have the request.

I thought Pyramid handled returning and raising HTTP exceptions identically. Raising them does cut through intermediate stack frames (function calls) to whichever level catches the exception, as with all exceptions. So that may bypass subscribers. On the other hand, somebody who's asking these questions is probably not using custom subscribers anyway.

--
Mike Orr <slugg...@gmail.com>

Jonathan Vanasco

unread,
Jun 21, 2012, 5:06:15 PM6/21/12
to pylons-discuss
nope. return and raise are handled differently.

i ran into it while writing my cookie xfer module

there's an illustration of the difference here:
https://github.com/jvanasco/pyramid_subscribers_cookiexfer

i also brought it up in a discussion on this group once before.

you'd have to inspect the objects in a subscriber , but the general
notion was that on a "return", you are passing back a request object
that you control, and pyramid will note the exception and act
accordingly. if you raise the error, the request or response object
is lost ( i don't recall which one ) and pyramid only really has the
caught exception to handle.

anyways, for general compatabilty reasons , i'd strongly suggest
returning http exceptions instead of raising them.

Biswas, Pinakee

unread,
Jun 22, 2012, 1:15:01 AM6/22/12
to pylons-...@googlegroups.com

Hi Mike and Jonathan,

 

Thanks for your prompt response.

 

Is there any corresponding API in Pyramid for the following?:

 

pylons.controllers.util.forward(wsgi_app)

Forward the request to a WSGI application. Returns its response.

return forward(FileApp('filename'))

 

Thanks,

Pinakee

 

P Please don't print this e-mail unless you really need to, this will preserve trees on planet earth.

----------------------------Disclaimer-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The information contained in this message (including any attachments) is confidential and may be privileged. If you have received it by mistake please notify the sender by return e-mail and permanently delete this message and any attachments from your system. Please note that e-mails are susceptible to change and malwares. VVIDIA COMMUNICATIONS PVT LTD. (including its group companies) shall not be liable for the improper or incomplete transmission of the information contained in this communication nor for any delay in its receipt or damage to your system.

-------------------------------------------------------------------------------------------------------------------------Disclaimer----------------------------------------------------------------------------------------------------------

 

--

Mike Orr

unread,
Jun 22, 2012, 1:01:02 PM6/22/12
to pylons-...@googlegroups.com
I saw something for forwarding in the Pyramid API but I don't remember where. Look through the API section in the Pyramid docs table of contents.


On Thu, Jun 21, 2012 at 10:15 PM, Biswas, Pinakee <pin...@vvidiacom.com> wrote:

Hi Mike and Jonathan,

 

Thanks for your prompt response.

 

Is there any corresponding API in Pyramid for the following?:

 

pylons.controllers.util.forward(wsgi_app)

Forward the request to a WSGI application. Returns its response.

return forward(FileApp('filename'))

 



--
Mike Orr <slugg...@gmail.com>

Jason

unread,
Jun 25, 2012, 2:16:43 PM6/25/12
to pylons-...@googlegroups.com


On Friday, June 22, 2012 1:15:01 AM UTC-4, Pinakee Biswas wrote:

Hi Mike and Jonathan,

 

Thanks for your prompt response.

 

Is there any corresponding API in Pyramid for the following?:

 

pylons.controllers.util.forward(wsgi_app)

Forward the request to a WSGI application. Returns its response.

return forward(FileApp('filename'))

 


I didn't use that API in Pylons, but I think you want to do something like:

class LegacyView(object):
    """ Forwards requests to given WSGI app. """
    def __init__(self, app):
        self.app = app
    def __call__(self, request): 
        return request.get_response(self.app)

I forward to a WSGI application when a route raises an HTTPNotFound (In Pylons what would be abort(404)). In the project's __init__ I have:

pylonsapp= make_app(global_config, **app_settings) # this is to make a Pylons WSGI Application 
legacy_view = LegacyView(pylonsapp)
config.add_view(context='pyramid.exceptions.NotFound', view=legacy_view, permission=NO_PERMISSION_REQUIRED)

Of course you could make a route and view like you normally would, but just specify that LegacyView instance as the view parameter to add_view.


-- Jason

Reply all
Reply to author
Forward
0 new messages