The issue seems to be a legacy CGI/WSGI issue with the older Trac 0.10
version that I'm using.  It expects start_response to return a write
function.  The paste.wsgilib.intercept_output worked fine for
rewriting web pages, but it didn't work for streaming content since it
would store everything in memory.  So, I ended up with the following
solution which may not be pretty, but it works well and handles both
web pages and streaming file downloads.

In routes, do the following:
    map.connect("wiki", "/trac", controller="EmbeddedTrac",
path_info="/")
    map.connect('/trac/*path_info', controller='EmbeddedTrac')

Then add the following controller:

class EmbeddedtracController(BaseController):

    def __init__(self):
        # set application to the trac WSGI dispatch_request function
        self.app = dispatch_request
        self.trac_env_path = config['trac_env_path']

    def __call__(self, environ, start_response):
        """Dispatches request to trac."""

        environ['trac.env_path'] = self.trac_env_path

        # seek back to the beginning of the wsgi.input parameter
(otherwise
        # trac forms and file upload will not work correctly)
        environ['input_saver.rewind']()

        # pass request to trac -- wrap the start_response to provide
the
        # legacy write return value.
        self.lstData = []
        def legacy_start_response(status, headers, exc_info = None):
            start_response(status, headers, exc_info or
sys.exc_info())

            def _write(data):
                self.lstData.append(data)

            return _write

        app_iter = self.app(environ, legacy_start_response)

        def generator():
            # iterate through the web output which is used for
streaming
            # large files
            for item in app_iter:
                yield item

                if self.lstData:
                    yield self.lstData[0]
                    self.lstData = self.lstData[1:]

            # send any remaining blocks (this typically handles a
single
            # response such as a web page)
            for item in self.lstData:
                yield item

            if hasattr(app_iter, 'close'):
                app_iter.close()
        return generator()


Any suggestions on improvements are always welcome!

Thanks,

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

Reply via email to