[Zope-dev] [OPINIONS SOLICITED] Adding Batch Factory to ZMI "Add" menu OK?

Craeg K Strong cstrong@arielpartners.com
Mon, 27 May 2002 16:32:25 -0400


Hello:

I am about to release a new version of the ExternalFile and CVSFile products

http://www.zope.org/Members/arielpartners/ExternalFile

They are Zope objects that behave like standard Zope objects but retrieve
their content from files anywhere in the file system. (kind of like a windows 
shortcut or UNIX symbolic link)

I would like to include a way to automatically create an instance for every
file within a directory:  a "batch" flavor.

Previously, one would have to select "ExternalFile" from the "Add" menu in ZMI
for each and every file, one at a time.  For large directories, this is a major
pain.

The way I have implemented this is to create a new "pseudo object" -- basically
a factory for creating batches of ExternalFiles: "ExternalFileBatch"

You select "External File Batch" from the "Add" menu in ZMI and you get a form
that allows you to specify the directory.

Here is the code for ExternalFileBatch.py:


def manage_batch_add(self, target_dir, basedir='', REQUEST=None):
     """
     Factory method to actually create a batch of instances of
     ExternalFile.
     """
     message = "<h2>External File Batch Create Results:</h2>"

     try:
         filelist = dirlist(os.path.join(basedir,target_dir))

         if len(filelist) == 0:
             message = message + "No files found."

         for fileid, filepath in filelist:
             message = message + "Object instance '" + \
                       fileid + \
                       "' successfully created, pointing to file: " + \
                       filepath + "<br/>"

             self._setObject(fileid, ExternalFile(fileid, '', '', filepath))
             self._getOb(fileid).reindex_object()

     except OSError, e:
         message = message + str(e)

     return MessageDialog(title   = 'results',
                          message = message,
                          action  = 'manage_main')

################################################################
# ExternalFileBatch class
################################################################

class ExternalFileBatch:
     """
     ExternalFileBatch is a factory for creating instances of
     ExternalFile a batch at a time
     """
     meta_type = 'External File Batch'


As you can see, ExternalFileBatch is not much of a class.   It is really just
a Factory for creating instances of another class, namely ExternalFile.

An alternative would have been to create an additional tab in the ZMI for 
Folders which allows you to create instances of ExternalFile.  I could do this
by monkeypatching Folders, or by creating an ExternalFileFolder class.

There are advantages and disadvantages either way.  I don't like 
ExternalFileFolder b/c ExternalFiles are not supposed to be anything special. 
The whole point is to be able to use them in ANY context in which you would use 
a standard Zope object.

Thoughts?

--Craeg