U
    T³d¦  ã                   @   sf   d Z ddlZddlZddlmZ ddlmZ e d¡Zej	ej
 ej
ej
dœdd„ZG d	d
„ d
ƒZdS )zê

uritemplate.template
====================

This module contains the essential inner workings of uritemplate.

What treasures await you:

- URITemplate class

You see a treasure chest of knowledge in front of you.
What do you do?
>

é    N)Ú
orderedset)Úvariablez	{([^}]+)})Úvar_dictÚ	overridesÚreturnc                 C   s   | r|   ¡ }| |¡ |S |S ©N)ÚcopyÚupdate)r   r   Úopts© r   ú8/tmp/pip-unpacked-wheel-qt7p1f5q/uritemplate/template.pyÚ_merge   s
    
r   c                   @   s¨   e Zd ZdZedœdd„Zedœdd„Zedœdd	„Zee	d
œdd„Z
edœdd„Zeje	edœdd„Zdejej ejedœdd„Zdejej ejd dœdd„ZdS )ÚURITemplatea	  This parses the template and will be used to expand it.

    This is the most important object as the center of the API.

    Example::

        from uritemplate import URITemplate
        import requests


        t = URITemplate(
            'https://api.github.com/users/sigmavirus24/gists{/gist_id}'
        )
        uri = t.expand(gist_id=123456)
        resp = requests.get(uri)
        for gist in resp.json():
            print(gist['html_url'])

    Please note::

        str(t)
        # 'https://api.github.com/users/sigmavirus24/gists{/gistid}'
        repr(t)  # is equivalent to
        # URITemplate(str(t))
        # Where str(t) is interpreted as the URI string.

    Also, ``URITemplates`` are hashable so they can be used as keys in
    dictionaries.

    ©Úuric                 C   sP   || _ dd„ t | j ¡D ƒ| _t ¡ | _| jD ]}|jD ]}| j |¡ q8q.d S )Nc                 S   s   g | ]}t  | ¡ d  ¡‘qS )r   )r   ZURIVariableÚgroups)Ú.0Úmr   r   r   Ú
<listcomp>K   s   ÿz(URITemplate.__init__.<locals>.<listcomp>)r   Útemplate_reÚfinditerÚ	variablesr   Z
OrderedSetZvariable_namesÚadd)Úselfr   ÚvarÚnamer   r   r   Ú__init__F   s    
þ


zURITemplate.__init__)r   c                 C   s   d|  S )NzURITemplate("%s")r   ©r   r   r   r   Ú__repr__U   s    zURITemplate.__repr__c                 C   s   | j S r   r   r   r   r   r   Ú__str__X   s    zURITemplate.__str__)Úotherr   c                 C   s   t |tƒstS | j|jkS r   )Ú
isinstancer   ÚNotImplementedr   )r   r    r   r   r   Ú__eq__[   s    
zURITemplate.__eq__c                 C   s
   t | jƒS r   )Úhashr   r   r   r   r   Ú__hash__`   s    zURITemplate.__hash__)r   Úreplacer   c                    sr   | j s| jS |}i ‰ | j D ]}ˆ  | |¡¡ qdtdœ‡ fdd„}dtdœ‡ fdd„}|r`|n|}t || j¡S )Nzre.Match[str])Úmatchr   c                    s   ˆ   |  ¡ d d¡S )Nr   Ú )Úgetr   )r'   ©Úexpandedr   r   Úreplace_alln   s    z(URITemplate._expand.<locals>.replace_allc                    s"   |   ¡ d }d| }ˆ  |¡p |S )Nr   z{%s})r   r)   )r'   Zmatch_groupr   r*   r   r   Úreplace_partialq   s    z,URITemplate._expand.<locals>.replace_partial)r   r   r	   ÚexpandÚstrr   Úsub)r   r   r&   Z	expansionÚvr,   r-   Zreplace_funcr   r*   r   Ú_expandc   s    
zURITemplate._expandN)r   Úkwargsr   c                 K   s   |   t||ƒd¡S )am  Expand the template with the given parameters.

        :param dict var_dict: Optional dictionary with variables and values
        :param kwargs: Alternative way to pass arguments
        :returns: str

        Example::

            t = URITemplate('https://api.github.com{/end}')
            t.expand({'end': 'users'})
            t.expand(end='gists')

        .. note:: Passing values by both parts, may override values in
                  ``var_dict``. For example::

                      expand('https://{var}', {'var': 'val1'}, var='val2')

                  ``val2`` will be used instead of ``val1``.

        F)r2   r   ©r   r   r3   r   r   r   r.   z   s    zURITemplate.expandc                 K   s   t |  t||ƒd¡ƒS )aù  Partially expand the template with the given parameters.

        If all of the parameters for the template are not given, return a
        partially expanded template.

        :param dict var_dict: Optional dictionary with variables and values
        :param kwargs: Alternative way to pass arguments
        :returns: :class:`URITemplate`

        Example::

            t = URITemplate('https://api.github.com{/end}')
            t.partial()  # => URITemplate('https://api.github.com{/end}')

        T)r   r2   r   r4   r   r   r   Úpartial•   s    zURITemplate.partial)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r/   r   r   r   ÚobjectÚboolr#   Úintr%   r   ÚVariableValueDictr2   ÚtÚOptionalZVariableValuer.   r5   r   r   r   r   r   %   s,    þ þ
ü þ
ür   )r9   ÚreÚtypingr>   Zuritemplater   r   Úcompiler   r?   r=   r   r   r   r   r   r   Ú<module>   s   

ý