[Zope-CVS] CVS: Products/AdaptableStorage/serial_ofs - .cvsignore:1.1 FolderItems.py:1.1 IdAttribute.py:1.1 MetaTypeClassifier.py:1.1 __init__.py:1.1 public.py:1.1 serial_public.py:1.1

Shane Hathaway shane@zope.com
Wed, 27 Nov 2002 13:37:09 -0500


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

Added Files:
	.cvsignore FolderItems.py IdAttribute.py MetaTypeClassifier.py 
	__init__.py public.py serial_public.py 
Log Message:
Moved the latest AdaptableStorage work out of the private repository.
It took a long time, but I moved it as soon as all the unit tests
passed and I felt that all the interface names and conventions were
good enough.

Documentation is still minimal, but now I think the system is finally
straight enough in my head to write down. :-) If you want a sneak
peek, the interfaces have some docstrings, if you're looking for a
"tree" view, while the OpenOffice diagram presents something of a
"forest" view.

Also note that I'm trying a new coding convention.  The "public"
module in each package defines exactly which objects should be
exported from the package.  This solves a few problems with imports
such as doubling of names and shadowing of modules.  Overall, the
"public" module makes it easier to tell which classes are supposed to
be used by other packages, and makes it easier for other packages to
use the public classes.  See what you think.



=== Added File Products/AdaptableStorage/serial_ofs/.cvsignore ===
*.pyc


=== Added File Products/AdaptableStorage/serial_ofs/FolderItems.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 for the items in an ObjectManager.

$Id: FolderItems.py,v 1.1 2002/11/27 18:37:07 shane Exp $
"""


from Acquisition import aq_base
from OFS.ObjectManager import ObjectManager

from serial_public import IAspectSerializer, RecordSchema


class FolderItems:
    """ """
    __implements__ = IAspectSerializer
    __used_for__ = ObjectManager

    schema = RecordSchema()
    schema.addColumn('id', 'string', 1)
    schema.addColumn('classification', 'classification', 0)

    def getSchema(self):
        return self.schema

    def serialize(self, object, event):
        assert isinstance(object, ObjectManager)
        state = []
        event.ignoreAttribute('_objects')
        for id, subob in object.objectItems():
            classification, mapper_name = event.classifyObject(subob)
            event.notifySerializedRef(
                id, aq_base(subob), attribute=1,
                mapper_name=mapper_name, stored_key=0)
            state.append((id, classification))
        return state

    def deserialize(self, object, event, state):
        assert isinstance(object, ObjectManager)
        for id, classification in state:
            subob = event.dereference(id, classification=classification)
            setattr(object, id, subob)
            object._objects += ({'id': id, 'meta_type':
                                 subob.__class__.meta_type},)



=== Added File Products/AdaptableStorage/serial_ofs/IdAttribute.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.
#
##############################################################################
"""Zope 2 id attribute.

$Id: IdAttribute.py,v 1.1 2002/11/27 18:37:07 shane Exp $
"""

from OFS.SimpleItem import Item_w__name__

from serial_public import IAspectSerializer, RecordSchema


class IdAttribute:
    __implements__ = IAspectSerializer

    schema = RecordSchema()
    schema.addColumn('id', 'string')

    def getSchema(self):
        return self.schema

    def getAttrName(self, object):
        if isinstance(object, Item_w__name__):
            return '__name__'
        else:
            return 'id'

    def serialize(self, object, event):
        attrname = self.getAttrName(object)
        id = getattr(object, attrname)
        assert id, 'ID of %r is %r' % (object, id)
        event.notifySerialized(attrname, id, 1)
        return ((id,),)

    def deserialize(self, object, event, state):
        attrname = self.getAttrName(object)
        assert attrname
        assert len(state) == 1
        assert len(state[0]) == 1
        id = state[0][0]
        setattr(object, attrname, id)
        event.notifyDeserialized(attrname, id)



=== Added File Products/AdaptableStorage/serial_ofs/MetaTypeClassifier.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.
#
##############################################################################
"""Zope 2 meta_type based classification.

$Id: MetaTypeClassifier.py,v 1.1 2002/11/27 18:37:07 shane Exp $
"""

import os

from Acquisition import aq_base

from serial_public import IClassifier, DeserializationError


class MetaTypeClassifier:

    __implements__ = IClassifier

    def __init__(self):
        self.ext_to_mt = {}
        self.mt_to_mapper = {}

    def register(self, meta_type, mapper_name, extensions):
        for ext in extensions:
            if not ext.startswith('.'):
                ext = '.' + ext
            ext = ext.lower()
            self.ext_to_mt[ext] = meta_type
        self.mt_to_mapper[meta_type] = mapper_name


    def registerNodeTypeDefault(self, meta_type, mapper_name, isdir):
        if isdir:
            ext = '<directory>'
        else:
            ext = '<file>'
        self.ext_to_mt[ext] = meta_type
        self.mt_to_mapper[meta_type] = mapper_name


    def registerObjectTypeDefault(self, mapper_name, folderish):
        if folderish:
            mt = '(folderish object)'
        else:
            mt = '(non-folderish object)'
        self.mt_to_mapper[mt] = mapper_name


    def classifyObject(self, value):
        mt = value.meta_type
        mapper_name = self.mt_to_mapper.get(mt)
        if mapper_name is None:
            folderish = not not getattr(aq_base(value), 'isPrincipiaFolderish')
            # Use a special meta_type.
            if folderish:
                smt = '(folderish object)'
            else:
                smt = '(non-folderish object)'
            mapper_name = self.mt_to_mapper.get(smt)
            if mapper_name is None:
                raise DeserializationError(
                    'No mapper known for meta_type %s' % repr(mt))
            mt = smt
        return {'meta_type': mt}, mapper_name


    def classifyFilename(self, filename, isdir):
        if isdir:
            mt = self.ext_to_mt.get('<directory>', 'Folder')
        else:
            name, ext = os.path.splitext(filename)
            mt = self.ext_to_mt.get(ext.lower())
            if not mt:
                mt = self.ext_to_mt.get('<file>')
            if not mt:
                mt = 'File'
        mapper_name = self.mt_to_mapper.get(mt)
        if mapper_name is None:
            raise DeserializationError(
                'No mapper known for meta_type %s' % repr(mt))
        return {'meta_type': mt}, mapper_name


    def chooseMapper(self, classification):
        mt = classification['meta_type']
        mapper_name = self.mt_to_mapper.get(mt)
        if mapper_name is None:
            raise DeserializationError(
                'No mapper known for meta_type %s' % repr(mt))
        return mapper_name



# Old cruft:

meta_types = {
    '<directory>': 'Folder',
    '<file>': 'File',
    '.html': 'Page Template',
    '.dtml': 'DTML Method',
    '.gif': 'Image',
    '.jpg': 'Image',
    '.jpeg': 'Image',
    }



=== Added File Products/AdaptableStorage/serial_ofs/__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.
#
##############################################################################
"""Zope 2 aspect serializers.

$Id: __init__.py,v 1.1 2002/11/27 18:37:07 shane Exp $
"""



=== Added File Products/AdaptableStorage/serial_ofs/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.
#
##############################################################################
"""serial_ofs public names

$Id: public.py,v 1.1 2002/11/27 18:37:07 shane Exp $
"""

from FolderItems import FolderItems
from IdAttribute import IdAttribute
from MetaTypeClassifier import MetaTypeClassifier



=== Added File Products/AdaptableStorage/serial_ofs/serial_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: serial_public.py,v 1.1 2002/11/27 18:37:07 shane Exp $
"""

from Products.AdaptableStorage.serial.public import *