3
 [ð]•%  ã               @   sâ   d Z ddlZddl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lmZ dd	lmZ dd
lmZ ddlmZ eƒ ZejdƒZeeƒZdZdZdd„ Zefdd„ZG dd„ deƒZeƒ Zdd„ ZG dd„ deƒZdS )a•  
    werkzeug.debug.repr
    ~~~~~~~~~~~~~~~~~~~

    This module implements object representations for debugging purposes.
    Unlike the default repr these reprs expose a lot more information and
    produce HTML instead of ASCII.

    Together with the CSS and JavaScript files of the debugger this gives
    a colorful and more compact output.

    :copyright: 2007 Pallets
    :license: BSD-3-Clause
é    N)Údeque)Úformat_exception_onlyé   )Úinteger_types)Ú	iteritems)ÚPY2)Ústring_types)Ú	text_type)Úescapez(?:\r\n|\r|\n){2,}zL<div class=box>
  <h3>%(title)s</h3>
  <pre class=help>%(text)s</pre>
</div>zQ<div class=box>
  <h3>%(title)s</h3>
  %(repr)s
  <table>%(items)s</table>
</div>c             C   s   t ƒ j| ƒS )z9Creates a debug repr of an object as HTML unicode string.)ÚDebugReprGeneratorÚrepr)Úobj© r   ú7/tmp/pip-build-w1as9q70/Werkzeug/werkzeug/debug/repr.pyÚ
debug_repr2   s    r   c             C   s<   t ƒ }| tkr"|jtjdƒjƒ}n
|j| ƒ}tjj|ƒ dS )zdPrint the object details to stdout._write (for the interactive
    console of the web debugger.
    é   N)	r   ÚmissingÚdump_localsÚsysÚ	_getframeÚf_localsÚdump_objectÚstdoutÚ_write)r   ÚgenÚrvr   r   r   Údump7   s
    
r   c               @   s"   e Zd ZdZdd„ Zddd„ZdS )Ú_Helperz�Displays an HTML version of the normal help, for the interactive
    debugger only because it requires a patched sys.stdout.
    c             C   s   dS )Nz(Type help(object) for help about object.r   )Úselfr   r   r   Ú__repr__H   s    z_Helper.__repr__Nc             C   sª   |d kr t jjdt| ƒ ƒ d S dd l}|j|ƒ t jjƒ }t|tƒrR|j	ddƒ}t
j|ƒ}t|ƒdkr„|d }dj|dd … ƒ}nd}|d }t jjt||dœ ƒ d S )	Nz<span class=help>%s</span>r   zutf-8Úignorer   z

ZHelp)ÚtitleÚtext)r   r   r   r   ÚpydocÚhelpÚresetÚ
isinstanceÚbytesÚdecodeÚ_paragraph_reÚsplitÚlenÚjoinÚ	HELP_HTML)r   Ztopicr#   r   Z
paragraphsr!   r"   r   r   r   Ú__call__K   s    



z_Helper.__call__)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r.   r   r   r   r   r   C   s   r   c             C   sh   t |tƒr*x.|D ]}t|ƒ|kr| S qW nt|ƒ|kr:| S d}|jjdkrVd|jj }d||jj| f S )NÚ Ú__builtin__Ú
exceptionsz<span class="module">%s.</span>z%s%s(%s))r4   r5   )r&   ÚtupleÚtypeÚ	__class__r0   r/   )Úinnerr   ÚbaseÚmoduler   r   r   Ú_add_subclass_infob   s    


r<   c               @   s¸   e Zd Zdd„ Zeƒ dfdd„ZeddeƒZedd	eƒZ	ed
de
ƒZeddeƒZeddeƒZ[dd„ Zd%dd„Zd&dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd'd#d$„Zd"S )(r   c             C   s
   g | _ d S )N)Ú_stack)r   r   r   r   Ú__init__p   s    zDebugReprGenerator.__init__é   c                s   ‡ ‡‡‡fdd„}|S )Nc                s–   |rt ˆd ˆ |ˆ ƒS ˆg}d}xHt|ƒD ]<\}}|rB|jdƒ |ˆkrX|jdƒ d}|j| j|ƒƒ q,W |rz|jdƒ |jˆƒ t dj|ƒ|ˆ ƒS )Nz...Fz, z<span class="extended">Tz</span>r3   )r<   Ú	enumerateÚappendr   r,   )r   r   Ú	recursiveÚbufÚhave_extended_sectionÚidxÚitem)r:   ÚleftÚlimitÚrightr   r   Úproxyt   s    



z6DebugReprGenerator._sequence_repr_maker.<locals>.proxyr   )rG   rI   r:   rH   rJ   r   )r:   rG   rH   rI   r   Ú_sequence_repr_makers   s    z'DebugReprGenerator._sequence_repr_makerú[ú]ú(ú)zset([z])zfrozenset([z/<span class="module">collections.</span>deque([c             C   s\   t |jƒ}tr|jddƒ}ntj|ddƒ}|d d… dkrLd|dd …  }nd| }d| S )	Nzstring-escaper    zunicode-escaper   ÚuÚurÚrz0re.compile(<span class="string regex">%s</span>))r   Úpatternr   r(   Úcodecs)r   r   rS   r   r   r   Ú
regex_repr�   s    
zDebugReprGenerator.regex_repréF   c             C   s¤   dg}t |ƒ}t|ƒ| dkrJ|jt|d |… ƒdt||d … ƒdfƒ n|jt|ƒƒ |jdƒ dj|ƒ}|d dks�|d dkr |d	 dkr t||ttfƒS |S )
Nz<span class="string">r   z<span class="extended">z</span>r3   r   z'"Zubr   )	r   r+   Úextendr
   rA   r,   r<   r'   r	   )r   r   rH   rC   rR   Úoutr   r   r   Ústring_reprœ   s    


