
 Xk                 @   s   d  Z  y d d l m Z Wn e k
 r4 d Z Yn Xd d l m Z m Z m Z d d l m	 Z	 Gd d   d e
  Z d d	 d
  Z d d   Z d d   Z d d   Z d d   Z e d k r Gd d   d e  Z n  Gd d   d e  Z d S)zCFunctions that help with dynamically creating decorators for views.    )ContextDecoratorN)WRAPPER_ASSIGNMENTSupdate_wrapperwraps)sixc                   s%   e  Z d  Z d   f d d  Z   S)classonlymethodNc                s4   | d  k	 r t  d   n  t t |   j | |  S)Nz=This method is available only on the class, not on instances.)AttributeErrorsuperr   __get__)selfinstancecls)	__class__ E/home/ubuntu/projects/ifolica/build/django/django/utils/decorators.pyr
      s    zclassonlymethod.__get__)__name__
__module____qualname__r
   r   r   )r   r   r      s   r    c                sy      f d d   } t    d  s@ t |   d t    n  t    d  rb d   j | _ n d   j j | _ | S)z?
    Converts a function decorator into a method decorator
    c                s   t  |  t  } | r  rf t |    rf t |     t   s~ t d j  |      q~ q t d j |      n |    f d d        f d d   }   d d    } t | |  t |   | r t	 |   |  |  S| S)	NzCCannot decorate '{0}' as it isn't a callable attribute of {1} ({2})zgThe keyword argument `name` must be the name of a method of the decorated class: {0}. Got '{1}' insteadc                sG   t    d  r= x'   d d d  D] } | |   }  q# W|  S  |   S)z
            Apply a list/tuple of decorators if decorator is one. Decorator
            functions are applied so that the call order is the same as the
            order in which they appear in the iterable.
            __iter__N   )hasattr)functiondec)	decoratorr   r   decorate0   s
    z0method_decorator.<locals>._dec.<locals>.decoratec                s(       f d d    } | | |   S)Nc                 s     j   t    |  |   S)N)r
   type)Zargs2Zkwargs2)funcr   r   r   
bound_func=   s    zDmethod_decorator.<locals>._dec.<locals>._wrapper.<locals>.bound_funcr   )r   argskwargsr   )r   r   )r   r   _wrapper<   s    z0method_decorator.<locals>._dec.<locals>._wrapperc              _   s   d  S)Nr   )r    r!   r   r   r   dummyH   s    z-method_decorator.<locals>._dec.<locals>.dummy)

isinstancer   r   getattrcallable	TypeErrorformat
ValueErrorr   setattr)objZis_classr"   r#   )r   name)r   r   r   _dec   s*    zmethod_decorator.<locals>._decr   assignedr   zmethod_decorator(%s))r   r   available_attrsr   r   )r   r,   r-   r   )r   r,   r   method_decorator   s    :r0   c             C   s
   t  |   S)a=  
    Like decorator_from_middleware, but returns a function
    that accepts the arguments to be passed to the middleware_class.
    Use like::

         cache_page = decorator_from_middleware_with_args(CacheMiddleware)
         # ...

         @cache_page(3600)
         def my_view(request):
             # ...
    )make_middleware_decorator)middleware_classr   r   r   #decorator_from_middleware_with_args`   s    r3   c             C   s   t  |     S)z
    Given a middleware class (not an instance), returns a view decorator. This
    lets you use middleware functionality on a per-view basis. The middleware
    is created with no params passed.
    )r1   )r2   r   r   r   decorator_from_middlewarep   s    r4   c                s.   t  j r t St   f d d   t D  Sd S)z
    Return the list of functools-wrappable attributes on a callable.
    This is required as a workaround for http://bugs.python.org/issue3445
    under Python 2.
    c             3   s$   |  ] } t    |  r | Vq d  S)N)r   ).0a)fnr   r   	<genexpr>   s    z"available_attrs.<locals>.<genexpr>N)r   PY3r   tuple)r7   r   )r7   r   r/   y   s    	r/   c                s     f d d   } | S)Nc                 s%    |  |       f d d   } | S)Nc                s1   t    d t       f d d    } | S)Nr.   c                s|  t   d  r1  j    } | d  k	 r1 | Sn  t   d  rk  j    | |  } | d  k	 rk | Sn  y    | |  } Wn\ t k
 r } z< t   d  r  j   |  } | d  k	 r | Sn    WYd  d  } ~ Xn Xt  | d  rYt | j  rYt   d  r" j   |  } n  t   d  rx   f d d   } | j |  qxn t   d  rx j	   |  S| S)	Nprocess_requestprocess_viewprocess_exceptionrenderprocess_template_responseprocess_responsec                s     j   |   S)N)r@   )response)
middlewarerequestr   r   callback   s    zomake_middleware_decorator.<locals>._make_decorator.<locals>._decorator.<locals>._wrapped_view.<locals>.callback)
r   r;   r<   	Exceptionr=   r&   r>   r?   Zadd_post_render_callbackr@   )rC   r    r!   resultrA   erD   )rB   	view_func)rC   r   _wrapped_view   s2    z]make_middleware_decorator.<locals>._make_decorator.<locals>._decorator.<locals>._wrapped_view)r   r/   )rH   rI   )rB   )rH   r   
_decorator   s    -zFmake_middleware_decorator.<locals>._make_decorator.<locals>._decoratorr   )Zm_argsZm_kwargsrJ   )r2   )rB   r   _make_decorator   s    !z2make_middleware_decorator.<locals>._make_decoratorr   )r2   rK   r   )r2   r   r1      s    %r1   c               @   s"   e  Z d  Z d Z d d   Z d S)r   z]
        A base class that enables a context manager to also be used as a decorator.
        c                s1   t    d t       f d d    } | S)Nr.   c           
      s       |  |   SWd  QXd  S)Nr   )r    r!   )r   r   r   r   inner   s    z(ContextDecorator.__call__.<locals>.inner)r   r/   )r   r   rL   r   )r   r   r   __call__   s    -zContextDecorator.__call__N)r   r   r   __doc__rM   r   r   r   r   r      s   r   c               @   s:   e  Z d  Z d d d  Z d d d  Z d d   Z d S)classpropertyNc             C   s   | |  _  d  S)N)fget)r   methodr   r   r   __init__   s    zclassproperty.__init__c             C   s   |  j  |  S)N)rP   )r   r   r   r   r   r   r
      s    zclassproperty.__get__c             C   s   | |  _  |  S)N)rP   )r   rQ   r   r   r   getter   s    	zclassproperty.getter)r   r   r   rR   r
   rS   r   r   r   r   rO      s   rO   )rN   
contextlibr   ImportError	functoolsr   r   r   Zdjango.utilsr   classmethodr   r0   r3   r4   r/   r1   objectrO   r   r   r   r   <module>   s   L	)