Geoff Talvola <[EMAIL PROTECTED]> wrote:
> I've also run into situations where this would be useful, but I've just 
> worked around them.  Here's an idea.  Ordinarily your writeContent() method 
> will look something like:
> 
>          def writeContent():
>                  self.writeBeginning()
>                  self.writeMiddle()
>                  self.writeEnd()
> 
> But suppose you want to re-arrange the order in which these are 
> processed.  You could do something like:
> 
>          def writeContent():
>                  middle = self.capture(self.writeMiddle)
>                  self.writeBeginning()
>                  self.write(middle)
>                  self.writeEnd()
> 
> where self.capture is a helper method that causes its argument to be 
> executed, but all self.write() and self.writeln() are captured and returned 
> as a string instead of being sent out.
> 
> I leave the implementation of self.capture() up to the reader :-)  Also, 
> I'm curious if people think this is a good approach.

It seems useful to me.  I implemented it with a new method to response
(so you'd call self.response().capture(func)):


from DummyStreamOut import DummyStreamOut
## ...
        def capture(self, func):
                oldStrmOut = self._strmOut
                self._strmOut = DummyStreamOut()
                func()
                self._strmOut.commit()
                value = self._strmOut._buffer
                self._strmOut = oldStrmOut
                return value



And a new class:

## DummyStreamOut.py
import string

class DummyStreamOut:
    def __init__(self):
        self._data = []
        self._committed = 0
    def write(self, charstr):
        self._data.append(charstr)
    _needCommit = 0
    def flush(self):
        pass
    def autoCommit(self, value):
        pass
    def commit(self):
        self._committed = 1
        self._buffer = string.join(self._data, '')
        del self._data
    def prepend(self, charstr):
        self._data = [charstr] + self._data
    def clear(self):
        self._data = []
    def size(self):
        return len(self._buffer)
    



I didn't entirely understand the interface that response._strmOut was
supposed to have, but this was my best guess.

  Ian

_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to