3
d                 @   sz  d dl mZ d dlmZ d dlmZmZmZ d dlm	Z	m
Z
mZ dZdZerTd dlZd&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eZG dd deZd'ddZedkrfd dlmZmZ d dlZeeZejdd  eded ed! ed"e Zej d#d  ej!ej"d$ Z#e$e# e$e#d  j%i  d d%l&m'Z'm(Z( dS )(    )
MethodType)OrderedDict)TokenConverterParseResultsoriginalTextFor)BNodeVariableURIRefTFNc                s   t |tr|j S t |tr,td| nt |trL fdd|D S t |ttfr j|}t |t	rx rx||dk	r|S r|S t
n,t |trt|dkrt |d S |S dS )a  
    utility function for evaluating something...

    Variables will be looked up in the context
    Normally, non-bound vars is an error,
    set variables=True to return unbound vars

    Normally, an error raises the error,
    set errors=True to return error

    z$What do I do with this CompValue? %sc                s   g | ]}t  |qS  )value).0x)ctxerrors	variablesr
   C/tmp/pip-build-7vycvbft/rdflib/rdflib/plugins/sparql/parserutils.py
<listcomp>B   s    zvalue.<locals>.<listcomp>N   r   )
isinstanceExpreval	CompValue	Exceptionlistr   r   getSPARQLErrorNotBoundErrorr   lenr   )r   valr   r   rr
   )r   r   r   r   r   /   s$    




r   c               @   s    e Zd ZdZdd Zdd ZdS )
ParamValuezq
    The result of parsing a Param
    This just keeps the name/value
    All cleverness is in the CompValue
    c             C   s8   || _ || _t|ttfr.t|dkr.|d }|| _d S )Nr   r   )isListnamer   r   r   r   	tokenList)selfr"   r#   r!   r
   r
   r   __init__^   s
    zParamValue.__init__c             C   s   d| j | jf S )NzParam(%s, %s))r"   r#   )r$   r
   r
   r   __str__f   s    zParamValue.__str__N)__name__
__module____qualname____doc__r%   r&   r
   r
   r
   r   r    W   s   r    c               @   s"   e Zd ZdZdddZdd ZdS )	Paramz
    A pyparsing token for labelling a part of the parse-tree
    if isList is true repeat occurrences of ParamList have
    their values merged in a list
    Fc             C   s(   || _ || _tj| | | j| j d S )N)r"   r!   r   r%   addParseAction
postParse2)r$   r"   exprr!   r
   r
   r   r%   q   s    zParam.__init__c             C   s   t | j|| jS )N)r    r"   r!   )r$   r#   r
   r
   r   r-   w   s    zParam.postParse2N)F)r'   r(   r)   r*   r%   r-   r
   r
   r
   r   r+   j   s   
r+   c               @   s   e Zd ZdZdd ZdS )	ParamListz1
    A shortcut for a Param with isList=True
    c             C   s   t j| ||d d S )NT)r+   r%   )r$   r"   r.   r
   r
   r   r%      s    zParamList.__init__N)r'   r(   r)   r*   r%   r
   r
   r
   r   r/   {   s   r/   c               @   s   e Zd ZdZdS )plistz:this is just a list, but we want our own type to check forN)r'   r(   r)   r*   r
   r
   r
   r   r0      s   r0   c               @   sT   e Zd ZdZdd Zdd Zdd Zdd	 ZdddZdd Z	dddZ
dd ZdS )r   zn
    The result of parsing a Comp
    Any included Params are avaiable as Dict keys
    or as attributes

    c             K   s   t j|  || _| j| d S )N)r   r%   r"   update)r$   r"   valuesr
   r
   r   r%      s    
zCompValue.__init__c             C   s   t | jf| S )N)r   r"   )r$   r
   r
   r   clone   s    zCompValue.clonec             C   s   | j d tj|  S )N_)r"   r   r&   )r$   r
   r
   r   r&      s    zCompValue.__str__c             C   s   | j d tj|  S )Nr4   )r"   dict__repr__)r$   r
   r
   r   r6      s    zCompValue.__repr__Fc             C   s    | j d k	rt| j ||S |S d S )N)r   r   )r$   r   r   r   r
   r
   r   _value   s    
