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

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. ''' The Let tag was contributed to Zope by and is copyright, 1999
  65.     Phillip J. Eby.  Permission has been granted to release the Let tag
  66.     under the Zope Public License.
  67.     
  68.    Let name=value...
  69.    The 'let' tag is used to bind variables to values within a block.
  70.    The text enclosed in the let tag is rendered using information
  71.    from the given variables or expressions.
  72.    For example::
  73.      <!--#let foofunc="foo()" my_bar=bar-->
  74.        foo() = <!--#var foofunc-->,
  75.        bar = <!--#var my_bar-->
  76.      <!--#/let-->
  77.    Notice that both 'name' and 'expr' style attributes may be used to
  78.    specify data.  'name' style attributes (e.g. my_bar=bar) will be
  79.    rendered as they are for var/with/in/etc.  Quoted attributes will
  80.    be treated as Python expressions.
  81.    Variables are processed in sequence, so later assignments can
  82.    reference and/or overwrite the results of previous assignments,
  83.    as desired.
  84. ''' 
  85. from DT_Util import render_blocks, str, Eval, expr_globals, Eval, ParseError, regex, strip
  86. class Let:
  87.     blockContinuations=()
  88.     name='let'
  89.     
  90.     def __init__(self, blocks):
  91.         tname, args, section = blocks[0]
  92.         self.__name__ = args
  93.         self.section = section.blocks
  94.         self.args = args = parse_let_params(args)
  95.         for i in range(len(args)):
  96.             name,expr = args[i]
  97.             if expr[:1]=='"' and expr[-1:]=='"' and len(expr) > 1:
  98. # expr shorthand
  99.                 expr=expr[1:-1]
  100.                 try: args[i] = name, Eval(expr, expr_globals).eval
  101.                 except SyntaxError, v:
  102.                     m,(huh,l,c,src) = v
  103.                     raise ParseError, (
  104.                         '<strong>Expression (Python) Syntax error</strong>:'
  105.                         'n<pre>n%sn</pre>n' % v[0],
  106.                         'let')
  107.     def render(self, md):
  108.         d={}; md._push(d)
  109.         try:
  110.             for name,expr in self.args:
  111.                 if type(expr) is type(''): d[name]=md[expr]
  112.                 else: d[name]=expr(md)
  113.             return render_blocks(self.section, md)
  114.         finally: md._pop(1)
  115.     __call__ = render
  116. def parse_let_params(text,
  117.             result=None,
  118.             tag='let',
  119.             parmre=regex.compile(
  120.                 '([- ]*([^- ="]+)=([^- ="]+))'),
  121.             qparmre=regex.compile(
  122.                 '([- ]*([^- ="]+)="([^"]*)")'),
  123.             **parms):
  124.     result=result or []
  125.     if parmre.match(text) >= 0:
  126.         name=parmre.group(2)
  127.         value=parmre.group(3)
  128.         l=len(parmre.group(1))
  129.     elif qparmre.match(text) >= 0:
  130.         name=qparmre.group(2)
  131.         value='"%s"' % qparmre.group(3)
  132.         l=len(qparmre.group(1))
  133.     else:
  134.         if not text or not strip(text): return result
  135.         raise ParseError, ('invalid parameter: "%s"' % text, tag)
  136.     
  137.     result.append((name,value))
  138.     text=strip(text[l:])
  139.     if text: return apply(parse_let_params,(text,result,tag),parms)
  140.     else: return result