B
    `                  @   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   R/home/kop/projects/devel/pgwui/test_venv/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tagspec	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   r4   __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/   r0   r   r1   )r   r+   r2   r   r   r   r   C   s    zHtmlTag.unicodeN)r,   )r   r   r    r   r   r   r   r   r8   B   s   r8   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   rD   N)r   r   r    r8   r   r   dictsplitr   r5   rD   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   rH   r   r   r   r%   e   s    zraw.__init__N)r   r   r    __doc__r%   r   r   r   r   rG   b   s   rG   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parentsr9   )r   rJ   r+   rM   r9   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*   rK   KeyError__mro__getattrr   _SimpleUnicodeVisitor__object)r   noder   Zvisitmethodsubclassr   r   r   r0   s   s    zSimpleUnicodeVisitor.visitc             C   s   |  tt| d S )N)rJ   escaper   )r   objr   r   r   Z__object   s    zSimpleUnicodeVisitor.__objectc             C   s   |  |j d S )N)rJ   rH   )r   rV   r   r   r   rG      s    zSimpleUnicodeVisitor.rawc             C   s<   t || jkstd| jt |< x|D ]}| | q&W d S )Nr   )r3   rL   AssertionErrorr0   )r   rV   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>)r3   rL   rW   rN   parent
IndexErrorrQ   r*   r   rM   	_isinlinerJ   r   r+   
attributesr/   r0   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   r1   )r   rc   Zattrlistr2   r   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   rZ   z %s="%s")rQ   endswith
isinstancerG   rH   rU   r   )r   attrsr   valueinsertr   r   r   rg      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
   rC   r   r   r   r=      s    z1SimpleUnicodeVisitor.getstyle.<locals>.<listcomp>z style="%s"z; N)styler"   r   r@   r   r1   )r   rc   Z	styledictZ	stylelistr   r   r   ri      s    zSimpleUnicodeVisitor.getstylec             C   s   | j S )z*can (and will) be overridden in subclasses)r9   )r   rd   r   r   r   rb      s    z!SimpleUnicodeVisitor._issingletonc             C   s   dS )z*can (and will) be overridden in subclassesFr   )r   rd   r   r   r   r_      s    zSimpleUnicodeVisitor._isinlineN)r   r   T)r   r   r    rI   r%   r0   rR   rG   rY   r!   r`   rg   ri   rb   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 )r:   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 varr\   c                s0   |dkrt ||}|d krd S tt| ||S )Nclass_)rQ   r'   r:   rg   )r   rn   r   ro   )r*   r   r   rg      s
    
zHtmlVisitor.repr_attributec             C   s
   || j kS )N)single)r   rd   r   r   r   rb      s    zHtmlVisitor._issingletonc             C   s
   || j kS )N)inline)r   rd   r   r   r   r_      s    zHtmlVisitor._isinline)r   r   r    rE   rF   rs   rt   rg   rb   r_   r7   r   r   )r*   r   r:      s   r:   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   rU   recompiler1   keys
charef_rex)r   r   r   r   r%      s    $z_escape.__init__c             C   s   | j |d S )Nr   )rU   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-8rA   )r   )r   UnicodeDecodeErrorr   subr   )r   Zustringr   r   r   __call__   s
    z_escape.__call__N)r   r   r    r%   r   r   r   r   r   r   ru      s   ru   )N)rI   sysr|   version_infor   r   r   r   rY   r!   r5   r   r8   r;   rG   r.   r:   ru   rU   r   r   r   r   <module>   s&   

l