[Zope] Temp Folder Magic

Terry Hancock hancock@anansispaceworks.com
Sun, 11 Aug 2002 03:31:57 -0700


Hi all,

I'm finally revisiting the nasty part of my code
that periodically hoses itself and fails to deliver
resized images. ;-D

I need to implement a cache of image objects in
Zope (i.e. not in front of Zope), and it was suggested
to me that the best way might be to use "Temporary
Folders" in Zope 2.5.1.

Anyway, I now see that Temporary Folder is a bit
more magical than most Zope objects (not too
surprizing I suppose). Anyway, what I'm trying to
do is inherit from both Image and Temporary Folder
and create the resized images in the object (supposing
that they will then be stored temporarily). Perhaps
this is naive, but it was my first guess as to
the most logical way to do this.

What I actually have looks like this:

class VarImage(OFS.Image.Image, MountedTemporaryFolder) :
    """
    Variable-size image object for Zope.
    """
    #[... lots of hopefully irrelevant detail omitted ...]

    def _read_url_cmd(self, keystr):
        # Actual command recognition omitted for brevity,
        # but for example's sake:
        if keystr=='s100':
            return (100, 100)
        else
            return None

    def __getattr__(self, keystr):
        dimensions = self._read_url_cmd(keystr)
        if dimensions == None:
        # Unrecognized URL requested, pass through:
        # raise ValueError, 'Unrecognized VarImage scaling command.'
            try:
                return MountedTemporaryFolder.__getattr__(self, keystr)
            except:
                return OFS.Image.Image.__getattr__(self, keystr)
            else:
                nx, ny = dimensions

        # This part is messy, because I'm testing, eventually it
        # will do a bit more to try to get a cache hit instead of
        # creating the image everytime:

        threading.Lock()
        img_file, img_type = img_resize(StringIO(self.data), 
   					self.content_type, nx, ny)
        img = OFS.Image.Image(keystr, title='', file=img_file,
content_type=img_type)

        self._setObject(keystr, img)
	# ^^^^^^-- This chokes

        #self.manage_addProduct['OFSP'].manage_addImage(keystr, '',
        #    			file=img_file, content_type=img_type)
        # ^^^^^^-- So does this when you uncomment it

        threading.Unlock()
	
        # Okay, now throw all that away and return self anyway.
        # just for debugging of course -- this is so I'm not debugging
        # the actual resizing code.
        return self

Looking in Products/TemporaryFolder.py makes me think
I'm being naive, though. I note that the meta_type for
MountedTemporaryFolder says it's "Broken Temporary Folder".
So where does "Temporary Folder" come from?  It's here
in the __init__.py:

def initialize(context):
    import TemporaryFolder
    context.registerClass(
        TemporaryFolder.MountedTemporaryFolder,
        permission=TemporaryFolder.ADD_TEMPORARY_FOLDER_PERM,
        icon='www/tempfolder.gif',
        meta_type='Temporary Folder',
        constructors=(TemporaryFolder.constructTemporaryFolderForm,
                      TemporaryFolder.constructTemporaryFolder)
        )

(constructTemporaryFolder doesn't appear to be particularly
different from the more common "manage_add" function, except
for the name, BTW).

This appears to be doing something clever, but I don't
get it.  I can't figure out when and how the Temporary
Folder acquires Folder-like behavior.

Questions:

  1. *Can* I inherit from Temporary Folder as desired,
     and if so, will it act as a Temporary Folder?

  2. If so, how?  Which object do I inherit from, and/or
     do I have to do anything special to make it non-broken,
     since it apparently starts out broken?

Any clarifications much appreciated, thank you!
Terry

-- 
------------------------------------------------------
Terry Hancock
hancock@anansispaceworks.com       
Anansi Spaceworks                 
http://www.anansispaceworks.com 
P.O. Box 60583                     
Pasadena, CA 91116-6583
------------------------------------------------------