B
    Mt\                  @   s   d Z ddlZddlZejdkr2dd ZdddZndd ZeZG d	d
 d
eZG dd deZ	ede
fde	ddZG dd de	ZG dd deZG dd de
ZG dd de
ZG dd deZG dd dZe ZdS )z
module for generating and serializing xml and html structures
by using simple python objects.

(c) holger krekel, holger at merlinux eu. 2009
    N)   r   c             C   s   | S )N )sr   r   )lib/python3.7/site-packages/py/_xmlgen.pyu
   s    r   c             C   s   t | dr|  S t| S )N__unicode__)hasattrr   str)xerrorsr   r   r   unicode   s    
r   c             C   s   t | S )N)r   )r   r   r   r   r      s    c               @   s   e Zd Zdd ZdS )NamespaceMetaclassc             C   sx   |d d dkrt || tkr(td| j}|d k	rF||krFt |i }| jrX||d< t|| jf|}t| || |S )N   _zNamespace class is abstractxmlname)AttributeError	Namespace
ValueError__tagspec____stickyname__type__tagclass__setattr)selfnameZtagspecZ	classattrclsr   r   r   __getattr__   s    zNamespaceMetaclass.__getattr__N)__name__
__module____qualname__r   r   r   r   r   r      s   r   c                   sJ   e Zd ZG dd deZ fddZdd ZeZddd	Zd
d Z	  Z
S )Tagc               @   s   e Zd Zdd ZdS )zTag.Attrc             K   s   | j | d S )N)__dict__update)r   kwargsr   r   r   __init__(   s    zTag.Attr.__init__N)r   r   r   r$   r   r   r   r   Attr'   s   r%   c                s"   t t| | | jf || _d S )N)superr    r$   r%   attr)r   argsr#   )	__class__r   r   r$   +   s    zTag.__init__c             C   s   | j ddS )Nr   )indent)r   )r   r   r   r   r   /   s    zTag.__unicode__   c             C   s$   g }t |j||  td|S )N )SimpleUnicodeVisitorappendvisitr   join)r   r*   lr   r   r   r   3   s    zTag.unicodec             C   s   | j j}d|t| f S )Nz<%r tag object %d>)r)   r   id)r   r   r   r   r   __repr__8   s    zTag.__repr__)r+   )r   r   r   objectr%   r$   r   __str__r   r3   __classcell__r   r   )r)   r   r    &   s   
r    r   F)r   r   r   c               @   s   e Zd ZdddZdS )HtmlTagr+   c             C   s(   g }t |j|dd|  td|S )NF)
shortemptyr,   )HtmlVisitorr.   r/   r   r0   )r   r*   r1   r   r   r   r   C   s    zHtmlTag.unicodeN)r+   )r   r   r   r   r   r   r   r   r7   B   s   r7   c               @   s<   e Zd ZeZdZedd ddD ZG dd de	Z
dS )	htmlTc             C   s   g | ]}|r|d fqS )r   r   ).0r
   r   r   r   
<listcomp>L   s    zhtml.<listcomp>a  a,abbr,acronym,address,applet,area,article,aside,audio,b,base,basefont,bdi,bdo,big,blink,blockquote,body,br,button,canvas,caption,center,cite,code,col,colgroup,command,comment,datalist,dd,del,details,dfn,dir,div,dl,dt,em,embed,fieldset,figcaption,figure,footer,font,form,frame,frameset,h1,h2,h3,h4,h5,h6,head,header,hgroup,hr,html,i,iframe,img,input,ins,isindex,kbd,keygen,label,legend,li,link,listing,map,mark,marquee,menu,meta,meter,multicol,nav,nobr,noembed,noframes,noscript,object,ol,optgroup,option,output,p,param,pre,progress,q,rp,rt,ruby,s,samp,script,section,select,small,source,span,strike,strong,style,sub,summary,sup,table,tbody,td,textarea,tfoot,th,thead,time,title,tr,track,tt,u,ul,xmp,var,video,wbr,c               @   s   e Zd Zdd ZdS )z
html.Stylec             K   s2   x,|  D ] \}}|dd}t| || q
W d S )Nr   -)itemsreplacer   )r   kwr
   yr   r   r   r$   \   s    zhtml.Style.__init__N)r   r   r   r$   r   r   r   r   Style[   s   rC   N)r   r   r   r7   r   r   dictsplitr   r4   rC   r   r   r   r   r:   I   s   r:   c               @   s   e Zd ZdZdd ZdS )rawz]just a box that can contain a unicode string that will be
    included directly in the outputc             C   s
   || _ d S )N)uniobj)r   rG   r   r   r   r$   e   s    zraw.__init__N)r   r   r   __doc__r$   r   r   r   r   rF   b   s   rF   c               @   sj   e Zd Z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 Zdd Zdd Zdd ZdS )r-   z% recursive visitor to write unicode. r   Tc             C   s.   || _ i | _i | _|| _|| _g | _|| _d S )N)writecachevisitedr*   	curindentparentsr8   )r   rI   r*   rL   r8   r   r   r   r$   j   s    zSimpleUnicodeVisitor.__init__c             C   sn   |j }y| j| }W nL tk
