[Zope] Storing files externally

tonyr@ep.newtimes.com tonyr@ep.newtimes.com
Fri, 17 Dec 1999 16:55:17 -0700


Alexander Limi wrote:

> Shouldn't it be easy to store files outside the ZODB? I find these methods a
> bit cumbersome.

I've been able to write to FS files using External Methods.  There is a sampling
of my code.

================================
import os, re, string, time
from ZPublisher import Client

def indexThis(self, idxkey=None):
    """ accept idxkey and write the indexable version to FS """
    if not idxkey:
        return 'No idxkey'

    REQUEST = self.REQUEST
    fname = "/home/sites/" + idxkey
    path = os.path.dirname(fname)

    if not os.path.isdir(path):
        os.makedirs(path)

    url = REQUEST.BASE0 + "/" + idxkey + "/page_clean"
    newString = Client.call(url,None,None)[1]

    output = open(fname,'w')
    output.write(newString)
    output.close()

    atime = time.mktime(time.strptime(string.split(fname,'/')[5],'%Y-%m-%d'))
    os.utime(fname,(atime,atime))

    return url
=====================================

Above the idxkey is basically the URL path of the object which becomes the path
for the FS file.  When indexThis is called it actually makes an exterenal web
request, via Client.call(), and stores the results to the filesystem.  I was not
able to get the method page_clean to run on the proper object via other means.
The messing around with the timestamp of the file is used to make sure our index
engine indexes in the proper order.   Python makes writting out to the FS
incredibly easy.

Hope this helps.



-------------------------------
tonyr@ep.newtimes.com
Director of Web Technology
Newtimes Inc.
-------------------------------