3
(h\'                 @   s6  d dl m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mZ ejrjd dlmZ G dd	 d	eZejZd%ddZejedeeejejejf Zdd Zd&ddZd'ddZd(ddZejreZeZ neZeZ eZ!eZ"de_#de _#dd Z$dd Z%dd Z&dd  Z'd!d" Z(d#d$ Z)e) Z*dS ))    )unicode_literalsN)Decimal)six)Promise)quoteunquote)unquote_to_bytesc               @   s   e Zd Zdd Zdd ZdS )DjangoUnicodeDecodeErrorc             G   s   || _ tj| f|  d S )N)objUnicodeDecodeError__init__)selfr
   args r   9/tmp/pip-install-q3hcpn_q/Django/django/utils/encoding.pyr      s    z!DjangoUnicodeDecodeError.__init__c             C   s    t j| }d|| jt| jf S )Nz%s. You passed in %r (%s))r   __str__r
   type)r   originalr   r   r   r      s    
z DjangoUnicodeDecodeError.__str__N)__name__
__module____qualname__r   r   r   r   r   r   r	      s   r	   utf-8Fstrictc             C   s   t | tr| S t| |||S )z
    Returns a text object representing 's' -- unicode on Python 2 and str on
    Python 3. Treats bytestrings using the 'encoding' codec.

    If strings_only is True, don't convert (some) non-string-like objects.
    )
isinstancer   
force_text)sencodingstrings_onlyerrorsr   r   r   
smart_text   s    
r   c             C   s
   t | tS )zDetermine if the object instance is of a protected type.

    Objects of protected types are preserved as-is when passed to
    force_text(strings_only=True).
    )r   _PROTECTED_TYPES)r
   r   r   r   is_protected_type1   s    r!   c                s   t t| tjr| S r$t| r$| S yvt t| tjstjrbt| trVtj|  } qtj| } qt	| drxtj| } qtjt|  } n| j
 } W nZ tk
r } z>t| tst| f|j ndj fdd| D } W Y dd}~X nX | S )z
    Similar to smart_text, except that lazy instances are resolved to
    strings, rather than kept as lazy objects.

    If strings_only is True, don't convert (some) non-string-like objects.
    __unicode__ c             3   s   | ]}t | V  qd S )N)r   ).0arg)r   r   r   r   r   	<genexpr>_   s   zforce_text.<locals>.<genexpr>N)
issubclassr   r   	text_typer!   string_typesPY3r   byteshasattrdecoder   	Exceptionr	   r   join)r   r   r   r   er   )r   r   r   r   r   :   s(    


r   c             C   s   t | tr| S t| |||S )z
    Returns a bytestring version of 's', encoded as specified in 'encoding'.

    If strings_only is True, don't convert (some) non-string-like objects.
    )r   r   force_bytes)r   r   r   r   r   r   r   smart_bytesd   s    
r2   c                s   t | tr* dkr| S | jdj S r:t| r:| S t | tjrNt| S t | trjtj| j S t | tj	sy"tj
rtj| j S t| S W q tk
r   t | trdj fdd| D S tj| j S X n| j S dS )z
    Similar to smart_bytes, except that lazy instances are resolved to
    strings, rather than kept as lazy objects.

    If strings_only is True, don't convert (some) non-string-like objects.
    zutf-8    c             3   s   | ]}t | V  qd S )N)r1   )r$   r%   )r   r   r   r   r   r&      s   zforce_bytes.<locals>.<genexpr>N)r   r+   r-   encoder!   r   
memoryviewr   r(   r)   r*   UnicodeEncodeErrorr.   r/   )r   r   r   r   r   )r   r   r   r   r1   p   s*    



r1   zw
Apply smart_text in Python 3 and smart_bytes in Python 2.

This is suitable for writing to sys.stdout (for instance).
z;
Apply force_text in Python 3 and force_bytes in Python 2.
c             C   s   | dkr| S t t| ddS )u  
    Convert an Internationalized Resource Identifier (IRI) portion to a URI
    portion that is suitable for inclusion in a URL.

    This is the algorithm from section 3.1 of RFC 3987.  However, since we are
    assuming input is either UTF-8 or unicode already, we can simplify things a
    little from the full method.

    Takes an IRI in UTF-8 bytes (e.g. '/I â¥ Django/') or unicode
    (e.g. '/I ♥ Django/') and returns ASCII bytes containing the encoded result
    (e.g. '/I%20%E2%99%A5%20Django/').
    Ns   /#%[]=:;$&()+,!?*@'~)safe)r   r1   )irir   r   r   
iri_to_uri   s    r9   c             C   s8   | dkr| S t | } tjr"t| nt| }t|jdS )u;  
    Converts a Uniform Resource Identifier(URI) into an Internationalized
    Resource Identifier(IRI).

    This is the algorithm from section 3.2 of RFC 3987.

    Takes an URI in ASCII bytes (e.g. '/I%20%E2%99%A5%20Django/') and returns
    unicode containing the encoded result (e.g. '/I â¥ Django/').
    Nzutf-8)r1   r   r*   r   r   repercent_broken_unicoder-   )urir8   r   r   r   
uri_to_iri   s
    
r<   c             C   s   t t| ddS )zh
    Escape the unsafe characters from the path portion of a Uniform Resource
    Identifier (URI).
    s   /:@&+$,-_.!~*'())r7   )r   r1   )pathr   r   r   escape_uri_path   s    r>   c             C   sv   y| j d W nb tk
rp } zFt| |j|j dd}t| d|j t| | |jd  } W Y dd}~X nX | S )z
    As per section 3.2 of RFC 3987, step three of converting a URI into an IRI,
    we need to re-percent-encode any octet produced that is not part of a
    strictly legal UTF-8 octet sequence.
    zutf-8s   /#%[]=:;$&()+,!?*@'~)r7   N)r-   r   r   startendr:   r1   )r=   r0   Z	repercentr   r   r   r:      s    8r:   c             C   s$   | dkr| S t t| jddddS )a  Convert a file system path to a URI portion that is suitable for
    inclusion in a URL.

    We are assuming input is either UTF-8 or unicode already.

    This method will encode certain chars that would normally be recognized as
    special chars for URIs.  Note that this method does not encode the '
    character, as it is a valid character within URIs.  See
    encodeURIComponent() JavaScript function for more details.

    Returns an ASCII string containing the encoded result.
    N   \   /s   /~!*()')r7   )r   r1   replace)r=   r   r   r   filepath_to_uri   s    rD   c              C   s<   yt j d pd} tj|  W n tk
r6   d} Y nX | S )z
    The encoding of the default system locale but falls back to the given
    fallback encoding if the encoding is unsupported by python or could
    not be determined.  See tickets #10335 and #5846
       ascii)localegetdefaultlocalecodecslookupr.   )r   r   r   r   get_system_encoding  s    
rK   )r   Fr   )r   Fr   )r   Fr   )r   Fr   )+
__future__r   rI   datetimerG   decimalr   Zdjango.utilsr   Zdjango.utils.functionalr   Z#django.utils.six.moves.urllib.parser   r   r*   urllib.parser   r   r	   python_2_unicode_compatibler   integer_typesr   floatdatetimer    r!   r   r2   r1   Z	smart_strZ	force_strZsmart_unicodeZforce_unicode__doc__r9   r<   r>   r:   rD   rK   ZDEFAULT_LOCALE_ENCODINGr   r   r   r   <module>   sD   
	
*

$