This is a hack for Zope products: import anything in Products/PRODUCT/import/ into /Control_Panel/Products/PRODUCT/ at startup time (if it's not already there). ZWiki used this at one point to install wiki templates from zexps.
import os
from Globals import package_home
from OFS.ObjectManager import customImporters
def autoImport(context):
"""Import any files in our import directory
into /Control_Panel/Products/PRODUCT.
Called at product startup and refresh.
"""
importdir = package_home(globals()) + os.sep + 'import'
if 0: #XXX not exists(importdir)
return
# sneaky, or dumb, way to get to the zodb
obj = context.getProductHelp()
productfolder = obj.getPhysicalRoot().Control_Panel.Products['ZWiki']
#XXX find product name dynamically
# based on manage_importObject
# locate a valid connection
connection=obj._p_jar
while connection is None:
obj=obj.aq_parent
connection=obj._p_jar
# try to import any and all files found
# will fail if the object is already in the ZODB
files = os.listdir(importdir)
for filename in files:
filepath = importdir + os.sep + filename
# how do we get these to show in undo ?
#get_transaction().begin()
try:
ob=connection.importFile(filepath, customImporters=customImporters)
# getId() fails on 'old' exported objects
id=ob.id
if hasattr(id, 'im_func'): id=id()
productfolder._setObject(id, ob, set_owner=0)
# try to make ownership implicit if possible
ob=productfolder._getOb(id)
ob.manage_changeOwnershipType(explicit=0)
#get_transaction().commit()
#XXX log it
except:
#get_transaction().abort()
#XXX log it
pass