[Zope-CVS] CVS: Products/AdaptableStorage/mapper_std - AnyObjectSerializer.py:1.1 FixedClassifier.py:1.1 FixedPersistentMapping.py:1.1 FullState.py:1.1 IgnoredAttribute.py:1.1 MappingGateway.py:1.1 PathKeychainGenerator.py:1.1 RollCall.py:1.1 StringDataAttribute.py:1.1 __init__.py:1.1 mapper_public.py:1.1 public.py:1.1

Shane Hathaway shane@zope.com
Tue, 31 Dec 2002 16:47:49 -0500


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

Added Files:
	AnyObjectSerializer.py FixedClassifier.py 
	FixedPersistentMapping.py FullState.py IgnoredAttribute.py 
	MappingGateway.py PathKeychainGenerator.py RollCall.py 
	StringDataAttribute.py __init__.py mapper_public.py public.py 
Log Message:
Changed the name of the "serial" package to "mapper".  It's a more
appropriate name, since mappers are the focus of this software.

Sorry about the flood of checkins.


=== Added File Products/AdaptableStorage/mapper_std/AnyObjectSerializer.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.
#
##############################################################################
"""Serializer of any persistent object

$Id: AnyObjectSerializer.py,v 1.1 2002/12/31 21:47:47 shane Exp $
"""

from mapper_public import ObjectSerializer


class AnyObjectSerializer (ObjectSerializer):

    __implements__ = ObjectSerializer.__implements__

    def __init__(self):
        self._aspects = []  # [(name, aspect)] -- Order matters.

    def canSerialize(self, object):
        return 1

    def createEmptyInstance(self, classification=None):
        if classification is None:
            # This serializer can't do anything without the classification.
            return None
        cn = classification['class_name']
        module, name = cn.split(':', 1)
        m = __import__(module, {}, {}, ('__doc__',))
        c = getattr(m, name)
        return c.__basicnew__()



=== Added File Products/AdaptableStorage/mapper_std/FixedClassifier.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.
#
##############################################################################
"""Fixed-key classification.

$Id: FixedClassifier.py,v 1.1 2002/12/31 21:47:47 shane Exp $
"""

import os

from mapper_public import IClassifier, DeserializationError


class FixedClassifier:

    __implements__ = IClassifier

    def __init__(self):
        self.key_to_res = {}

    def register(self, key, mapper_name):
        self.key_to_res[key] = ({}, mapper_name)

    def classifyObject(self, value, keychain):
        k = keychain[-1]
        return self.key_to_res[k]

    def classifyState(self, event):
        k = event.getKeychain()[-1]
        return self.key_to_res[k]

    def store(self, event, classification):
        pass



=== Added File Products/AdaptableStorage/mapper_std/FixedPersistentMapping.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.
#
##############################################################################
"""Unchanging persistent mapping.  Generally used for a ZODB root object.

$Id: FixedPersistentMapping.py,v 1.1 2002/12/31 21:47:47 shane Exp $
"""

from mapper_public import IAspectSerializer


class FixedPersistentMapping:
    __implements__ = IAspectSerializer

    def __init__(self):
        # map: { name -> (keychain, mapper) }
        self.map = {}

    def add(self, name, keychain, mapper_names=None):
        self.map[name] = (keychain, mapper_names)

    def getSchema(self):
        return None  # No storage

    def serialize(self, object, event):
        names = object.keys()
        names.sort()
        expected = self.map.keys()
        expected.sort()
        assert names == expected, '%s != %s' % (names, expected)

        for name in names:
            keychain, mapper_names = self.map[name]
            subob = object[name]
            event.notifySerializedRef(name, subob, 0, keychain)

        # One of the two will work. ;-)
        event.ignoreAttribute('data')
        event.ignoreAttribute('_container')


    def deserialize(self, object, event, state):
        assert state is None
        data = {}
        for name, (keychain, mapper_names) in self.map.items():
            subob = event.dereference(name, keychain,
                                      {'mapper_names': mapper_names})
            data[name] = subob
        # The PersistentMapping doesn't have its data or _container
        # attribute yet, and we don't know what its name should be
        # since PersistentMapping's internal structure is not fixed.
        # So call the PersistentMapping's constructor.
        object.__init__(data)



=== Added File Products/AdaptableStorage/mapper_std/FullState.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.
#
##############################################################################
"""Aspect that loads/stores the entire state of an object

$Id: FullState.py,v 1.1 2002/12/31 21:47:47 shane Exp $
"""

from mapper_public import IAspectSerializer, FieldSchema


class FullState:
    __implements__ = IAspectSerializer

    schema = FieldSchema('data', 'object')

    def getSchema(self):
        return self.schema

    def serialize(self, object, event):
        return object.__getstate__()

    def deserialize(self, object, event, state):
        object.__setstate__(state)



=== Added File Products/AdaptableStorage/mapper_std/IgnoredAttribute.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.
#
##############################################################################
"""Aspect that explicitly ignores an attribute

$Id: IgnoredAttribute.py,v 1.1 2002/12/31 21:47:47 shane Exp $
"""


from mapper_public import IAspectSerializer