$zDebugReprGenerator.string_repré   c       	      C   s¨   |rt d|tƒS dg}d}xbtt|ƒƒD ]R\}\}}|rB|jdƒ ||d kr\|jdƒ d}|jd| j|ƒ| j|ƒf ƒ q(W |rŒ|jd	ƒ |jd
ƒ t dj|ƒ|tƒS )Nz{...}Ú{Fz, r   z<span class="extended">TzT<span class="pair"><span class="key">%s</span>: <span class="value">%s</span></span>z</span>Ú}r3   )r<   Údictr@   r   rA   r   r,   )	r   ÚdrB   rH   rC   rD   rE   ÚkeyÚvaluer   r   r   Ú	dict_repr·   s"    



zDebugReprGenerator.dict_reprc             C   s$   t |ƒ}tr|jddƒ}dt|ƒ S )Nzutf-8Úreplacez<span class="object">%s</span>)r   r   r(   r
   )r   r   rR   r   r   r   Úobject_reprÌ   s    zDebugReprGenerator.object_reprc             C   sð   |t krdt  S t|tttfƒr(d| S t|tƒs<t|tƒrF| j|ƒS t|tƒrZ| j	|ƒS t|t
ƒrp| j||ƒS t|tƒr†| j||ƒS t|tƒrœ| j||ƒS t|tƒr²| j||ƒS t|tƒrÈ| j||ƒS td k	ræt|tƒræ| j||ƒS | j|ƒS )Nz<span class="help">%r</span>z<span class="number">%r</span>)Úhelperr&   r   ÚfloatÚcomplexr   r'   rY   Ú	RegexTyperU   ÚlistÚ	list_reprr6   Ú
tuple_reprÚsetÚset_reprÚ	frozensetÚfrozenset_reprr]   ra   r   Ú
deque_reprrc   )r   r   rB   r   r   r   Údispatch_reprÒ   s*    







z DebugReprGenerator.dispatch_reprc             C   sX   ydj ttjƒ d d… Ž ƒ}W n tk
r6   d}Y nX trH|jddƒ}dt|jƒ ƒ S )Nr3   r   ú?zutf-8r    z8<span class="brokenrepr">&lt;broken repr (%s)&gt;</span>)	r,   r   r   Úexc_infoÚ	Exceptionr   r(   r
   Ústrip)r   Úinfor   r   r   Úfallback_repré   s    
z DebugReprGenerator.fallback_reprc             C   sj   d}x| j D ]}||krd}P qW | j j|ƒ z*y| j||ƒS  tk
rT   | jƒ S X W d | j jƒ  X d S )NFT)r=   rA   rp   rs   rv   Úpop)r   r   rB   rF   r   r   r   r   ô   s    zDebugReprGenerator.reprc             C   sÜ   d  }}t |tƒrTd}g }x8t|ƒD ],\}}t |tƒs<d }P |j|| j|ƒfƒ q$W |d kr´g }| j|ƒ}xDt|ƒD ]8}y|j|| jt||ƒƒfƒ W qt tk
rª   Y qtX qtW d}|dt	j
|ƒdd…  7 }| j|||ƒS )NzContents ofzDetails forú r   éÿÿÿÿ)r&   r]   r   r   rA   r   ÚdirÚgetattrrs   Úobjectr   Úrender_object_dump)r   r   r   Úitemsr!   r_   r`   r   r   r   r     s(    



zDebugReprGenerator.dump_objectc                s"   ‡ fdd„|j ƒ D ƒ}ˆ j|dƒS )Nc                s   g | ]\}}|ˆ j |ƒf‘qS r   )r   )Ú.0r_   r`   )r   r   r   ú
<listcomp>  s    z2DebugReprGenerator.dump_locals.<locals>.<listcomp>zLocal variables in frame)r~   r}   )r   r^   r~   r   )r   r   r     s    zDebugReprGenerator.dump_localsNc             C   s`   g }x&|D ]\}}|j dt|ƒ|f ƒ q
W |s:|j dƒ tt|ƒ|rNd| nddj|ƒdœ S )Nz&<tr><th>%s<td><pre class=repr>%s</pre>z<tr><td><em>Nothing</em>z<pre class=repr>%s</pre>r3   Ú
)r!   r   r~   )rA   r
   ÚOBJECT_DUMP_HTMLr,   )r   r~   r!   r   Z
html_itemsr_   r`   r   r   r   r}     s    
z%DebugReprGenerator.render_object_dump)rV   )rZ   )N)r/   r0   r1   r>   r|   rK   rh   ri   r6   rj   rk   rl   rm   rn   r   ro   rU   rY   ra   rc   rp   rv   r   r   r   r}   r   r   r   r   r   o   s&   


r   )r2   rT   Úrer   Úcollectionsr   Ú	tracebackr   Ú_compatr   r   r   r   r	   Úutilsr
   r|   r   Úcompiler)   r7   rg   r-   r‚   r   r   r   rd   r<   r   r   r   r   r   Ú<module>   s,   
