3
dX              *   @   s   d Z ddlmZ ddlZddlZddlZddlZddlZddlmZ ej	j
ejdejdejdejdgZd	d
 d=D Zd4Zd5Zd5Zi fd6d7Zd8d9 Zd:d; Zed<k re  dS )>z
A commandline tool for drawing RDF graphs in Graphviz DOT format

You can draw the graph of an RDF file directly:

.. code-block: bash

   rdf2dot my_rdf_file.rdf | dot -Tpng | display

    )absolute_importN)XSDz%http://purl.org/dc/elements/1.1/titlezhttp://xmlns.com/foaf/0.1/namez"http://www.w3.org/2006/vcard/ns#fnz#http://www.w3.org/2006/vcard/ns#orgc             C   s   g | ]}t | qS  )r   ).0xr   r   6/tmp/pip-build-7vycvbft/rdflib/rdflib/tools/rdf2dot.py
<listcomp>   s    r   anyURIbase64BinarybooleanbytedatedateTimedecimaldoubledurationfloatgDaygMonth	gMonthDaygYear
gYearMonth	hexBinaryIDIDREFIDREFSintintegerlanguagelongNameNCNamenegativeIntegerNMTOKENNMTOKENSnonNegativeIntegernonPositiveIntegernormalizedStringpositiveIntegerQNameshortstringtimetokenunsignedByteunsignedIntunsignedLongunsignedShortZblueZblackc          
      sF  t jt}i   fdd}dd }fdd}dd d	d
 }|jd x| D ]~\}}	}
||}|	tjjkrnqNt|
tjtj	fr||
}d}|j|||||	|	| f  qN|| j
|	| ||
| f qNW xf j D ]Z\}}|jd||f  dd t|| D }d}|j||t||| ||dj|f  qW |jd dS ) zN
    Convert the RDF graph to DOT
    writes the dot output to the stream
    c                s    |  krdt    | <  |  S )Nznode%d)len)r   )nodesr   r   node7   s    zrdf2dot.<locals>.nodec          	   S   sB   x t D ]}|j| |}|r|S qW y|jj| d S    | S d S )N   )LABEL_PROPERTIESvalueZnamespace_managercompute_qname)r   gZ	labelProplr   r   r   label=   s    
zrdf2dot.<locals>.labelc                s@   t j| }| jr$d| | j|f S | jr8d|| jf S d| S )Nz&quot;%s&quot;^^%sz&quot;%s&quot;@%sz&quot;%s&quot;)cgiescapedatatyper   )r:   r9   v)qnamer   r   formatliteralI   s    
zrdf2dot.<locals>.formatliteralc          	   S   s.   y|j | }|d d |d  S    | S d S )Nr   :r5   )r8   )r   r9   qr   r   r   r@   Q   s
    
zrdf2dot.<locals>.qnamec             S   s   dS )NBLACKr   )pr   r   r   colorX   s    zrdf2dot.<locals>.colorz/digraph { 
 node [ fontname="DejaVu Sans" ] ; 
z4	%s -> %s [ color=%s, label=< <font point-size='10' z color='#336633'>%s</font> > ] ;
z# %s %s
c             S   s   g | ]}d | qS )z9<tr><td align='left'>%s</td><td align='left'>%s</td></tr>r   )r   r   r   r   r   r   k   s   zrdf2dot.<locals>.<listcomp>z8%s [ shape=none, color=%s label=< <table color='#666666'z/ cellborder='0' cellspacing='0' border='1'><tr>z6<td colspan='2' bgcolor='grey'><B>%s</B></td></tr><tr>z,<td href='%s' bgcolor='#eeeeee' colspan='2'>z4<font point-size='10' color='#6666ff'>%s</font></td>z</tr>%s</table> > ] 
 z}
NzT	%s -> %s [ color=%s, label=< <font point-size='10' color='#336633'>%s</font> > ] ;
zg%s [ shape=none, color=%s label=< <table color='#666666' cellborder='0' cellspacing='0' border='1'><tr>z%s [ shape=none, color=%s label=< <table color='#666666' cellborder='0' cellspacing='0' border='1'><tr><td colspan='2' bgcolor='grey'><B>%s</B></td></tr><tr>z%s [ shape=none, color=%s label=< <table color='#666666' cellborder='0' cellspacing='0' border='1'><tr><td colspan='2' bgcolor='grey'><B>%s</B></td></tr><tr><td href='%s' bgcolor='#eeeeee' colspan='2'>z%s [ shape=none, color=%s label=< <table color='#666666' cellborder='0' cellspacing='0' border='1'><tr><td colspan='2' bgcolor='grey'><B>%s</B></td></tr><tr><td href='%s' bgcolor='#eeeeee' colspan='2'><font point-size='10' color='#6666ff'>%s</font></td>a  %s [ shape=none, color=%s label=< <table color='#666666' cellborder='0' cellspacing='0' border='1'><tr><td colspan='2' bgcolor='grey'><B>%s</B></td></tr><tr><td href='%s' bgcolor='#eeeeee' colspan='2'><font point-size='10' color='#6666ff'>%s</font></td></tr>%s</table> > ] 
)collectionsdefaultdictsetwriterdflibRDFSr;   
isinstanceURIRefZBNodeadditemssorted	NODECOLORjoin)r9   streamoptsfieldsr4   r;   rA   rF   srE   oZsnonZopstrunfr   )r3   r@   r   rdf2dot.   s4    

 "" *r^   c               C   s   t jjd d S )Nz
rdf2dot.py [-f <format>] files...
Read RDF files given on STDOUT, writes a graph of the RDFS schema in DOT
language to stdout
-f specifies parser to use, if not given,

)sysstderrrK   r   r   r   r   _helpx   s    ra   c               C   s   t jjjtt d S )N)rL   extrasZcmdlineutilsmainr^   ra   r   r   r   r   rc      s    rc   __main__))r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   )__doc__
__future__r   rL   Zrdflib.extras.cmdlineutilsr_   r<   rH   r   rM   r;   rO   r6   ZXSDTERMSZ	EDGECOLORrS   ZISACOLORr^   ra   rc   __name__r   r   r   r   <module>
   s:          J

