Back to index

plone3  3.1.7
UserPropertySheet.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights
00004 # Reserved.
00005 #
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this
00008 # distribution.
00009 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00010 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00011 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00012 # FOR A PARTICULAR PURPOSE.
00013 #
00014 ##############################################################################
00015 """ Represent a group of properties about a user.
00016 
00017 $Id: UserPropertySheet.py 74710 2007-04-24 16:53:47Z tseaver $
00018 """
00019 from types import IntType
00020 from types import FloatType
00021 from types import LongType
00022 from types import TupleType
00023 from types import ListType
00024 from types import InstanceType
00025 from types import BooleanType
00026 
00027 try:
00028     from types import StringTypes
00029 except ImportError:
00030     from types import StringType
00031     from types import UnicodeType
00032     StringTypes = ( StringType, UnicodeType )
00033 
00034 _SequenceTypes = ( TupleType, ListType )
00035 
00036 from DateTime.DateTime import DateTime
00037 
00038 from OFS.Image import Image
00039 
00040 from Products.PluggableAuthService.utils import classImplements
00041 from Products.PluggableAuthService.interfaces.propertysheets \
00042     import IPropertySheet
00043 
00044 
00045 def _guessSchema( kw ):
00046 
00047     schema = []
00048     for k, v in kw.items():
00049 
00050         ptype = 'string'
00051 
00052         if type( v ) is IntType:
00053             ptype = 'int'
00054 
00055         elif type( v ) is FloatType:
00056             ptype = 'float'
00057 
00058         elif type( v ) is LongType:
00059             ptype = 'long'
00060 
00061         elif type( v ) is BooleanType:
00062             ptype = 'boolean'
00063 
00064         elif type( v ) in _SequenceTypes:
00065 
00066             if v and type( v[0] ) not in StringTypes:
00067                 raise ValueError, 'Property %s: sequence items not strings' % k
00068 
00069             ptype = 'lines'
00070 
00071         elif type( v ) is InstanceType:
00072 
00073             if isinstance( v, DateTime ):
00074                 ptype = 'date'
00075             else:
00076                 raise ValueError, 'Property %s: unknown class' % k
00077 
00078         elif isinstance( v, Image ):
00079             ptype = 'image'
00080 
00081         elif type( v ) not in StringTypes:
00082             raise ValueError, 'Property %s: unknown type' % k
00083 
00084         schema.append( ( k, ptype ) )
00085 
00086     return schema
00087 
00088 class UserPropertySheet:
00089 
00090     """ Model a single, read-only set of properties about a user.
00091 
00092     o Values for the sheet are passed as keyword args to the c'tor.
00093 
00094     o The schema for the sheet may be passed into the c'tor explicitly
00095       as a sequence of (id, type) tuples;  if not passed, the c'tor will
00096       guess the schema from the keyword args.
00097     """
00098 
00099     def __init__( self, id, schema=None, **kw ):
00100 
00101         self._id = id
00102 
00103         if schema is None:
00104             schema = _guessSchema( kw )
00105 
00106         self._schema = tuple( schema )
00107         self._properties = {}
00108 
00109         for id, ptype in schema:
00110 
00111             value = kw.get( id )
00112 
00113             if ptype == 'lines':
00114                 if value is not None:
00115                     value = tuple( value )
00116 
00117             self._properties[ id ] = value
00118 
00119     #
00120     #   IPropertySheet implementation
00121     #
00122     def getId( self ):
00123 
00124         """ See IPropertySheet.
00125         """
00126         return self._id
00127 
00128     def hasProperty( self, id ):
00129 
00130         """ See IPropertySheet.
00131         """
00132         return id in self.propertyIds()
00133 
00134     def getProperty( self, id, default=None ):
00135 
00136         """ See IPropertySheet.
00137         """
00138         return self._properties.get( id, default )
00139 
00140     def getPropertyType( self, id ):
00141 
00142         """ See IPropertySheet.
00143         """
00144         found = [ x[1] for x in self._schema if x[0] == id ]
00145 
00146         return found and found[0] or None
00147 
00148     def propertyInfo( self, id ):
00149 
00150         """ See IPropertySheet.
00151         """
00152         for schema_id, ptype in self._schema:
00153 
00154             if schema_id == id:
00155                 return { 'id' : id, 'type' : ptype, 'mode' : '' }
00156 
00157         return None
00158 
00159     def propertyMap( self ):
00160 
00161         """ See IPropertySheet.
00162         """
00163         result = []
00164 
00165         for id, ptype in self._schema:
00166             result.append( { 'id' : id, 'type' : ptype, 'mode' : '' } )
00167 
00168         return tuple( result )
00169 
00170     def propertyIds( self ):
00171 
00172         """ See IPropertySheet.
00173         """
00174         return [ x[0] for x in self._schema ]
00175 
00176     def propertyValues( self ):
00177 
00178         """ See IPropertySheet.
00179         """
00180         return [ self._properties.get( x ) for x in self.propertyIds() ]
00181 
00182     def propertyItems( self ):
00183         """ See IPropertySheet.
00184         """
00185         return [ ( x, self._properties.get( x ) ) for x in self.propertyIds() ]
00186 
00187 classImplements( UserPropertySheet
00188                , IPropertySheet
00189                )