[Zope-CVS] CVS: Products/Ape/lib/apelib/zope2/setup - TmpStore.py:1.1 __init__.py:1.1 dbtab.py:1.1 patches.py:1.1

Shane Hathaway shane@zope.com
Wed, 9 Apr 2003 23:10:00 -0400


Update of /cvs-repository/Products/Ape/lib/apelib/zope2/setup
In directory cvs.zope.org:/tmp/cvs-serv32010/lib/apelib/zope2/setup

Added Files:
	TmpStore.py __init__.py dbtab.py patches.py 
Log Message:
Moved apelib into a "lib" subdirectory.  This simplified the
Python hacking required to make apelib a top-level package.  Sorry
about the flood of checkins, but CVS makes a move like this quite painful.


=== Added File Products/Ape/lib/apelib/zope2/setup/TmpStore.py ===
##############################################################################
#
# Copyright (c) 2001, 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
#
##############################################################################

from ZODB import POSException
from ZODB.utils import p64, u64

import tempfile

class TmpStore:
    """A storage to support subtransactions."""

    _bver = ''

    def __init__(self, base_version):
        self._transaction = None
        if base_version:
            self._bver = base_version
        self._file = tempfile.TemporaryFile()
        # _pos: current file position
        # _tpos: file position at last commit point
        self._pos = self._tpos = 0L
        # _index: map oid to pos of last committed version
        self._index = {}
        # _tindex: map oid to pos for new updates
        self._tindex = {}
        self._db = None
        self._creating = []

    def __del__(self):
        # XXX Is this necessary?
        self._file.close()

    def close(self):
        self._file.close()

    def getName(self):
        return self._db.getName()
    
    def getSize(self):
        return self._pos

    def load(self, oid, version):
        pos = self._index.get(oid)
        if pos is None:
            return self._storage.load(oid, self._bver)
        self._file.seek(pos)
        h = self._file.read(8)
        oidlen = u64(h)
        read_oid = self._file.read(oidlen)
        if read_oid != oid:
            raise POSException.StorageSystemError('Bad temporary storage')
        h = self._file.read(16)
        size = u64(h[8:])
        serial = h[:8]
        return self._file.read(size), serial

    # XXX clarify difference between self._storage & self._db._storage

    def modifiedInVersion(self, oid):
        if self._index.has_key(oid):
            return self._bver
        return self._db._storage.modifiedInVersion(oid)

    def new_oid(self):
        return self._db._storage.new_oid()

    def registerDB(self, db, limit):
        self._db = db
        self._storage = db._storage

    def store(self, oid, serial, data, version, transaction):
        if transaction is not self._transaction:
            raise POSException.StorageTransactionError(self, transaction)
        self._file.seek(self._pos)
        l = len(data)
        if serial is None:
            serial = '\0' * 8
        header = p64(len(oid)) + oid + serial + p64(l)
        self._file.write(header)
        self._file.write(data)
        self._tindex[oid] = self._pos
        self._pos += l + len(header)
        return serial

    def tpc_abort(self, transaction):
        if transaction is not self._transaction:
            return
        self._tindex.clear()
        self._transaction = None
        self._pos = self._tpos

    def tpc_begin(self, transaction):
        if self._transaction is transaction:
            return
        self._transaction = transaction
        self._tindex.clear() # Just to be sure!
        self._pos = self._tpos

    def tpc_vote(self, transaction):
        pass

    def tpc_finish(self, transaction, f=None):
        if transaction is not self._transaction:
            return
        if f is not None:
            f()
        self._index.update(self._tindex)
        self._tindex.clear()
        self._tpos = self._pos

    def undoLog(self, first, last, filter=None):
        return ()

    def versionEmpty(self, version):
        # XXX what is this supposed to do?
        if version == self._bver:
            return len(self._index)


=== Added File Products/Ape/lib/apelib/zope2/setup/__init__.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.
#
##############################################################################
"""Run-time Zope 2 configuration modules.

$Id: __init__.py,v 1.1 2003/04/10 03:09:59 shane Exp $
"""


=== Added File Products/Ape/lib/apelib/zope2/setup/dbtab.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.
#
##############################################################################
"""Configuration of storage types for DBTab/ZODB

$Id: dbtab.py,v 1.1 2003/04/10 03:09:59 shane Exp $
"""

def convertApeStorageArgs(name, factory=None, **kw):
    from apelib.zodb3.db import callMapperFactory
    from apelib.zodb3.resource import StaticResource
    if factory is None:
        raise RuntimeError('factory is required')
    mapper, conns = callMapperFactory(factory, kw)
    return {
        'mapper_resource': StaticResource(mapper),
        'tpc_conns': conns,
        'name': name,
        }

try:
    from Products.DBTab.StorageTypes import storage_types
    from Products.DBTab.DBTab import database_types
except ImportError:
    pass
else:
    storage_types['ApeStorage'] = (
        'apelib.zodb3.storage', convertApeStorageArgs)
    database_types['ApeDB'] = 'apelib.zodb3.db'



=== Added File Products/Ape/lib/apelib/zope2/setup/patches.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.
#
##############################################################################
"""Monkey-patches necessary to make apelib work in Zope.

$Id: patches.py,v 1.1 2003/04/10 03:09:59 shane Exp $
"""

from Acquisition import aq_base

from apelib.zodb3.utils import copyOf


def applyCopySupportPatch():
    # Fall back to copying by pickle when ZEXP export/import is not
    # implemented.
    def _getCopy(self, container):
        try:
            return self._real_getCopy(container)
        except NotImplementedError:
            return copyOf(aq_base(self))

    from OFS.CopySupport import CopySource
    CopySource._real_getCopy = CopySource._getCopy
    CopySource._getCopy = _getCopy


def applySetObPatch():
    # Fall back to copying when move/rename is not possible.
    def _setOb(self, id, object):
        if object._p_oid is not None:
            if (getattr(self, '_use_fixed_oids_', 0)
                or (object._p_jar is not None
                    and object._p_jar is not self._p_jar)):
                old = object
                # Forget changes to the original object
                old._p_changed = 0
                object = copyOf(object)
        setattr(self, id, object)

    from OFS.ObjectManager import ObjectManager
    ObjectManager._setOb = _setOb


def applyTmpStorePatch():
    from TmpStore import TmpStore as patched_TmpStore
    import ZODB.TmpStore
    ZODB.TmpStore.TmpStore = patched_TmpStore

def applyPatches():
    applyCopySupportPatch()
    applySetObPatch()
    applyTmpStorePatch()