3
d*%                 @   s  d dl mZ d dl m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	Z	d dl
mZ d dl
mZ d dl
mZ d dlmZ d	d
dddg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
eZG dd deZG dd deZdS )    )absolute_import)division)print_functionN)BytesIO)PY2)	text_type)urlparse	ProcessorResultResultParserResultSerializerResultExceptionc               @   s(   e Zd ZdZdd Zi i dfddZdS )r	   z
    Query plugin interface.

    This module is useful for those wanting to write a query processor
    that can plugin to rdf. If you are wanting to execute a query you
    likely want to do so through the Graph class query method.

    c             C   s   d S )N )selfgraphr   r   ./tmp/pip-build-7vycvbft/rdflib/rdflib/query.py__init__   s    zProcessor.__init__Fc             C   s   d S )Nr   )r   
strOrQueryinitBindingsinitNsDEBUGr   r   r   query"   s    zProcessor.queryN)__name__
__module____qualname____doc__r   r   r   r   r   r   r	      s   c               @   s&   e Zd ZdZdd Zi i fddZdS )UpdateProcessora!  
    Update plugin interface.

    This module is useful for those wanting to write an update
    processor that can plugin to rdflib. If you are wanting to execute
    an update statement you likely want to do so through the Graph
    class update method.

    .. versionadded:: 4.0

    c             C   s   d S )Nr   )r   r   r   r   r   r   3   s    zUpdateProcessor.__init__c             C   s   d S )Nr   )r   r   r   r   r   r   r   update6   s    zUpdateProcessor.updateN)r   r   r   r   r   r   r   r   r   r   r   &   s   r   c               @   s   e Zd ZdS )r   N)r   r   r   r   r   r   r   r   :   s   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	EncodeOnlyUnicodezR
    This is a crappy work-around for
    http://bugs.python.org/issue11649


    c             C   s
   || _ d S )N)_EncodeOnlyUnicode__stream)r   streamr   r   r   r   F   s    zEncodeOnlyUnicode.__init__c             C   s.   t |tr| jj|jd n| jj| d S )Nzutf-8)
isinstancer   r   writeencode)r   argr   r   r   r"   I   s    
zEncodeOnlyUnicode.writec             C   s   t | j|S )N)getattrr   )r   namer   r   r   __getattr__O   s    zEncodeOnlyUnicode.__getattr__N)r   r   r   r   r   r"   r'   r   r   r   r   r   >   s   r   c                   sB   e Zd ZdZ fddZdd Zdd Zdd	d
Zdd Z  Z	S )	ResultRowa  
    a single result row
    allows accessing bindings as attributes or with []

    >>> from rdflib import URIRef, Variable
    >>> rr=ResultRow({ Variable('a'): URIRef('urn:cake') }, [Variable('a')])

    >>> rr[0]
    rdflib.term.URIRef(u'urn:cake')
    >>> rr[1]
    Traceback (most recent call last):
        ...
    IndexError: tuple index out of range

    >>> rr.a
    rdflib.term.URIRef(u'urn:cake')
    >>> rr.b
    Traceback (most recent call last):
        ...
    AttributeError: b

    >>> rr['a']
    rdflib.term.URIRef(u'urn:cake')
    >>> rr['b']
    Traceback (most recent call last):
        ...
    KeyError: 'b'

    >>> rr[Variable('a')]
    rdflib.term.URIRef(u'urn:cake')

    .. versionadded:: 4.0

    c                s<   t t| j|  fdd|D }tdd t|D |_|S )Nc             3   s   | ]} j |V  qd S )N)get).0v)valuesr   r   	<genexpr>z   s    z$ResultRow.__new__.<locals>.<genexpr>c             s   s"   | ]}t |d  |d fV  qdS )   r   N)r   )r*   xr   r   r   r-   {   s   )superr(   __new__dict	enumeratelabels)clsr,   r4   instance)	__class__)r,   r   r1   w   s
    
zResultRow.__new__c             C   s$   || j krt|tj| | j | S )N)r4   AttributeErrortuple__getitem__)r   r&   r   r   r   r'      s    
zResultRow.__getattr__c             C   sn   yt j| |S  tk
rh   || jkr8t j| | j| S t|| jkr\t j| | jt| S t|Y nX d S )N)r9   r:   	TypeErrorr4   r   KeyError)r   r&   r   r   r   r:      s    
zResultRow.__getitem__Nc             C   s"   y| | S  t k
r   |S X d S )N)r<   )r   r&   defaultr   r   r   r)      s    zResultRow.getc                s   t  fdd jD S )Nc             3   s&   | ]} | d k	r| | fV  qd S )Nr   )r*   r+   )r   r   r   r-      s    z#ResultRow.asdict.<locals>.<genexpr>)r2   r4   )r   r   )r   r   asdict   s    zResultRow.asdict)N)
r   r   r   r   r1   r'   r:   r)   r>   __classcell__r   r   )r7   r   r(   S   s   "