zCompValue._valuec             C   s   | j tj| |S )N)r7   r   __getitem__)r$   ar
   r
   r   r8      s    zCompValue.__getitem__c             C   s   | j tj| ||||S )N)r7   r   r   )r$   r9   r   r   r
   r
   r   r      s    zCompValue.getc             C   s.   |dkrt y| | S  tk
r(   d S X d S )N_OrderedDict__root_OrderedDict__end)r:   r;   )AttributeErrorKeyError)r$   r9   r
   r
   r   __getattr__   s    zCompValue.__getattr__N)FF)FF)r'   r(   r)   r*   r%   r3   r&   r6   r7   r8   r   r>   r
   r
   r
   r   r      s   

r   c                   s.   e Zd ZdZd fdd	Zi fddZ  ZS )r   z)
    A CompValue that is evaluatable
    Nc                s.   t t| j|f| d | _|r*t|| | _d S )N)superr   r%   _evalfnr   )r$   r"   evalfnr2   )	__class__r
   r   r%      s    zExpr.__init__c             C   sD   z6y|| _ | j|S  tk
r2 } z|S d }~X nX W d d | _ X d S )N)r   r@   r   )r$   r   er
   r
   r   r      s    
z	Expr.eval)N)r'   r(   r)   r*   r%   r   __classcell__r
   r
   )rB   r   r      s   r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	Compz
    A pyparsing token for grouping together things with a label
    Any sub-tokens that are not Params will be ignored.

    Returns CompValue / Expr objects - depending on whether evalFn is set.
    c             C   s"   || _ tj| | || _d | _d S )N)r.   r   r%   r"   rA   )r$   r"   r.   r
   r
   r   r%      s    zComp.__init__c             C   s   | j r t| j}t| j ||_n8t| j}| jdkrXt| j}|j|d d }||d< xR|D ]J}t	|t
r^|jr|j|krt ||j< ||j j|j q^|j||j< q^W |S )NZServiceGraphPatternr   service_string)rA   r   r"   r   r@   r   r   r.   searchStringr   r    r!   r0   appendr#   )r$   instringlocr#   resZsgprF   tr
   r
   r   	postParse   s     






zComp.postParsec             C   s
   || _ | S )N)rA   )r$   rA   r
   r
   r   	setEvalFn   s    zComp.setEvalFnN)r'   r(   r)   r*   r%   rM   rN   r
   r
   r
   r   rE      s   rE    c             C   s  g }t | trx&| j D ]}|jt|||d  qW xFt| j D ]6\}}|jd|d| |f  |jt|||d  qDW nt | tr|jd|d| | jf  x| j D ]:\}}|jd|d|d  |f  |jt|||d  qW nt | t	rHx| j D ]<\}}|jd|d|d  |f  |jt|||d  qW nLt | t
r|x>| D ]}|jt|||d  qZW n|jd|d| | f  dj|S )Nr   z
%s%s- %s:
z  z
%s%s> %s:
   z	%s%s- %r
rO   )r   r   asListrH   prettify_parsetreesorteditemsr   r"   r5   r   join)rL   indentdepthoutrC   kvr
   r
   r   rR      s*    


rR   __main__)Wordnumsc             C   s   t | d S )Nr   )int)r   r
   r
   r   <lambda>  s    r_   plusr9   +bc             C   s   | j | j S )N)r9   rb   )r$   r   r
   r
   r   r_     s    r   )r   r   )FF)rO   r   ))typesr   collectionsr   	pyparsingr   r   r   Zrdflibr   r   r	   DEBUG	tracebackr   objectr    r+   r/   r   r0   r   r   rE   rR   r'   r\   r]   sysNumbersetParseActionZPlusrN   parseStringargvr   printr   Zrdflib.plugins.sparql.sparqlr   r   r
   r
   r
   r   <module>   s6   "
(	./

