o
    r|aj  ã                   @   s8   d Z ddlmZ ddlmZ dd„ ZG dd„ deƒZdS )	z3
Defines a class responsible for rendering logic.

é    )Ú	is_string)Úparsec                 C   s
   |   |¡S )z?
    Find and return a name from a ContextStack instance.

    )Úget)ÚstackÚname© r   ú7/usr/lib/python3/dist-packages/pystache/renderengine.pyÚcontext_get   s   
r	   c                   @   sH   e Zd ZdZ					ddd„Zdd„ Zdd„ Zdd	d
„Zddd„ZdS )ÚRenderEngineaÏ  
    Provides a render() method.

    This class is meant only for internal use.

    As a rule, the code in this class operates on unicode strings where
    possible rather than, say, strings of type str or markupsafe.Markup.
    This means that strings obtained from "external" sources like partials
    and variable tag values are immediately converted to unicode (or
    escaped and converted to unicode) before being operated on further.
    This makes maintaining, reasoning about, and testing the correctness
    of the code much simpler.  In particular, it keeps the implementation
    of this class independent of the API details of one (or possibly more)
    unicode subclasses (e.g. markupsafe.Markup).

    Nc                 C   s"   || _ || _|| _|| _|| _dS )aÒ  
        Arguments:

          literal: the function used to convert unescaped variable tag
            values to unicode, e.g. the value corresponding to a tag
            "{{{name}}}".  The function should accept a string of type
            str or unicode (or a subclass) and return a string of type
            unicode (but not a proper subclass of unicode).
                This class will only pass basestring instances to this
            function.  For example, it will call str() on integer variable
            values prior to passing them to this function.

          escape: the function used to escape and convert variable tag
            values to unicode, e.g. the value corresponding to a tag
            "{{name}}".  The function should obey the same properties
            described above for the "literal" function argument.
                This function should take care to convert any str
            arguments to unicode just as the literal function should, as
            this class will not pass tag values to literal prior to passing
            them to this function.  This allows for more flexibility,
            for example using a custom escape function that handles
            incoming strings of type markupsafe.Markup differently
            from plain unicode strings.

          resolve_context: the function to call to resolve a name against
            a context stack.  The function should accept two positional
            arguments: a ContextStack instance and a name to resolve.

          resolve_partial: the function to call when loading a partial.
            The function should accept a template name string and return a
            template string of type unicode (not a subclass).

          to_str: a function that accepts an object and returns a string (e.g.
            the built-in function str).  This function is used for string
            coercion whenever a string is required (e.g. for converting None
            or 0 to a string).

        N)ÚescapeÚliteralÚresolve_contextÚresolve_partialÚto_str)Úselfr   r   r   r   r   r   r   r   Ú__init__+   s
   .
zRenderEngine.__init__c                 C   s8   |   ||¡}t|ƒr|  |ƒ |¡S t|ƒs|  |¡S |S )zO
        Get a value from the given context as a basestring instance.

        )r   ÚcallableÚ_render_valuer   r   )r   Úcontextr   Úvalr   r   r   Úfetch_stringh   s   
zRenderEngine.fetch_stringc                 C   s\   |   ||¡}|sg }|S zt|ƒ W n ty   |g}Y |S w t|ƒs)t|tƒr,|g}|S )z:
        Fetch the value of a section as a list.

        )r   ÚiterÚ	TypeErrorr   Ú
isinstanceÚdict)r   r   r   Údatar   r   r   Úfetch_section_datax   s   	õ÷zRenderEngine.fetch_section_datac                 C   s6   t |ƒs	|  |¡}t|ƒtur|  |¡}|  |||¡S )z-
        Render an arbitrary value.

        )r   r   ÚtypeÚstrr   Úrender)r   r   r   Ú
delimitersr   r   r   r   Ÿ   s
   

zRenderEngine._render_valuec                 C   s   t ||ƒ}| | |¡S )zý
        Render a unicode template string, and return as unicode.

        Arguments:

          template: a template string of type unicode (but not a proper
            subclass of unicode).

          context_stack: a ContextStack instance.

        )r   r   )r   ÚtemplateZcontext_stackr    Zparsed_templater   r   r   r   «   s   
zRenderEngine.render)NNNNN)N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   r
      s    
ú=
'r
   N)r%   Zpystache.commonr   Zpystache.parserr   r	   Úobjectr
   r   r   r   r   Ú<module>   s
   