U
    ‰dd  ã                   @   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 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   úE/tmp/pip-unpacked-wheel-quko9yh2/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 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ÚAssertionErrorr	   r   r   r   r   7   s    ÿzViewInspector.viewc                 C   s
   || _ d S r   ©r   )r
   Úvaluer   r   r   r   @   s    c                 C   s
   d | _ d S r   r   r	   r   r   r   r   D   s    c                 C   sf   | j }t|d| ¡ ƒ}t||dƒj}|rD|  || ¡ t t|ƒ¡¡S |  |t|d| ¡ ƒ| ¡ ¡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„ |  ¡ D ƒ}d}ddi}|D ]B}| j |¡rP| d¡\}}}	|	 ¡ ||< q"||  d| 7  < q"tj}
||kr€||  ¡ S ||
kr¤|
| |kr¤||
|   ¡ S |d  ¡ 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 ƒ  ||¡}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   Ú
isinstancer;   r   ZDEFAULT_SCHEMA_CLASSÚ
issubclassr   r   r   )r
   r   r   ÚresultZinspector_classZ	inspector©Ú	__class__r   r   r   r   s    
ÿzDefaultSchema.__get__)r3   r4   r5   r   r   Ú__classcell__r   r   r@   r   r;   p   s   r;   )r   r6   Úweakrefr   Zdjango.utils.encodingr   Zrest_framework.settingsr   Zrest_framework.utilsr   r   r;   r   r   r   r   Ú<module>   s   a