ó
ži‚Nc           @   s0  d  Z  d d l m Z d d l m Z m Z m Z m Z d d l Z d d l	 Z	 d d l
 Z
 d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d S(   s…   Provides the Template class, a facade for parsing, generating and executing
template strings, as well as template runtime operations.iÿÿÿÿ(   t   Lexer(   t   runtimet   utilt
   exceptionst   codegenNt   Templatec           B   sÔ   e  Z d  Z d d d e d d d d d d d d d d e e d d e d d e d „ Z d „  Z e d „  ƒ Z	 e d „  ƒ Z
 e d „  ƒ Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z e d „  ƒ Z RS(   s"  Represents a compiled template.
 
    :class:`.Template` includes a reference to the original
    template source (via the ``.source`` attribute) 
    as well as the source code of the
    generated Python module (i.e. the ``.code`` attribute), 
    as well as a reference to an actual Python module.

    :class:`.Template` is constructed using either a literal string
    representing the template text, or a filename representing a filesystem
    path to a source file.
 
    :param text: textual template source.  This argument is mutually
     exclusive versus the "filename" parameter.

    :param filename: filename of the source template.  This argument is 
     mutually exclusive versus the "text" parameter.

    :param buffer_filters: string list of filters to be applied
     to the output of %defs which are buffered, cached, or otherwise
     filtered, after all filters
     defined with the %def itself have been applied. Allows the
     creation of default expression filters that let the output
     of return-valued %defs "opt out" of that filtering via
     passing special attributes or objects.

    :param bytestring_passthrough: When True, and output_encoding is 
     set to None, and :meth:`.Template.render` is used to render,
     the StringIO or cStringIO buffer will be used instead of the
     default "fast" buffer.   This allows raw bytestrings in the
     output stream, such as in expressions, to pass straight
     through to the buffer.   New in 0.4 to provide the same
     behavior as that of the previous series.  This flag is forced
     to True if disable_unicode is also configured.

    :param cache_dir: Filesystem directory where cache files will be
     placed.  See :ref:`caching_toplevel`.

    :param cache_enabled: Boolean flag which enables caching of this
     template.  See :ref:`caching_toplevel`.

    :param cache_type: Type of Beaker caching to be applied to the 
     template. See :ref:`caching_toplevel`.
 
    :param cache_url: URL of a memcached server with which to use
     for caching.  See :ref:`caching_toplevel`.

    :param default_filters: List of string filter names that will
     be applied to all expressions.  See :ref:`filtering_default_filters`.

    :param disable_unicode: Disables all awareness of Python Unicode
     objects.  See :ref:`unicode_disabled`.

    :param encoding_errors: Error parameter passed to ``encode()`` when
     string encoding is performed. See :ref:`usage_unicode`.
 
    :param error_handler: Python callable which is called whenever
     compile or runtime exceptions occur. The callable is passed
     the current context as well as the exception. If the
     callable returns ``True``, the exception is considered to
     be handled, else it is re-raised after the function
     completes. Is used to provide custom error-rendering
     functions.
 
    :param format_exceptions: if ``True``, exceptions which occur during
     the render phase of this template will be caught and
     formatted into an HTML error page, which then becomes the
     rendered result of the :meth:`render` call. Otherwise,
     runtime exceptions are propagated outwards.
 
    :param imports: String list of Python statements, typically individual
     "import" lines, which will be placed into the module level
     preamble of all generated Python modules. See the example
     in :ref:`filtering_default_filters`.

    :param input_encoding: Encoding of the template's source code.  Can
     be used in lieu of the coding comment. See
     :ref:`usage_unicode` as well as :ref:`unicode_toplevel` for
     details on source encoding.
 
    :param lookup: a :class:`.TemplateLookup` instance that will be used
     for all file lookups via the ``<%namespace>``,
     ``<%include>``, and ``<%inherit>`` tags. See
     :ref:`usage_templatelookup`.
 
    :param module_directory: Filesystem location where generated 
     Python module files will be placed.

    :param module_filename: Overrides the filename of the generated 
     Python module file. For advanced usage only.
 
    :param output_encoding: The encoding to use when :meth:`.render` 
     is called.  
     See :ref:`usage_unicode` as well as :ref:`unicode_toplevel`.
 
    :param preprocessor: Python callable which will be passed 
     the full template source before it is parsed. The return
     result of the callable will be used as the template source
     code.
 
    :param strict_undefined: Replaces the automatic usage of 
     ``UNDEFINED`` for any undeclared variables not located in
     the :class:`.Context` with an immediate raise of
     ``NameError``. The advantage is immediate reporting of
     missing variables which include the name. New in 0.3.6.
 
    :param uri: string uri or other identifier for this template. 
     If not provided, the uri is generated from the filesystem
     path, or from the in-memory identity of a non-file-based
     template. The primary usage of the uri is to provide a key
     within :class:`.TemplateLookup`, as well as to generate the
     file path of the generated Python module file, if
     ``module_directory`` is specified.
 
    t   strictc         C   s0  | r* t  j d d | ƒ |  _ | |  _ n‹ | r t  j d d | ƒ |  _ t j j | ƒ \ } } t j j | ƒ j t j j	 d ƒ } | |  _ n% d t
 t |  ƒ ƒ |  _ |  j |  _ |  j } | j d ƒ rÚ | d } n  t j j | ƒ } | j d ƒ rt j d |  j ƒ ‚ n  | |  _ | |  _ | |  _ | |  _ | pA| |  _ | |  _ t j rq| rqt j d ƒ ‚ n | r| rt j d	 ƒ ‚ n  | d  k rËt j s­|  j r¼d
 g |  _ qÔd g |  _ n	 | |  _ | |  _ | |  _ | |  _ | d  k	 rAt |  | | ƒ \ } } | |  _ | |  _ t  | d  |  | | | ƒ n‹ | d  k	 r½| d  k	 rb| } nF |	 d  k	 r¢t j j! t j j" t j j |	 ƒ | d ƒ ƒ } n d  } |  j# | | ƒ } n t j$ d ƒ ‚ | |  _% | |  _& |  j% j' |  _( | |  _) | |  _* | |  _+ |
 |  _, | |  _- | |  _. | |  _/ d  S(   Ns   \Wt   _t   /s   memory:i   s   ..sN   Template uri "%s" is invalid - it cannot be relative outside of the root path.s4   Mako for Python 3 does not support disabling UnicodesA   output_encoding must be set to None when disable_unicode is used.t   strt   unicodes   .pys"   Template requires text or filename(0   t   ret   subt	   module_idt   urit   ost   patht
   splitdrivet   normpatht   replacet   sept   hext   idt
   startswithR   t   TemplateLookupExceptiont   input_encodingt   output_encodingt   encoding_errorst   disable_unicodet   bytestring_passthrought   strict_undefinedR   t   py3kt   UnsupportedErrort   Nonet   default_filterst   buffer_filterst   importst   preprocessort   _compile_textt   _codet   _sourcet
   ModuleInfot   abspatht   joint   _compile_from_filet   RuntimeExceptiont   modulet   filenamet   render_bodyt	   callable_t   format_exceptionst   error_handlert   lookupt
   cache_typet	   cache_dirt	   cache_urlt   cache_enabled(   t   selft   textR/   R   R2   R3   R4   R   R   t   module_directoryR5   R6   R7   t   module_filenameR   R   R   R"   R#   R   R$   R%   R8   t   driveR   t   u_normt   codeR.   (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyt   __init__„   s‚    $																								c         C   s”  | d  k	 r>t j t j j | ƒ ƒ t j | ƒ t j } t j j | ƒ sj t j | ƒ t j | k  r t	 |  t
 | d ƒ j ƒ  | | ƒ n  t j |  j | t
 | d ƒ ƒ } t j |  j =| j t j k r"t	 |  t
 | d ƒ j ƒ  | | ƒ t j |  j | t
 | d ƒ ƒ } t j |  j =n  t | | |  | d  d  ƒ nR t |  t
 | d ƒ j ƒ  | ƒ \ } } d  |  _ | |  _ t | d  |  | | d  ƒ | S(   Nt   rb(   R!   R   t   verify_directoryR   R   t   dirnamet   statt   ST_MTIMEt   existst   _compile_module_filet   opent   readt   impt   load_sourceR   t   syst   modulest   _magic_numberR   t   MAGIC_NUMBERR)   R&   R(   R'   (   R9   R   R/   t	   filemtimeR.   R?   (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyR,   ð   s:    
!!		c         C   s   t  |  j ƒ j S(   s2   return the template source code for this Template.(   t   _get_module_info_from_callableR1   t   source(   R9   (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyRR     s    c         C   s   t  |  j ƒ j S(   s/   return the module source code for this Template(   RQ   R1   R?   (   R9   (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyR?     s    c         C   s
   |  j  j S(   N(   R.   t   _template_cache(   R9   (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyt   cache  s    c         O   s   t  j |  |  j | | ƒ S(   sì  Render the output of this template as a string.
 
        if the template specifies an output encoding, the string
        will be encoded accordingly, else the output is raw (raw
        output uses cStringIO and can't handle multibyte
        characters). a Context object is created corresponding
        to the given data. Arguments that are explictly declared
        by this template's internal rendering method are also
        pulled from the given \*args, \**data members.
 
        (   R   t   _renderR1   (   R9   t   argst   data(    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyt   render"  s    c         O   s   t  j |  |  j | | d t ƒS(   s7   render the output of this template as a unicode object.t
   as_unicode(   R   RU   R1   t   True(   R9   RV   RW   (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyt   render_unicode0  s
    	c         O   sD   t  | d d ƒ d k r$ |  | _ n  t j |  |  j | | | Ž d S(   so   Render this Template with the given context. 
 
        the data is written to the context's buffer.
 
        t   _with_templateN(   t   getattrR!   R\   R   t   _render_contextR1   (   R9   t   contextRV   t   kwargs(    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyt   render_context9  s    	c         C   s   t  |  j d | ƒ S(   Ns	   render_%s(   t   hasattrR.   (   R9   t   name(    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyt   has_defG  s    c         C   s   t  |  t |  j d | ƒ ƒ S(   s9   Return a def of this template as a :class:`.DefTemplate`.s	   render_%s(   t   DefTemplateR]   R.   (   R9   Rc   (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyt   get_defJ  s    c         C   s   t  |  j d | ƒ S(   Ns	   render_%s(   R]   R.   (   R9   Rc   (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyt   _get_def_callableO  s    c         C   s
   |  j  j S(   N(   R.   t   _modified_time(   R9   (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyt   last_modifiedR  s    N(    (   t   __name__t
   __module__t   __doc__R!   t   FalseRZ   R@   R,   t   propertyRR   R?   RT   RX   R[   Ra   Rd   Rf   Rg   Ri   (    (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyR      sD   sV	"							t   ModuleTemplatec           B   sG   e  Z d  Z d d d d d d d e e e d d d d d e d „ Z RS(   s.  A Template which is constructed given an existing Python module.
 
        e.g.::
 
        t = Template("this is a template")
        f = file("mymodule.py", "w")
        f.write(t.code)
        f.close()
 
        import mymodule
 
        t = ModuleTemplate(mymodule)
        print t.render()
 
    R   c      
   C   s  t  j d d | j ƒ |  _ | j |  _ | j |  _ | |  _ | |  _ |	 |  _	 |
 pW |	 |  _
 t j r~ |	 r~ t j d ƒ ‚ n | rœ |	 rœ t j d ƒ ‚ n  | |  _ | |  _ t | | |  | | | ƒ |  j j |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   Ns   \WR   s4   Mako for Python 3 does not support disabling UnicodesA   output_encoding must be set to None when disable_unicode is used.(   R   R   t   _template_uriR   R   t   _source_encodingR   R   R   R   R   R   R   R   R    R.   R/   R)   R0   R1   R2   R3   R4   R5   R6   R7   R8   (   R9   R.   R<   t   templatet   template_filenamet   module_sourcet   template_sourceR   R   R   R   R2   R3   R4   R5   R6   R7   R8   (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyR@   g  s:    											N(   Rj   Rk   Rl   R!   Rm   RZ   R@   (    (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyRo   V  s"   Re   c           B   s    e  Z d  Z d „  Z d „  Z RS(   sD   a Template which represents a callable def in a parent
    template.c         C   sj   | |  _  | |  _ | j |  _ | j |  _ | j |  _ | j |  _ | j |  _ | j |  _ | j |  _ d  S(   N(	   t   parentR1   R   R.   R   R2   R3   R4   R   (   R9   Rv   R1   (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyR@      s    		c         C   s   |  j  j | ƒ S(   N(   Rv   Rf   (   R9   Rc   (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyRf   «  s    (   Rj   Rk   Rl   R@   Rf   (    (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyRe   œ  s   	R)   c           B   sA   e  Z d  Z e j ƒ  Z d „  Z e d „  ƒ Z e d „  ƒ Z	 RS(   s¯   Stores information about a module currently loaded into
    memory, provides reverse lookups of template source, module
    source code based on a module's identifier.
 
     c         C   s^   | |  _  | |  _ | |  _ | |  _ | |  _ |  |  j | j <| _ | rZ |  |  j | <n  d  S(   N(   R.   R<   Rs   Rt   Ru   t   _modulesRj   t   _mmarker(   R9   R.   R<   Rr   Rs   Rt   Ru   (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyR@   ¶  s    					c         C   s-   |  j  d  k	 r |  j  St |  j ƒ j ƒ  Sd  S(   N(   Rt   R!   RH   R<   RI   (   R9   (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyR?   Æ  s    c         C   s–   |  j  d  k	 rN |  j j rD t |  j  t ƒ rD |  j  j |  j j ƒ S|  j  SnD |  j j r t |  j d ƒ j	 ƒ  j |  j j ƒ St |  j ƒ j	 ƒ  Sd  S(   NRA   (
   Ru   R!   R.   Rq   t
   isinstanceR
   t   decodeRH   Rs   RI   (   R9   (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyRR   Í  s    	
(
   Rj   Rk   Rl   t   weakreft   WeakValueDictionaryRw   R@   Rn   R?   RR   (    (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyR)   ®  s
   	c   
      B   sÿ   |  j  } e | | d |  j d |  j d |  j ƒ} | j ƒ  } e j | |  j | d |  j	 d |  j
 d |  j d | j d |  j d |  j d	 |  j ƒ} | } e j rÄ e | e ƒ rÄ | j ƒ  } n  e j | ƒ } e | | d
 ƒ }	 |	 | j | j U| | f S(   NR   R   R%   R"   R#   R$   t   source_encodingt   generate_magic_commentR   t   exec(   R   R    R   R   R%   t   parseR   t   compileR   R"   R#   R$   t   encodingR   R   R   Ry   R
   t   encodet   typest
   ModuleTypet   __dict__(
   Rr   R:   R/   t
   identifiert   lexert   nodeRR   t   cidR.   R?   (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyR&   Ý  s0    										c   
      C   s  |  j  } t | | d |  j d |  j d |  j ƒ} | j ƒ  } t j | |  j | d |  j	 d |  j
 d |  j d | j d t d |  j d	 |  j ƒ} t j d
 t j j | ƒ ƒ \ } }	 t | t ƒ rá | j | j pØ d ƒ } n  t j | | ƒ t j | ƒ t j |	 | ƒ d  S(   NR   R   R%   R"   R#   R$   R}   R~   R   t   dirt   ascii(   R   R    R   R   R%   R€   R   R   R   R"   R#   R$   R‚   RZ   R   t   tempfilet   mkstempR   R   RC   Ry   R
   Rƒ   t   writet   closet   shutilt   move(
   Rr   R:   R/   t
   outputpathR‡   Rˆ   R‰   RR   t   destRc   (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyRG   ù  s.    									$c         C   s   t  |  j d ƒ S(   NRj   (   t   _get_module_infot   func_globals(   R1   (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyRQ     s    c         C   s   t  j |  S(   N(   R)   Rw   (   R/   (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyR•     s    (   Rl   t
   mako.lexerR    t   makoR   R   R   R   RJ   R   R   R‘   RD   RL   R   t   timeR„   R{   t   objectR   Ro   Re   R)   R&   RG   RQ   R•   (    (    (    s1   /usr/lib/python2.7/dist-packages/mako/template.pyt   <module>   s   "xÿ HF/		 	