U
    Â“«jŒ  ã                   @   sæ   d dl Z d dlmZmZmZmZmZmZmZm	Z	 d dl
mZ d dlmZmZmZ d dlmZmZmZ erxd dlmZmZ edƒZeded	ef d
�ZG dd„ dƒZed	ef edœdd„Zeddœdd„ƒZeeƒG dd„ dƒƒZdS )é    N)ÚTYPE_CHECKINGÚAnyÚCallableÚListÚOptionalÚTypeVarÚUnionÚoverload)Úimplementer)ÚMissingRenderMethodÚMissingTemplateLoaderÚUnexposedMethodError)ÚIRenderableÚIRequestÚITemplateLoader)ÚFlattenableÚTagÚTÚ_Tc.)Úboundc                   @   sŽ   e Zd ZdZeedef edœdd„Zeƒ Ze	ee
edef dœdd„ƒZe	ee
eeedef ef d	œd
d„ƒZefee
eed	œdd„ZdS )ÚExposea/  
    Helper for exposing methods for various uses using a simple decorator-style
    callable.

    Instances of this class can be called with one or more functions as
    positional arguments.  The names of these functions will be added to a list
    on the class object of which they are methods.
    .)ÚfÚfuncObjsÚreturnc                G   s:   t  |g|¡D ]&}t|dg ƒ}| | ¡ t|d|ƒ q|S )a;  
        Add one or more functions to the set of exposed functions.

        This is a way to declare something about a class definition, similar to
        L{zope.interface.implementer}.  Use it like this::

            magic = Expose('perform extra magic')
            class Foo(Bar):
                def twiddle(self, x, y):
                    ...
                def frob(self, a, b):
                    ...
                magic(twiddle, frob)

        Later you can query the object::

            aFoo = Foo()
            magic.get(aFoo, 'twiddle')(x=1, y=2)

        The call to C{get} will fail if the name it is given has not been
        exposed using C{magic}.

        @param funcObjs: One or more function objects which will be exposed to
        the client.

        @return: The first of C{funcObjs}.
        ÚexposedThrough)Ú	itertoolsÚchainÚgetattrÚappendÚsetattr)Úselfr   r   ZfObjr   © r!   ú8/tmp/pip-unpacked-wheel-59kc99xd/twisted/web/_element.pyÚ__call__,   s
    
zExpose.__call__)ÚinstanceÚ
methodNamer   c                 C   s   d S ©Nr!   )r    r$   r%   r!   r!   r"   ÚgetP   s    z
Expose.get)r$   r%   Údefaultr   c                 C   s   d S r&   r!   )r    r$   r%   r(   r!   r!   r"   r'   T   s    c                 C   s<   t ||dƒ}t |dg ƒ}| |kr8|| jkr4t| |ƒ‚|S |S )aB  
        Retrieve an exposed method with the given name from the given instance.

        @raise UnexposedMethodError: Raised if C{default} is not specified and
        there is no exposed method with the given name.

        @return: A callable object for the named method assigned to the given
        instance.
        Nr   )r   Ú
_nodefaultr   )r    r$   r%   r(   Úmethodr   r!   r!   r"   r'   Z   s    

N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Úobjectr#   r)   r	   Ústrr   r'   r   r   r!   r!   r!   r"   r   "   s$   	"  þÿ  þr   )Úthunkr   c                 C   s   t ƒ }| j|_|S r&   )r   r.   )r1   Zexposer!   r!   r"   Úexposero   s    r2   )r   c                   C   s   dS )aâ  
    Decorate with L{renderer} to use methods as template render directives.

    For example::

        class Foo(Element):
            @renderer
            def twiddle(self, request, tag):
                return tag('Hello, world.')

        <div xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1">
            <span t:render="twiddle" />
        </div>

    Will result in this final output::

        <div>
            <span>Hello, world.</span>
        </div>
    Nr!   r!   r!   r!   r"   Úrendereru   s    r3   c                   @   sj   e Zd ZU dZdZee ed< dee dœdd„Ze	e
ee dgdf d	œd
d„Zee ddœdd„ZdS )ÚElementaª  
    Base for classes which can render part of a page.

    An Element is a renderer that can be embedded in a stan document and can
    hook its template (from the loader) up to render methods.

    An Element might be used to encapsulate the rendering of a complex piece of
    data which is to be displayed in multiple different contexts.  The Element
    allows the rendering logic to be easily re-used in different ways.

    Element returns render methods which are registered using
    L{twisted.web._element.renderer}.  For example::

        class Menu(Element):
            @renderer
            def items(self, request, tag):
                ....

    Render methods are invoked with two arguments: first, the
    L{twisted.web.http.Request} being served and second, the tag object which
    "invoked" the render method.

    @ivar loader: The factory which will be used to load documents to
        return from C{render}.
    NÚloader©r5   c                 C   s   |d k	r|| _ d S r&   r6   )r    r5   r!   r!   r"   Ú__init__«   s    zElement.__init__r   r   )Únamer   c                 C   s$   t  | |d¡}|dkr t| |ƒ‚|S )z=
        Look up and return the named render method.
        N)r3   r'   r   )r    r8   r*   r!   r!   r"   ÚlookupRenderMethod¯   s    
zElement.lookupRenderMethod)Úrequestr   c                 C   s   | j }|dkrt| ƒ‚| ¡ S )aê  
        Implement L{IRenderable} to allow one L{Element} to be embedded in
        another's template or rendering output.

        (This will simply load the template from the C{loader}; when used in a
        template, the flattening engine will keep track of this object
        separately as the object to lookup renderers on and call
        L{Element.renderer} to look them up.  The resulting object from this
        method is not directly associated with this L{Element}.)
        N)r5   r   Úload)r    r:   r5   r!   r!   r"   Úrenderº   s    zElement.render)N)r+   r,   r-   r.   r5   r   r   Ú__annotations__r7   r0   r   r   r9   r<   r!   r!   r!   r"   r4   �   s   
þr4   )r   Útypingr   r   r   r   r   r   r   r	   Zzope.interfacer
   Ztwisted.web.errorr   r   r   Ztwisted.web.iwebr   r   r   Ztwisted.web.templater   r   r   r/   r   r   r2   r3   r4   r!   r!   r!   r"   Ú<module>   s   (M