[Zope-CVS] CVS: Products/AdaptableStorage/serial_ofs - ZSQLMethodSerializer.py:1.1.2.1

Christian Zagrodnick cz@gocept.com
Mon, 13 Jan 2003 17:10:53 -0500


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

Added Files:
      Tag: zagy-patches
	ZSQLMethodSerializer.py 
Log Message:
Serializers for the action sql statement and ZSQL's properties.


=== Added File Products/AdaptableStorage/serial_ofs/ZSQLMethodSerializer.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 Z SQL Method

$Id: ZSQLMethodSerializer.py,v 1.1.2.1 2003/01/13 22:10:50 zagy Exp $
"""

import re
from types import StringType

from Products.ZSQLMethods.SQL import SQL
from Shared.DC.ZRDB.Aqueduct import parse
try: from IOBTree import Bucket
except: Bucket=lambda:{}

from mapper_public import IAspectSerializer, FieldSchema, RowSequenceSchema


class ZSQLMethodSerializer:
    """Serializer for ZSQLMethods.

        ZSQLMethodSerializer serializes using the same representation
        as FTP or WebDAV.  All computable attributes like compiled code
        are dropped.
    """
        
    __implements__ = IAspectSerializer

    schema = FieldSchema('data', 'string')
    attributes = ('arguments_src', 'src')
    
    _p_PARAMS = re.compile(r'\s*<params>(.*)</params>\s*\n', re.I | re.S)
    
    def getSchema(self):
        return self.schema

    def canSerialize(self, object):
        return isinstance(object, SQL)

    def serialize(self, object, event):
        data = object.document_src()
        for attr in self.attributes:
            event.notifySerialized(attr, getattr(object, attr), 1)
        for attr in ('_arg', 'template'):
            event.ignoreAttribute(attr)
   
        return data
        
    def deserialize(self, object, event, state):
        assert isinstance(state, StringType)
        assert isinstance(object, SQL)
        body = state
        m = self._p_PARAMS.match(body)
        if m:
            object.arguments_src = m.group(1)
            body = body[m.end():]
        else:
            object.argument_src = ''
        object._arg = parse(object.arguments_src)
        object.src = body
        object.template = object.template_class(body)
        object.template.cook()
        object._v_cache = {}, Bucket()
        for attr in self.attributes:
            event.notifyDeserialized(attr, getattr(object, attr))

class ZSQLMethodPropertiesSerializer:
   __implements__ = IAspectSerializer
    
    schema = RowSequenceSchema()
    schema.addField('id', 'string', 1)
    schema.addField('type', 'string')
    schema.addField('data', 'string')

    attributes = {
        'title': str,
        'connection_id': str,
        'max_rows_': int,
        'max_cache_': int,
        'cache_time': int, 
        'class_name_': str, 
        'class_file_': str, 
        'zclass': str, # XXX, what's that 
        'allow_simple_one_argument_traversal': int,
        'connection_hook': str, 
    }

    def getSchema(self):
        return self.schema

    def canSerialize(self, object):
        return isinstance(object, SQL)

    def serialize(self, object, event):
        assert isinstance(object, SQL)
        res = []
        for attribute, factory in self.attributes.items():
            if not hasattr(object, attribute):
                continue
            value = getattr(object, attribute)
            t = factory.__name__
            if value is None:
                if factory in (int, long):
                    value = 0
                else: 
                    value = ''
            value = str(value)
            event.notifySerialized(attribute, value, 1)
            res.append((attribute, t, value))
        event.ignoreAttribute('_col') 
        return res 
        
    def deserialize(self, object, event, state):
        assert isinstance(object, SQL)
        for attribute, t, value in state:
            factory = self.attributes.get(attribute)
            if factory is None:
                continue
            value = factory(value)
            setattr(object, attribute, value)
            event.notifyDeserialized(attribute, value)