[Zope3-checkins] CVS: Zope3/src/zope/schema - accessors.py:1.1 __init__.py:1.8 _bootstrapfields.py:1.9 interfaces.py:1.7
Jim Fulton
jim@zope.com
Mon, 14 Apr 2003 04:21:47 -0400
Update of /cvs-repository/Zope3/src/zope/schema
In directory cvs.zope.org:/tmp/cvs-serv4964
Modified Files:
__init__.py _bootstrapfields.py interfaces.py
Added Files:
accessors.py
Log Message:
Added support for fields implemented by accessor functions rather than
attributes. See the doc string for accessors.py.
To do this, I added three new field methods: ``get``, ``query``, and
``set``, which are used for getting and setting data defined by
fields. These methods should be used by infrastructure software rather
than assuming that field implementations are accessed as attributes.
=== Added File Zope3/src/zope/schema/accessors.py ===
##############################################################################
#
# Copyright (c) 2003 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.
#
##############################################################################
"""\
Field accessors
===============
Accessors are used to model methods used to access data defgined by fields.
Accessors are fields that work by decorating existing fields.
To define accessors in an interface, use the accessors function::
class IMyInterface(Interface):
getFoo, setFoo = accessors(Text(title=u'Foo', ...))
getBar, = accessors(TextLine(title=u'Foo', readonly=True, ...)
Normally a read accessor and a write accessor are defined. Only a
readaccessor is defined for read-only fields.
Read accessors function as access method sepcifications and as field
specifications. Write accessors are solely method specifications.
$Id: accessors.py,v 1.1 2003/04/14 08:21:16 jim Exp $
"""
from __future__ import generators
from zope.interface.interface import Method
class FieldReadAccessor(Method):
"""Field read accessor
"""
# A read field accessor is a method and a field.
# A read accessor is a decorator of a field, using the given
# fields proprtyoes to provide meta data.
def __init__(self, field):
self.field = field
Method.__init__(self, '')
self.__doc__ = 'get %s' % field.__doc__
self.__implements__ = field.__implements__, Method.__implements__
def getSignatureString(self):
return '()'
def getSignatureInfo(self):
return {'positional': (),
'required': (),
'optional': (),
'varargs': None,
'kwargs': None,
}
def get(self, object):
return getattr(object, self.__name__)()
def query(self, object, default=None):
return getattr(object, self.__name__)()
def set(self, object, value):
if self.readonly:
raise TypeError("Can't set values on read-only fields")
getattr(object, self.writer.__name__)(value)
def __getattr__(self, name):
return getattr(self.field, name)
def bind(self, object):
clone = self.__class__.__new__(self.__class__)
clone.__dict__.update(self.__dict__)
clone.field = self.field.bind(object)
return clone
class FieldWriteAccessor(Method):
def __init__(self, field):
Method.__init__(self, '')
self.field = field
self.__doc__ = 'set %s' % field.__doc__
def getSignatureString(self):
return '(newvalue)'
def getSignatureInfo(self):
return {'positional': ('newvalue',),
'required': ('newvalue',),
'optional': (),
'varargs': None,
'kwargs': None,
}
def accessors(field):
reader = FieldReadAccessor(field)
yield reader
if not field.readonly:
writer = FieldWriteAccessor(field)
reader.writer = writer
yield writer
=== Zope3/src/zope/schema/__init__.py 1.7 => 1.8 ===
--- Zope3/src/zope/schema/__init__.py:1.7 Thu Apr 10 05:34:30 2003
+++ Zope3/src/zope/schema/__init__.py Mon Apr 14 04:21:16 2003
@@ -22,3 +22,4 @@
from zope.schema._field import Password, Dict, Datetime, SourceText
from zope.schema._schema import getFields, getFieldsInOrder
from zope.schema._schema import getFieldNames, getFieldNamesInOrder
+from zope.schema.accessors import accessors
=== Zope3/src/zope/schema/_bootstrapfields.py 1.8 => 1.9 ===
--- Zope3/src/zope/schema/_bootstrapfields.py:1.8 Wed Apr 9 11:51:56 2003
+++ Zope3/src/zope/schema/_bootstrapfields.py Mon Apr 14 04:21:16 2003
@@ -125,6 +125,16 @@
if self.constraint is not None and not self.constraint(value):
raise ValidationError(errornames.ConstraintNotSatisfied, value)
+ def get(self, object):
+ return getattr(object, self.__name__)
+
+ def query(self, object, default=None):
+ return getattr(object, self.__name__, default)
+
+ def set(self, object, value):
+ if self.readonly:
+ raise TypeError("Can't set values on read-only fields")
+ setattr(object, self.__name__, value)
class Container(Field):
=== Zope3/src/zope/schema/interfaces.py 1.6 => 1.7 ===
--- Zope3/src/zope/schema/interfaces.py:1.6 Thu Apr 10 05:34:30 2003
+++ Zope3/src/zope/schema/interfaces.py Mon Apr 14 04:21:16 2003
@@ -165,6 +165,21 @@
readonly=True,
)
+ def get(object):
+ """Get the value of the field for the given object.
+ """
+
+ def query(object, default=None):
+ """Query the value of the field for the given object.
+
+ Return the default if the value hasn't been set.
+ """
+
+ def set(object, value):
+ """Set the value of the field for the object
+
+ Raises a type error if the field is a read-only field.
+ """
class IIterable(IField):
u"""Fields with a value that can be iterated over.