[Zope-CVS] CVS: Products/AdaptableStorage - Zope2SQL.py:1.1

Shane Hathaway shane@zope.com
Tue, 10 Dec 2002 15:37:52 -0500


Update of /cvs-repository/Products/AdaptableStorage
In directory cvs.zope.org:/tmp/cvs-serv10311

Added Files:
	Zope2SQL.py 
Log Message:
Checkpoint: storage to a Postgres database.  Not working yet.


=== Added File Products/AdaptableStorage/Zope2SQL.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""A basic mapping from Zope 2 objects to a Postgres database.

$Id: Zope2SQL.py,v 1.1 2002/12/10 20:37:51 shane Exp $
"""


from serial.public \
     import ObjectSerializer, ObjectGateway, ObjectMapper
from serial_std.public \
     import RollCall, FixedPersistentMapping, IgnoredAttribute, \
     RemainingState, PathKeychainGenerator, AnyObjectSerializer
from serial_ofs.public import FolderItemsByKeychain, MetaTypeClassifier, \
     IdAttribute
from gateway_sql.public import PsycopgConnection, SQLClassification, \
     SQLFolderItems, SQLItemId, SQLKeychainGenerator, SQLRemainder


def createMapper(params='', table_prefix='zodb'):
    conn = PsycopgConnection(params, table_prefix)

    object_serializers = {}
    object_gateways = {}

    # SERIALIZERS

    # folder serializer
    s = ObjectSerializer('OFS.Folder', 'Folder')
    s.addAspect('items', FolderItemsByKeychain())
    s.addAspect('id', IdAttribute())
    s.addAspect('remainder', RemainingState())
    object_serializers['OFS.Folder.Folder'] = s

    # anyfolder serializer
    s = AnyObjectSerializer()
    s.addAspect('items', FolderItemsByKeychain())
    s.addAspect('id', IdAttribute())
    s.addAspect('remainder', RemainingState())
    object_serializers['anyfolder'] = s

    # anyfile serializer
    s = AnyObjectSerializer()
    s.addAspect('id', IdAttribute())
    s.addAspect('remainder', RemainingState())
    object_serializers['anyfile'] = s

    # application serializer
    s = ObjectSerializer('OFS.Application', 'Application')
    s.addAspect('items', FolderItemsByKeychain())
    s.addAspect('remainder', RemainingState())
    object_serializers['OFS.Application.Application'] = s

    # root serializer
    s = ObjectSerializer('Persistence', 'PersistentMapping')
    aspect = FixedPersistentMapping()
    aspect.add('Application', ('/',), ('OFS.Application.Application',))
    s.addAspect('items', aspect)
    s.addAspect('roll_call', RollCall())
    root_serializer = s

    # GATEWAYS

    folder_items_gw = SQLFolderItems(conn)
    item_id_gw = SQLItemId(conn)
    remainder_gw = SQLRemainder(conn)
    classification_gw = SQLClassificationSection(conn)
    keychain_gen = SQLKeychainGenerator(conn)
    gws = (folder_items_gw, item_id_gw, remainder_gw, classification_gw,
           keychain_gen)

    # folder gateway
    g = ObjectGateway()
    g.addGateway('items', folder_items_gw)
    g.addGateway('id', item_id_gw)
    g.addGateway('remainder', remainder_gw)
    object_gateways['OFS.Folder.Folder'] = g

    # anyfolder object gateway
    g = ObjectGateway()
    g.addGateway('items', folder_items_gw)
    g.addGateway('id', item_id_gw)
    g.addGateway('remainder', remainder_gw)
    object_gateways['anyfolder'] = g

    # anyfile object gateway
    g = ObjectGateway()
    g.addGateway('id', item_id_gw)
    g.addGateway('remainder', remainder_gw)
    object_gateways['anyfile'] = g

    # application gateway
    g = ObjectGateway()
    g.addGateway('items', folder_items_gw)
    g.addGateway('remainder', remainder_gw)
    object_gateways['OFS.Application.Application'] = g

    # root gateway (no storage)
    g = ObjectGateway()
    root_gateway = g

    # Sanity check
    s_keys = object_serializers.keys()
    s_keys.sort()
    g_keys = object_gateways.keys()
    g_keys.sort()
    assert s_keys == g_keys

    # Put everything together
    classifier = MetaTypeClassifier(classification_gw)
    classifier.registerDefaultLoader('Folder', 'OFS.Folder.Folder', 1)
    classifier.registerDefaultStorage('(folderish object)', 'anyfolder', 1)
    classifier.registerDefaultStorage('(fileish object)', 'anyfile', 0)
    classifier.registerKey('Application', 'OFS.Application.Application', '/')
    classifier.register('CMF Skins Tool', 'anyfile')  # XXX workaround
    rm = ObjectMapper(
        None, root_serializer, root_gateway, classifier, keychain_gen)
    for name in s_keys:
        mapper = ObjectMapper(rm, object_serializers[name],
                              object_gateways[name], volatile=volatile)
        rm.addSubMapper(name, mapper)

    return rm, conn, gws


# For use in dbtab.conf:

from zodb.public import ASConnection, ASStorage, ASDB, StaticResource


class Zope2SQLStorage (ASStorage):

    def __init__(self, **kw):
        dm, conn, gws = createMapper(**kw)
        res = StaticResource(dm)
        ASStorage.__init__(self, res, [conn], **kw)


class Zope2SQLDatabase (ASDB):

    def __init__(self, storage, **kw):
        ASDB.__init__(self, storage, storage._mapper_resource, **kw)