U
    I]
                     @   s   d Z ddlmZmZ ddlmZmZmZ zddlZW n  e	k
rT   ddl
mZ Y nX dZdd Zdd	 Zd
d Zdd Zdd Zdd Zdd ZdS )zn
oauthlib.utils
~~~~~~~~~~~~~~

This module contains utility methods used by various parts of the OAuth
spec.
    )absolute_importunicode_literals)quoteunicode_typeunquoteNZ>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789c                    s    fdd} j |_ |S )zDecorator which filters params to remove non-oauth_* parameters

    Assumes the decorated method takes a params dict or list of tuples as its
    first argument.
    c                    s   t | }  | f||S )N)filter_oauth_params)paramsargskwargstarget ?/usr/lib/python3/dist-packages/oauthlib/oauth1/rfc5849/utils.pywrapper   s    zfilter_params.<locals>.wrapper)__doc__)r   r   r   r   r   filter_params   s    r   c                 C   s:   dd }t | tr(tt|t|  S tt|| S dS )zARemoves all non oauth parameters from a dict or a list of params.c                 S   s   | d  dS )Nr   Zoauth_)
startswith)Zkvr   r   r   <lambda>(       z%filter_oauth_params.<locals>.<lambda>N)
isinstancedictlistfilteritems)r   Zis_oauthr   r   r   r   &   s    
r   c                 C   s.   t | ts"tdd| t| f  t| ddS )zEscape a unicode string in an OAuth-compatible fashion.

    Per `section 3.6`_ of the spec.

    .. _`section 3.6`: https://tools.ietf.org/html/rfc5849#section-3.6

    z$Only unicode objects are escapable. zGot %r of type %s.   ~)Zsafe)r   r   
ValueErrortyper   ur   r   r   escape/   s
    
r   c                 C   s   t | tstdt| S )Nz%Only unicode objects are unescapable.)r   r   r   r   r   r   r   r   unescape?   s    
r    c                 C   s
   t | S )z1A unicode-safe version of urllib2.parse_keqv_list)urllib2parse_keqv_list)lr   r   r   r"   E   s    r"   c                 C   s
   t | S )z1A unicode-safe version of urllib2.parse_http_list)r!   parse_http_listr   r   r   r   r$   K   s    r$   c              	   C   sn   d  }| dt|   |rbt| t|d }ztt| W S  ttfk
r`   Y nX tddS )z;Parse an OAuth authorization header into a list of 2-tupleszOAuth NzMalformed authorization header)	lowerlenr   r$   r   r"   r   
IndexErrorr   )Zauthorization_headerZauth_schemer   r   r   r   parse_authorization_headerQ   s    r(   )r   Z
__future__r   r   Zoauthlib.commonr   r   r   r!   ImportErrorZurllib.requestZrequestZUNICODE_ASCII_CHARACTER_SETr   r   r   r    r"   r$   r(   r   r   r   r   <module>   s   	