r`   x,|jD ]}t| |jd}|dk	r,P q,W | j}|| j|< Y nX || dS )z( dispatcher on node's class/bases name. N)r)   rJ   KeyError__mro__getattrr   _SimpleUnicodeVisitor__object)r   Znoder   Zvisitmethodsubclassr   r   r   r/   s   s    zSimpleUnicodeVisitor.visitc             C   s   |  tt| d S )N)rI   escaper   )r   objr   r   r   Z__object   s    zSimpleUnicodeVisitor.__objectc             C   s   |  |j d S )N)rI   rG   )r   rT   r   r   r   rF      s    zSimpleUnicodeVisitor.rawc             C   s<   t || jkstd| jt |< x|D ]}| | q&W d S )Nr   )r2   rK   AssertionErrorr/   )r   rT   elemr   r   r   list   s    
zSimpleUnicodeVisitor.listc             C   sR  t || jksty| jd |_W n tk
r<   d |_Y nX d| jt |< t|d|jj}| j	r| 
|s| dtd| j	   |r|  j	| j7  _	| td|| |f  | j| x|D ]}| | qW | j  | td|  |  j	| j8  _	nF|| | }| |r8| td|f  n| td	||f  d S )
Nr   r   
 z<%s%s>z</%s>z<%s/>z	<%s></%s>)r2   rK   rU   rM   parent
IndexErrorrP   r)   r   rL   	_isinlinerI   r   r*   
attributesr.   r/   pop_issingleton)r   tagtagnamer
   Znameattrr   r   r   r       s,    

zSimpleUnicodeVisitor.Tagc             C   sb   t |j}|  g }x,|D ]$}| |j|}|d k	r|| qW || | td|S )Nr,   )	dirr'   sortrepr_attributer.   extendgetstyler   r0   )r   ra   Zattrlistr1   r   Zresr   r   r   r^      s    

zSimpleUnicodeVisitor.attributesc             C   s^   |d d dkrZt ||}|dr0|d d }t|trB|j}ntt|}d||f S d S )Nr+   __r   rX   z %s="%s")rP   endswith
isinstancerF   rG   rS   r   )r   attrsr   valueinsertr   r   r   re      s    


z#SimpleUnicodeVisitor.repr_attributec             C   sP   y|j j}W n tk
r    g S X dd | D }tdtd| gS dS )z- return attribute list suitable for styling. c             S   s   g | ]\}}|d  | qS )z: r   )r;   r
   rB   r   r   r   r<      s    z1SimpleUnicodeVisitor.getstyle.<locals>.<listcomp>z style="%s"z; N)Zstyler!   r   r?   r   r0   )r   ra   Z	styledictZ	stylelistr   r   r   rg      s    zSimpleUnicodeVisitor.getstylec             C   s   | j S )z*can (and will) be overridden in subclasses)r8   )r   rb   r   r   r   r`      s    z!SimpleUnicodeVisitor._issingletonc             C   s   dS )z*can (and will) be overridden in subclassesFr   )r   rb   r   r   r   r]      s    zSimpleUnicodeVisitor._isinlineN)r   r   T)r   r   r   rH   r$   r/   rQ   rF   rW   r    r^   re   rg   r`   r]   r   r   r   r   r-   h   s   
	
r-   c                   s\   e Zd Zedd ddD Zedd ddD Z fdd	Zd
d Zdd Z	  Z
S )r9   c             C   s   g | ]}|d fqS )r   r   )r;   r
   r   r   r   r<      s    zHtmlVisitor.<listcomp>z3br,img,area,param,col,hr,meta,link,base,input,framer=   c             C   s   g | ]}|d fqS )r   r   )r;   r
   r   r   r   r<      s    za abbr acronym b basefont bdo big br cite code dfn em font i img input kbd label q s samp select small span strike strong sub sup textarea tt u varrZ   c                s0   |dkrt ||}|d krd S tt| ||S )NZclass_)rP   r&   r9   re   )r   rk   r   rl   )r)   r   r   re      s
    
zHtmlVisitor.repr_attributec             C   s
   || j kS )N)single)r   rb   r   r   r   r`      s    zHtmlVisitor._issingletonc             C   s
   || j kS )N)inline)r   rb   r   r   r   r]      s    zHtmlVisitor._isinline)r   r   r   rD   rE   rn   ro   re   r`   r]   r6   r   r   )r)   r   r9      s   r9   c               @   s$   e Zd Zdd Zdd Zdd ZdS )_escapec             C   sb   t dt dt dt dt dt dt dt dt d	t d
i| _tt d| j | _d S )N"z&quot;<z&lt;>z&gt;&z&amp;'z&apos;|)r   rS   recompiler0   keys
charef_rex)r   r   r   r   r$      s    $z_escape.__init__c             C   s   | j |d S )Nr   )rS   group)r   matchr   r   r   	_replacer   s    z_escape._replacerc             C   s@   yt |}W n" tk
r.   t |ddd}Y nX | j| j|S )z& xml-escape the given unicode string. zutf-8r@   )r   )r   UnicodeDecodeErrorrz   subr}   )r   Zustringr   r   r   __call__   s
    z_escape.__call__N)r   r   r   r$   r}   r   r   r   r   r   rp      s   rp   )N)rH   sysrw   version_infor   r   r   r   rW   r    r4   r   r7   r:   rF   r-   r9   rp   rS   r   r   r   r   <module>   s&   

l