DT_With.py
上传用户:gyjinxi
上传日期:2007-01-04
资源大小:159k
文件大小:6k
源码类别:

WEB邮件程序

开发平台:

Python

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. '''Nested namespace access
  65.    The 'with' tag is used to introduce nested namespaces.
  66.    The text enclosed in the with tag is rendered using information
  67.    from the given variable or expression.
  68.    For example, if the variable 'person' is bound to an object that
  69.    has attributes 'name' and 'age', then a 'with' tag like the
  70.    following can be used to access these attributes::
  71.      <!--#with person-->
  72.        <!--#var name-->,
  73.        <!--#var age-->
  74.      <!--#/with-->
  75.    Eather a 'name' or an 'expr' attribute may be used to specify data.
  76.    A 'mapping' attribute may be used to indicate that the given data
  77.    should be treated as mapping object, rather than as an object with
  78.    named attributes.
  79. ''' 
  80. __rcs_id__='$Id: DT_With.py,v 1.10 1999/03/10 00:15:08 klm Exp $'
  81. __version__='$Revision: 1.10 $'[11:-2]
  82. from DT_Util import parse_params, name_param, InstanceDict, render_blocks, str
  83. from DT_Util import TemplateDict
  84. class With:
  85.     blockContinuations=()
  86.     name='with'
  87.     mapping=None
  88.     only=0
  89.     
  90.     def __init__(self, blocks):
  91.         tname, args, section = blocks[0]
  92.         args=parse_params(args, name='', expr='', mapping=1, only=1)
  93.         name,expr=name_param(args,'with',1)
  94.         if expr is None: expr=name
  95.         else: expr=expr.eval
  96.         self.__name__, self.expr = name, expr
  97.         self.section=section.blocks
  98.         if args.has_key('mapping') and args['mapping']: self.mapping=1
  99.         if args.has_key('only') and args['only']: self.only=1
  100.     def render(self, md):
  101.         expr=self.expr
  102.         if type(expr) is type(''): v=md[expr]
  103.         else: v=expr(md)
  104.         if not self.mapping:
  105.             if type(v) is type(()) and len(v)==1: v=v[0]
  106.             v=InstanceDict(v,md)
  107.         if self.only:
  108.             _md=md
  109.             md=TemplateDict()
  110.             if hasattr(_md, 'AUTHENTICATED_USER'):
  111.                 md.AUTHENTICATED_USER=_md.AUTHENTICATED_USER
  112.             if hasattr(_md, 'validate'):
  113.                 md.validate=_md.validate
  114.         md._push(v)
  115.         try: return render_blocks(self.section, md)
  116.         finally: md._pop(1)
  117.     __call__=render