ó
ù`]c           @  sÜ   d  Z  d d l m Z d Z d d d g Z d d l Z e j Z g  e j D] Z e rK d e k rK e ^ qK e _ d d l	 Z	 e e _ d	 „  Z
 d d
 „ Z d „  Z d „  Z d „  Z d „  Z d „  Z e d „  ƒ Z d S(   s  
Decorator module by Michele Simionato <michelesimionato@libero.it>
Copyright Michele Simionato, distributed under the terms of the BSD License (see below).
http://www.phyast.pitt.edu/~micheles/python/documentation.html

Included in NLTK for its support of a nice memoization decorator.
iÿÿÿÿ(   t   print_functions   restructuredtext ent	   decoratort   new_wrappert   getinfoNt   nltkc   
      C  sP  t  j |  ƒ s$ t  j |  ƒ s$ t ‚ t j d d k rI t  j |  ƒ } n t  j |  ƒ } | d  \ } } } } t | ƒ } | r | j	 | ƒ n  | r¦ | j	 | ƒ n  t  j
 | | | | d d „  ƒd d !} t |  d ƒ rò |  j } |  j }	 n |  j } |  j }	 t d	 |  j d
 | d | d |  j d |  j d |  j d |  j d |	 d | ƒ 	S(   sg  
    Returns an info dictionary containing:
    - name (the name of the function : str)
    - argnames (the names of the arguments : list)
    - defaults (the values of the default arguments : tuple)
    - signature (the signature : str)
    - doc (the docstring : str)
    - module (the module name : str)
    - dict (the function __dict__ : str)

    >>> def f(self, x=1, y=2, *args, **kw): pass

    >>> info = getinfo(f)

    >>> info["name"]
    'f'
    >>> info["argnames"]
    ['self', 'x', 'y', 'args', 'kw']

    >>> info["defaults"]
    (1, 2)

    >>> info["signature"]
    'self, x, y, *args, **kw'
    i    i   i   t   formatvaluec         S  s   d S(   Nt    (    (   t   value(    (    s.   lib/python2.7/site-packages/nltk/decorators.pyt   <lambda>D   R   i   iÿÿÿÿt   __closure__t   namet   argnamest	   signaturet   defaultst   doct   modulet   dictt   globalst   closure(   t   inspectt   ismethodt
   isfunctiont   AssertionErrort   syst   version_infot   getfullargspect
   getargspect   listt   appendt   formatargspect   hasattrR	   t   __globals__t   func_closuret   func_globalsR   t   __name__t   __defaults__t   __doc__t
   __module__t   __dict__(
   t   funct   argspect   regargst   varargst	   varkwargsR   R   R   t   _closuret   _globals(    (    s.   lib/python2.7/site-packages/nltk/decorators.pyR      s8    $
								c         C  sg   | p t  | ƒ } | d |  _ | d |  _ | d |  _ |  j j | d ƒ | d |  _ | |  _ |  S(   NR
   R   R   R   R   (   R   R"   R$   R%   R&   t   updateR#   t   undecorated(   t   wrappert   modelt   infodict(    (    s.   lib/python2.7/site-packages/nltk/decorators.pyt   update_wrapper]   s    	c         C  sr   t  | t ƒ r | } n t | ƒ } d | d k s@ t d ƒ ‚ d | } t | t d |  ƒ ƒ } t | | | ƒ S(   sA  
    An improvement over functools.update_wrapper. The wrapper is a generic
    callable object. It works by generating a copy of the wrapper with the
    right signature and by updating the copy, not the original.
    Moreovoer, 'model' can be a dictionary with keys 'name', 'doc', 'module',
    'dict', 'defaults'.
    t	   _wrapper_R   s(   "_wrapper_" is a reserved argument name!s.   lambda %(signature)s: _wrapper_(%(signature)s)(   t
   isinstanceR   R   R   t   evalR3   (   R0   R1   R2   t   srct   funcopy(    (    s.   lib/python2.7/site-packages/nltk/decorators.pyR   h   s    		
c           s   t  ‡  ‡ f d †  ˆ  ƒ S(   Nc            s   ˆ j  ˆ  |  | Ž S(   N(   t   call(   t   at   k(   R'   t   self(    s.   lib/python2.7/site-packages/nltk/decorators.pyR   ~   R   (   R   (   R<   R'   (    (   R'   R<   s.   lib/python2.7/site-packages/nltk/decorators.pyt   __call__}   s    c         C  sU   t  t |  ƒ ƒ } d | k r- t d ƒ ‚ n  d | k rH t d ƒ ‚ n  t |  _ |  S(   sí   
    Take a class with a ``.caller`` method and return a callable decorator
    object. It works by adding a suitable __call__ method to the class;
    it raises a TypeError if the class already has a nontrivial __call__
    method.
    R=   s=   You cannot decorate a class with a nontrivial __call__ methodR9   s2   You cannot decorate a class without a .call method(   t   sett   dirt	   TypeErrorR=   (   t   clst   attrs(    (    s.   lib/python2.7/site-packages/nltk/decorators.pyt   decorator_factory   s    	c           s5   t  j ˆ  ƒ r t ˆ  ƒ S‡  f d †  } t | ˆ  ƒ S(   sò  
    General purpose decorator factory: takes a caller function as
    input and returns a decorator with the same attributes.
    A caller function is any function like this::

     def caller(func, *args, **kw):
         # do something
         return func(*args, **kw)

    Here is an example of usage:

    >>> @decorator
    ... def chatty(f, *args, **kw):
    ...     print("Calling %r" % f.__name__)
    ...     return f(*args, **kw)

    >>> chatty.__name__
    'chatty'

    >>> @chatty
    ... def f(): pass
    ...
    >>> f()
    Calling 'f'

    decorator can also take in input a class with a .caller method; in this
    case it converts the class into a factory of callable decorator objects.
    See the documentation for an example.
    c           ss   t  |  ƒ } | d } d | k p+ d | k s; t d ƒ ‚ d | } t | t d |  d ˆ  ƒ ƒ } t | |  | ƒ S(   NR   t   _call_t   _func_s2   You cannot use _call_ or _func_ as argument names!s3   lambda %(signature)s: _call_(_func_, %(signature)s)(   R   R   R6   R   R3   (   R'   R2   R   R7   t   dec_func(   t   caller(    s.   lib/python2.7/site-packages/nltk/decorators.pyt
   _decorator´   s    
	
(   R   t   isclassRC   R3   (   RG   RH   (    (   RG   s.   lib/python2.7/site-packages/nltk/decorators.pyR   “   s    
c         C  sC   y t  |  | ƒ SWn+ t k
 r> | ƒ  } t |  | | ƒ | SXd S(   s'   Similar to .setdefault in dictionaries.N(   t   getattrt   AttributeErrort   setattr(   t   objR
   t   default_thunkt   default(    (    s.   lib/python2.7/site-packages/nltk/decorators.pyt   getattr_Â   s    	c         G  sD   t  |  d t ƒ } | | k r& | | S|  | Œ  } | | | <| Sd  S(   Nt   memoize_dic(   RP   R   (   R'   t   argst   dict   result(    (    s.   lib/python2.7/site-packages/nltk/decorators.pyt   memoizeÌ   s    
(   R$   t
   __future__R    t   __docformat__t   __all__R   t   patht   old_sys_patht   pR   R   t   NoneR3   R   R=   RC   R   RP   RU   (    (    (    s.   lib/python2.7/site-packages/nltk/decorators.pyt   <module>   s    
1		?				/	
