why wsgi is wrong
i have been thinking about middleware, particularly within the context of bitsyblog but since i really want to do it right with bitsyblog (and since its a hobby project i'm free to do so) i think my observations have wider implications.
my thoughts first started with thinking about UI. specifically, two different pieces of UI -- css files and a site-nav. maybe these should be in two different packages....or...not. the issue with both of them is that they involve modifying the response of a full-fledged app.
lets think about how these would work. for a sitenav bar, an application puts something in the environmet -- namely, it appends to a list of links. for the css files (think also, javascript, etc), there is some mechanism as yet to be determined that inserts <link rel="" /> elements into the head of the returned (presumedly) HTML page.
how to do this is simple. you use lxml, decompose the html into a elementtree, and insert the elements you want where you want.
my question comes ... what if these are separate apps? then you are getting a node tree from lxml and putting it back together as an html page twice. okay, this might not be horrendous. but its not good. what if you have several apps that modify the page in (say) fairly autonomous and calculated ways. then you do this N times. not cheap. and also, as a programmer, i don't like the smell of it.
if it were me doing wsgi, i would try to do the transform once, if possible. i think this is doable. if you have the innermost wrapped app return the html, you check for the existence of a node structure in the response. if it does not exist, you make one. then, you modify the response such that the method to get the html is a virtual one -- you can obtain it, if needs be, but it involves reassembling the tree (this is now the expensive operation that one should avoid). other than that, things are free to modify the elementtree to their hearts' content. the client-facing wsgi layer reassembles the html and serves.
better. and doable in wsgi middleware!
so maybe wsgi's not wrong.