r(   c               @   s~   e Zd ZdZdd Zdd Zdd Zeeedd	Ze	dddZ
dddZdd Zdd ZerbeZdd Zdd Zdd Zd
S )r
   a  
    A common class for representing query result.

    There is a bit of magic here that makes this appear like different
    Python objects, depending on the type of result.

    If the type is "SELECT", iterating will yield lists of ResultRow objects

    If the type is "ASK", iterating will yield a single bool (or
    bool(result) will return the same bool)

    If the type is "CONSTRUCT" or "DESCRIBE" iterating will yield the
    triples.

    len(result) also works.

    c             C   s<   |dkrt d| || _d | _d | _d | _d | _d | _d S )N	CONSTRUCTDESCRIBESELECTASKzUnknown Result type: %s)r@   rA   rB   rC   )r   typevars	_bindings_genbindings	askAnswerr   )r   type_r   r   r   r      s    zResult.__init__c             C   s&   | j r |  jt| j 7  _d | _ | jS )N)rG   rF   list)r   r   r   r   _get_bindings   s    zResult._get_bindingsc             C   s*   t |tjtjfr || _g | _n|| _d S )N)r!   typesGeneratorType	itertoolsislicerG   rF   )r   br   r   r   _set_bindings   s    zResult._set_bindingsz$a list of variable bindings as dicts)docNc             K   sT   ddl m} |r|}n|r,|jddd }nd}|j|t }|j| fd|i|S )Nr   )plugin;r.   xmlcontent_type)rdflibrS   splitr)   r   parse)sourceformatrV   kwargsrS   Z
plugin_keyparserr   r   r   rY      s    zResult.parseutf-8rU   c             K   s0  | j dkr$| jj|f||d|S ddlm} |j|t| }|d krtt }t|}|j|fd|i| |j	 S t
|dr|}|j|fd|i| n|}	t|	\}
}}}}}|dkrtd d S tj \}}tj|d}|j|fd|i| |j  t
tdrtj|| ntj|| tj| d S )Nr@   rA   )encodingr[   r   )rS   r_   r"    zWARNING: not saving as locationzis not a local file referencewbmove)r@   rA   z<WARNING: not saving as locationis not a local file reference)rD   r   	serializerW   rS   r)   r   r   r   getvaluehasattrr   printtempfilemkstemposfdopencloseshutilrb   copyremove)r   Zdestinationr_   r[   argsrS   
serializerr    Zstream2locationschemenetlocpathparamsr   fragmentfdr&   r   r   r   rc      s6    

zResult.serializec             C   s0   | j dkrdS | j dkr"t| jS t| jS d S )NrC   r.   rB   )rD   lenbindingsr   )r   r   r   r   __len__   s
    


zResult.__len__c             C   s    | j dkr| jS t| dkS d S )NrC   r   )rD   rH   rx   )r   r   r   r   __bool__  s    
zResult.__bool__c             c   s   | j dkr"x| jD ]
}|V  qW n|| j dkr6| jV  nh| j dkr| jr|x,| jD ]"}|rN| jj| t|| jV  qNW d | _n"x | jD ]}|rt|| jV  qW d S )Nr@   rA   rC   rB   )r@   rA   )rD   r   rH   rG   rF   appendr(   rE   )r   trP   r   r   r   __iter__  s    



zResult.__iter__c                sl    j dkr" jd k	r" jj |S  j dkrX|dkrXtjdtdd  fdd	 jD S td
 |f d S )Nr@   rA   rB   resultzPaccessing the 'result' attribute is deprecated. Iterate over the object instead.   )
stacklevelc                s$   g | ] t  fd djD qS )c             3   s   | ]} | V  qd S )Nr   )r*   r+   )rP   r   r   r-   (  s    z0Result.__getattr__.<locals>.<listcomp>.<genexpr>)r9   rE   )r*   )r   )rP   r   
<listcomp>(  s    z&Result.__getattr__.<locals>.<listcomp>z!'%s' object has no attribute '%s')r@   rA   )rD   r   r'   warningswarnDeprecationWarningry   r8   )r   r&   r   )r   r   r'     s    
zResult.__getattr__c          	   C   sh   yX| j |j krdS | j dkr(| j|jkS | j dkrJ| j|jkoH| j|jkS | j|jkS W n
   dS d S )NFrC   rB   )rD   rH   rE   ry   r   )r   otherr   r   r   __eq__-  s    

zResult.__eq__)NNN)Nr^   rU   )r   r   r   r   r   rK   rQ   propertyry   staticmethodrY   rc   rz   r{   r   __nonzero__r~   r'   r   r   r   r   r   r
      s    
"c               @   s   e Zd Zdd Zdd ZdS )r   c             C   s   d S )Nr   )r   r   r   r   r   ?  s    zResultParser.__init__c             K   s   dS )zreturn a Result objectNr   )r   rZ   r\   r   r   r   rY   B  s    zResultParser.parseN)r   r   r   r   rY   r   r   r   r   r   =  s   c               @   s   e Zd Zdd ZdddZdS )r   c             C   s
   || _ d S )N)r   )r   r   r   r   r   r   I  s    zResultSerializer.__init__utf-8c             K   s   dS )z#return a string properly serializedNr   )r   r    r_   r\   r   r   r   rc   L  s    zResultSerializer.serializeN)r   )r   r   r   r   rc   r   r   r   r   r   G  s   )
__future__r   r   r   ri   rN   rl   rg   r   rL   sixr   r   r   Zsix.moves.urllib.parser   __all__objectr	   r   	Exceptionr   r   r9   r(   r
   r   r   r   r   r   r   <module>   s.   E &
