3
Ah`Ç  ã               @   sn   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	 dd„ Z
d	d
„ Zdd„ Zdd„ ZG dd„ dƒZdS )zP
Utility functions to return a formatted name and description for a given view.
é    N)Ú	force_str)Úescape)Ú	mark_safe)Úapply_markdownc             C   s(   | j |ƒr$| |kr$| dt|ƒ … S | S )z|
    Strip trailing component `trailing` from `content` if it exists.
    Used when generating names from view classes.
    N)ÚendswithÚlen)ÚcontentZtrailing© r	   úN/tmp/pip-build-9m32_hle/djangorestframework/rest_framework/utils/formatting.pyÚremove_trailing_string   s    r   c             C   s¤   t | ƒ} dd„ | jƒ dd… D ƒ}|rœtdd„ |D ƒƒ}tdd„ |D ƒƒ}|rtdd|  }tjtj|tjƒd	| ƒ} n(|rœdd
|  }tjtj|tjƒd	| ƒ} | jƒ S )a  
    Remove leading indent from a block of text.
    Used when generating descriptions from docstrings.

    Note that python's `textwrap.dedent` doesn't quite cut it,
    as it fails to dedent multiline docstrings that include
    unindented text on the initial line.
    c             S   s   g | ]}|j ƒ r|‘qS r	   )Úlstrip)Ú.0Úliner	   r	   r
   ú
<listcomp>!   s    zdedent.<locals>.<listcomp>é   Nc             S   s"   g | ]}t |ƒt |jd ƒƒ ‘qS )ú )r   r   )r   r   r	   r	   r
   r   %   s    c             S   s"   g | ]}t |ƒt |jd ƒƒ ‘qS )ú	)r   r   )r   r   r	   r	   r
   r   &   s    ú^r   Ú r   )r   Ú
splitlinesÚminÚreÚsubÚcompileÚ	MULTILINEÚstrip)r   ÚlinesZwhitespace_countsZ
tab_countsZwhitespace_patternr	   r	   r
   Údedent   s    	r   c             C   s*   d}t j|d| ƒjƒ } dj| jdƒƒjƒ S )zm
    Translate 'CamelCaseNames' to 'Camel Case Names'.
    Used when generating names from view classes.
    z&(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))z \1r   Ú_)r   r   r   ÚjoinÚsplitÚtitle)r   Zcamelcase_boundaryr	   r	   r
   Úcamelcase_to_spaces0   s    r"   c             C   s2   t rt | ƒ} nt| ƒjddƒ} d|  d } t| ƒS )z5
    Apply HTML markup to the given description.
    Ú
z<br />z<p>z</p>)r   r   Úreplacer   )Údescriptionr	   r	   r
   Úmarkup_description:   s
    
r&   c               @   s,   e Zd ZdZdZdd„ Zdd	„ Zd
d„ ZdS )Úlazy_formatz¯
    Delay formatting until it's actually needed.

    Useful when the format string or one of the arguments is lazy.

    Not using Django's lazy because it is too slow.
    Úformat_stringÚargsÚkwargsÚresultc             O   s   d | _ || _|| _|| _d S )N)r+   r(   r)   r*   )Úselfr(   r)   r*   r	   r	   r
   Ú__init__P   s    zlazy_format.__init__c             C   s4   | j d kr.| jj| j| jŽ| _ d\| _| _| _| j S )N)NNN)r+   r(   Úformatr)   r*   )r,   r	   r	   r
   Ú__str__V   s    
zlazy_format.__str__c             C   s   t | ƒ| S )N)Ústr)r,   Úvaluer	   r	   r
   Ú__mod__\   s    zlazy_format.__mod__N)r(   r)   r*   r+   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r-   r/   r2   r	   r	   r	   r
   r'   F   s
   r'   )r6   r   Zdjango.utils.encodingr   Zdjango.utils.htmlr   Zdjango.utils.safestringr   Zrest_framework.compatr   r   r   r"   r&   r'   r	   r	   r	   r
   Ú<module>   s   

