3
Ah`d  ã               @   s^   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 G dd„ dƒZ
G d	d
„ d
e
ƒZdS )zb
inspectors.py   # Per-endpoint view introspection

See schemas.__init__.py for package overview.
é    N)ÚWeakKeyDictionary)Ú	smart_str)Úapi_settings)Ú
formattingc               @   sj   e Zd ZdZejdƒZdd„ Zdd„ Zdd„ Z	e
d	d
„ ƒZejdd
„ ƒZejdd
„ ƒZdd„ Zdd„ ZdS )ÚViewInspectorz[
    Descriptor class on APIView.

    Provide subclass for per-view schema generation
    z^[a-zA-Z][0-9A-Za-z_]*:c             C   s   t ƒ | _d S )N)r   Úinstance_schemas)Úself© r	   úP/tmp/pip-build-9m32_hle/djangorestframework/rest_framework/schemas/inspectors.pyÚ__init__   s    zViewInspector.__init__c             C   s   || j kr| j | S || _| S )a%  
        Enables `ViewInspector` as a Python _Descriptor_.

        This is how `view.schema` knows about `view`.

        `__get__` is called when the descriptor is accessed on the owner.
        (That will be when view.schema is called in our case.)

        `owner` is always the owner class. (An APIView, or subclass for us.)
        `instance` is the view instance or `None` if accessed from the class,
        rather than an instance.

        See: https://docs.python.org/3/howto/descriptor.html for info on
        descriptor usage.
        )r   Úview)r   ÚinstanceÚownerr	   r	   r
   Ú__get__   s    

zViewInspector.__get__c             C   s   || j |< |d k	r||_d S )N)r   r   )r   r   Úotherr	   r	   r
   Ú__set__2   s    
zViewInspector.__set__c             C   s   | j dk	stdƒ‚| j S )zView property.NzvSchema generation REQUIRES a view instance. (Hint: you accessed `schema` from the view class rather than an instance.))Ú_viewÚAssertionError)r   r	   r	   r
   r   7   s    zViewInspector.viewc             C   s
   || _ d S )N)r   )r   Úvaluer	   r	   r
   r   @   s    c             C   s
   d | _ d S )N)r   )r   r	   r	   r
   r   D   s    c             C   sf   | j }t|d|jƒ ƒ}t||dƒj}|rD| j||jƒ tjt|ƒƒƒS | j|t|d|jƒ ƒ|jƒ ƒS dS )z—
        Determine a path description.

        This will be based on the method docstring if one exists,
        or else the class docstring.
        ÚactionN)	r   ÚgetattrÚlowerÚ__doc__Ú_get_description_sectionr   Údedentr   Zget_view_description)r   ÚpathÚmethodr   Úmethod_nameZmethod_docstringr	   r	   r
   Úget_descriptionH   s    zViewInspector.get_descriptionc             C   s´   dd„ |j ƒ D ƒ}d}ddi}xJ|D ]B}| jj|ƒrR|jdƒ\}}}	|	jƒ ||< q$||  d| 7  < q$W tj}
||kr„|| jƒ S ||
kr¨|
| |kr¨||
|  jƒ S |d jƒ S )Nc             S   s   g | ]}|‘qS r	   r	   )Ú.0Úliner	   r	   r
   ú
<listcomp>[   s    z:ViewInspector._get_description_section.<locals>.<listcomp>Ú ú:Ú
)Ú
splitlinesÚheader_regexÚmatchÚ	partitionÚstripr   ZSCHEMA_COERCE_METHOD_NAMES)r   r   ÚheaderÚdescriptionÚlinesZcurrent_sectionÚsectionsr    Ú	separatorZleadZcoerce_method_namesr	   r	   r
   r   Z   s    
z&ViewInspector._get_description_sectionN)Ú__name__Ú
__module__Ú__qualname__r   ÚreÚcompiler&   r   r   r   Úpropertyr   ÚsetterÚdeleterr   r   r	   r	   r	   r
   r      s   
	r   c                   s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚDefaultSchemaz?Allows overriding AutoSchema using DEFAULT_SCHEMA_CLASS settingc                sD   t ƒ j||ƒ}t|tƒs|S tj}t|tƒs4tdƒ‚|ƒ }||_	|S )NzTDEFAULT_SCHEMA_CLASS must be set to a ViewInspector (usually an AutoSchema) subclass)
Úsuperr   Ú
isinstancer7   r   ZDEFAULT_SCHEMA_CLASSÚ
issubclassr   r   r   )r   r   r   ÚresultZinspector_classZ	inspector)Ú	__class__r	   r
   r   r   s    
zDefaultSchema.__get__)r/   r0   r1   r   r   Ú__classcell__r	   r	   )r<   r
   r7   p   s   r7   )r   r2   Úweakrefr   Zdjango.utils.encodingr   Zrest_framework.settingsr   Zrest_framework.utilsr   r   r7   r	   r	   r	   r
   Ú<module>   s   a