[mod_python] Trouble with multiple class objects in publisher model

Martin Stoufer MCStoufer at lbl.gov
Wed Nov 8 16:52:54 EST 2006

I am moving a set of python classes out of the Apache CGI model and into 
mod_python. There is both inheritance and composition going on among 
these classes. A few factory classes as well. It seems that when it 
comes time to pull the generated content from the hierarchy of classes, 
the string object returned to the initial class end up 'None'.

I have stripped down my work as an example. If there is some larger 
example of how this class structure should work, I would love to see it.

from mod_python import apache

class Dom:
    def __call__(self, req):
        from DomProdLibOutput import Html
        self.form = req.form
        self.output = Html('modPython')
        self.host = self.form['host'].value
        self.sortBy = self.form['sortby'].value
        self.mode = self.form['mode'].value
        if self.form.has_key('term'):
           self.term = ""
        req.content_type = "text/html"
        result = self.generate()
        return apache.OK

    def generate(self):
        result = self.output.generate()
        return result

dom = Dom()

In the DomProdLibOutput file in the same dir:

class Html:
    def __init__(self, destination='screen'):
        self.output = HtmlOutputFactory(destination).getOutput()

    def generate(self):
        #result = "Foo in Html"
        result = self.output.generate()
        return result

class HtmlModPython:
    def __init__(self):
        self.content = """Empty content in HtmlModPython"""
    def update(self, content):
        self.content += content

    def generate(self):
        return "Foo in HtmlModPython"
        #return self.content

class HtmlOutputFactory:
    def __init__(self, destination):
        self.dest = destination

    def getOutput(self):
        if self.dest == 'screen': return HtmlScreen()
        elif self.dest == 'modPython': return HtmlModPython()
        elif self.dest == 'file': return HtmlFile()
            raise InvalidHtmlOutputDest, \
        "%s is not a supported output" % self.dest

As an interesting side note, if I have the generate() in Html return 
just the string, things work fine. Its when I try and resolve the string 
stored by the HtmlModPython class instance that the resulting string 
comes back 'None' irregardless of what the deepest generate() returns. 
Do all these classes have to define __call__ as well?

* Martin C. Stoufer              *
* DIDC/DSD/ITG                   *
* Lawrence Berkeley National Lab *
* MS 50B-2215 510-486-8662       *

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3423 bytes
Desc: S/MIME Cryptographic Signature
Url : http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20061108/2a36f5b5/smime.bin

More information about the Mod_python mailing list