You are not logged in Log in Join
You are here: Home » Members » andym » zodb2fs.py 0.1 » View File

Log in
Name

Password

 

zodb2fs.py 0.1

File details
Size
2 K
File type
text/plain

File contents

from os import path, sep, makedirs

objectList = []

def walk(folder, lookup):
    # recursive walk fn
    global objectList
    objs = folder.objectValues(lookup.keys())
    for obj in objs:
        objectList.append(obj)
        if getattr(obj, 'isPrincipiaFolderish', None):
            walk(obj, lookup)

def write(folder, lookup):
    # write fn
    global objectList

    for obj in objectList:
        # ie ignore folders...
        mt = str(obj.meta_type)
        if lookup[mt] is None: continue

        # for win, but shouldnt matter
        dest = obj.absolute_url(1).replace('/', sep)
        dest = path.join(folder, dest)

        # the dir
        destDir = sep.join(dest.split(sep)[:-1])
        if not path.exists(destDir): makedirs(destDir)

        # write out the object
        if not dest.endswith(lookup[mt]):
            dest = dest + lookup[mt]

        fh = open(dest, 'wb')
        if mt == 'Image':
            fh.write(str(obj.data))
        else:
            fh.write(obj.document_src())
        fh.close()

        # the object properties... 
        # wierd propertyMap dont seem to work...
        props = obj.propertyIds()
        if props:
            # hack for python scripts
            if props == ['title',] and mt == 'Script (Python)': continue

            pdest = dest + '.properties'

            fh = open(pdest, 'w')
            for prop in props: fh.write("%s=%s\n" % (prop, getattr(obj, prop)))
            fh.close()

def setup(folder, name):
    if not path.exists(folder): makedirs(folder)
    # touch refresh.txt
    open(path.join(folder, 'refresh.txt'), 'w').close()
    fh = open(path.join(folder, '__init__.py'), 'w')

    # register the name of the folder
    fh.write("""
from Products.FileSystemSite.DirectoryView import registerDirectory
registerDirectory('%s', globals())""" % name)
    fh.close()

def zodb2fs(self):
    # some variables to set
    # the folder to rummage through
    zopeFolder = self
    # the destination on the fs to write to

    # note, this doesnt have to be, but is recommended
    # to be in the instance_home, so then
    # the __init__.py will register the directory as a product
    # if the name of the folder in products is zodb2fs, then
    # the product name in the control panel will be zodb2fs
    destFolder = r'/home/andy/FatSite'

    # the object mapping of object to ext
    # any objects not in this list will be ignored
    objectDict = {
        'DTML Document':'.ddoc',
        'Script (Python)':'.py',
        'DTML Method':'.dtml',
        'Image':'',
        'Folder':None,
        }

    setup(destFolder, zopeFolder.getId())
    walk(zopeFolder, objectDict)
    write(destFolder, objectDict)

    return "Done"