class IgnoredAttribute:
    __implements__ = IAspectSerializer

    def __init__(self, attrname):
        self.attrname = attrname

    def getSchema(self):
        return None  # No storage

    def serialize(self, object, event):
        event.ignoreAttribute(self.attrname)
        return None

    def deserialize(self, object, event, state):
        assert state is None



=== Added File Products/AdaptableStorage/mapper_std/MappingGateway.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.
#
##############################################################################
"""Gateway to a simple dictionary (primarily for testing)

$Id: MappingGateway.py,v 1.1 2002/12/31 21:47:47 shane Exp $
"""

import time

from mapper_public import IGateway

class MappingGateway:
    """Stores data in a mapping."""

    __implements__ = IGateway

    def __init__(self, schema):
        self.schema = schema
        self.data = {}

    def getSchema(self):
        return self.schema

    def load(self, event):
        # Returns (data, serial)
        return self.data[event.getKeychain()]

    def store(self, event, data):
        serial = time.time()
        self.data[event.getKeychain()] = (data, serial)
        return serial


=== Added File Products/AdaptableStorage/mapper_std/PathKeychainGenerator.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.
#
##############################################################################
"""Path-based keychain generator

$Id: PathKeychainGenerator.py,v 1.1 2002/12/31 21:47:47 shane Exp $
"""

from mapper_public import IKeychainGenerator

class PathKeychainGenerator:
    """Path-based keychain generator
    """
    __implements__ = IKeychainGenerator

    def makeKeychain(self, event, name, stored):
        if name.startswith('.') or '/' in name:
            raise ValueError, '%s is not a legal name' % name
        parent_keychain = event.getKeychain()
        k = parent_keychain[-1]
        if k.endswith('/'):
            k = k + name
        else:
            k = '%s/%s' % (k, name)
        return parent_keychain[:-1] + (k,)


=== Added File Products/AdaptableStorage/mapper_std/RollCall.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.
#
##############################################################################
"""Roll call aspect.

$Id: RollCall.py,v 1.1 2002/12/31 21:47:47 shane Exp $
"""

from mapper_public import \
     IAspectSerializer, IFullSerializationEvent, SerializationError


class RollCall:
    """Helps ensure all parts of an object get serialized.

    Designed for debugging purposes.
    """
    __implements__ = IAspectSerializer

    def getSchema(self):
        return None  # No storage

    def serialize(self, object, tracker):
        assert IFullSerializationEvent.isImplementedBy(tracker)
        attrs = tracker.getSerializedAttributeNames()
        attrs_map = {}
        for attr in attrs:
            attrs_map[attr] = 1
        missed = []
        for k in object.__dict__.keys():
            if not k.startswith('_v_') and not attrs_map.has_key(k):
                missed.append(repr(k))
        if missed:
            raise SerializationError(
                'Attribute(s) %s of %s not serialized' %
                (', '.join(missed), repr(object)))
        return None

    def deserialize(self, object, tracker, state):
        assert state is None




=== Added File Products/AdaptableStorage/mapper_std/StringDataAttribute.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.
#
##############################################################################
"""Aspect for a simple string data attribute.

$Id: StringDataAttribute.py,v 1.1 2002/12/31 21:47:47 shane Exp $
"""

from types import StringType

from mapper_public import IAspectSerializer, FieldSchema


class StringDataAttribute:
    __implements__ = IAspectSerializer

    schema = FieldSchema('data', 'string')

    def __init__(self, attrname):
        self.attrname = attrname

    def getSchema(self):
        return self.schema

    def serialize(self, object, event):
        attrname = self.attrname
        assert attrname
        v = getattr(object, attrname)
        assert isinstance(v, StringType)
        event.notifySerialized(attrname, v, 1)
        return v

    def deserialize(self, object, event, state):
        attrname = self.attrname
        assert attrname
        assert isinstance(state, StringType)
        setattr(object, attrname, state)
        event.notifyDeserialized(attrname, state)



=== Added File Products/AdaptableStorage/mapper_std/__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.
#
##############################################################################
"""Package containing some standard aspect serializers.

$Id: __init__.py,v 1.1 2002/12/31 21:47:47 shane Exp $
"""



=== Added File Products/AdaptableStorage/mapper_std/mapper_public.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.
#
##############################################################################
"""Import of the public classes and interfaces from the serial package.

$Id: mapper_public.py,v 1.1 2002/12/31 21:47:47 shane Exp $
"""

from Products.AdaptableStorage.mapper.public import *



=== Added File Products/AdaptableStorage/mapper_std/public.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.
#
##############################################################################
"""Public implementations in the serial_std package.

$Id: public.py,v 1.1 2002/12/31 21:47:47 shane Exp $
"""

from AnyObjectSerializer import AnyObjectSerializer
from FixedClassifier import FixedClassifier
from FixedPersistentMapping import FixedPersistentMapping
from FullState import FullState
from IgnoredAttribute import IgnoredAttribute
from MappingGateway import MappingGateway
from PathKeychainGenerator import PathKeychainGenerator
from RollCall import RollCall
from StringDataAttribute import StringDataAttribute