o
    €Ï  ã                   @   sJ   d Z ddlZddlmZ ddlmZ e d¡Zdd„ ZG dd	„ d	e	ƒ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)ÚURIVariablez	{([^}]+)}c                 C   s   | r|   ¡ }| |¡ |S |S ©N)ÚcopyÚupdate)Úvar_dictÚ	overridesÚopts© r
   ú=/tmp/google-cloud-sdk/lib/third_party/uritemplate/template.pyÚ_merge   s
   
r   c                   @   sT   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	ddd„Z
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.

    c                 C   sN   || _ dd„ t | j ¡D ƒ| _tƒ | _| jD ]}|jD ]}| j |¡ qqd S )Nc                 S   s   g | ]
}t | ¡ d  ƒ‘qS )r   )r   Úgroups)Ú.0Úmr
   r
   r   Ú
<listcomp>G   s    ÿz(URITemplate.__init__.<locals>.<listcomp>)ÚuriÚtemplate_reÚfinditerÚ	variablesr   Úvariable_namesÚadd)Úselfr   ÚvariableÚnamer
   r
   r   Ú__init__B   s   
ÿ

ÿÿzURITemplate.__init__c                 C   s   d|  S )NzURITemplate("%s")r
   ©r   r
   r
   r   Ú__repr__P   s   zURITemplate.__repr__c                 C   s   | j S r   ©r   r   r
   r
   r   Ú__str__S   s   zURITemplate.__str__c                 C   s   | j |j kS r   r   )r   Úotherr
   r
   r   Ú__eq__V   s   zURITemplate.__eq__c                 C   s
   t | jƒS r   )Úhashr   r   r
   r
   r   Ú__hash__Y   s   
zURITemplate.__hash__c                    sb   | j s| jS |}i ‰ | j D ]
}ˆ  | |¡¡ q‡ fdd„}‡ fdd„}|r(|n|}t || j¡S )Nc                    s   ˆ   |  ¡ d d¡S )Nr   Ú )Úgetr   )Úmatch©Úexpandedr
   r   Úreplace_alle   s   z(URITemplate._expand.<locals>.replace_allc                    s"   |   ¡ d } d|  }ˆ  | ¡p|S )Nr   z{%s})r   r%   )r&   Úvarr'   r
   r   Úreplace_partialh   s   z,URITemplate._expand.<locals>.replace_partial)r   r   r   Úexpandr   Úsub)r   r   ÚreplaceÚ	expansionÚvr)   r+   r
   r'   r   Ú_expand\   s   
zURITemplate._expandNc                 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)r1   r   ©r   r   Úkwargsr
   r
   r   r,   q   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   r1   r   r2   r
   r
   r   Úpartialˆ   s   zURITemplate.partialr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r!   r#   r1   r,   r4   r
   r
   r
   r   r   !   s    
r   )r8   ÚreÚuritemplate.orderedsetr   Úuritemplate.variabler   Úcompiler   r   Úobjectr   r
   r
   r
   r   Ú<module>   s    
