U
    VÚdV  ã                   @   sp  d Z ddlmZmZmZ ddlmZmZ	m
Z
mZ ddlmZmZmZ ddlZddlZzddlmZ W n ek
r€   ddlZY nX dZe ¡ ZerðddlmZ ddlmZ dd	lmZ  d
d„ Z!dd„ Z"dd„ Z#dd„ Z$d&dd„Z%e%Z&d'dd„Z'n\ddlm(Z ddlm)Z dd	l*mZ  dd„ Z!dd„ Z"dd„ Z#dd„ Z$d(dd„Z%d)dd„Z&e&Z'e%d ƒZ+e%d!ƒZ,d"d#„ Z-d$d%„ Z.dS )*z¨
Tool functions to support Python 2 and 3.

Inspired by six https://pythonhosted.org/six/

TODO: since it is now based on six, we should remove this module eventually.
é    )ÚBytesIOÚPY2ÚPY3)Ú	cStringIOÚinputÚqueueÚxrange)ÚquoteÚunquoteÚurlparseNÚreStructuredText)Údecodestring)Úencodestring)Úescapec                 C   s
   t | tƒS ©zNReturn True for any string type (for str/unicode on Py2 and bytes/str on Py3).)Ú
isinstanceÚ
basestring©Ús© r   ú/opt/wsgidav/wsgidav/compat.pyÚis_basestring*   s    r   c                 C   s
   t | tƒS ©z>Return True for bytestrings (for str on Py2 and bytes on Py3).©r   Ústrr   r   r   r   Úis_bytes.   s    r   c                 C   s
   t | tƒS ©z8Return True for native strings (for str on Py2 and Py3).r   r   r   r   r   Ú	is_native2   s    r   c                 C   s
   t | tƒS ©zDReturn True for unicode strings (for unicode on Py2 and str on Py3).)r   Úunicoder   r   r   r   Ú
is_unicode6   s    r    Úutf8c                 C   s0   t | ƒtkr|  |¡} nt | ƒtk	r,t| ƒ} | S )zLConvert unicode (text strings) to binary data (str on Py2 and bytes on Py3).)Útyper   Úencoder   ©r   Úencodingr   r   r   Úto_bytes:   s
    r&   c                 C   s   t | ƒtk	rt| |ƒ} | S )z=Convert data to unicode text (unicode on Py2 and str on Py3).)r"   r   r$   r   r   r   Ú
to_unicodeE   s    
r'   )Údecodebytes)Úencodebytesc                 C   s   t | ttfƒS r   )r   r   Úbytesr   r   r   r   r   R   s    c                 C   s
   t | tƒS r   )r   r*   r   r   r   r   r   V   s    c                 C   s
   t | tƒS r   r   r   r   r   r   r   Z   s    c                 C   s
   t | tƒS r   r   r   r   r   r   r    ^   s    c                 C   s   t | ƒtk	rt| |ƒ} | S )zLConvert a text string (unicode) to bytestring (str on Py2 and bytes on Py3).)r"   r*   r$   r   r   r   r&   b   s    
c                 C   s0   t | ƒtkrt| |ƒ} nt | ƒtk	r,t| ƒ} | S )zGConvert data to native str type (bytestring on Py2 and unicode on Py3).)r"   r*   r   r$   r   r   r   Ú	to_nativeh   s
    r+   Ú ú/c                 C   s   |   td¡ d¡S )zDConvert an environment variable to a WSGI 'bytes-as-unicode' string.Úsurrogateescapeú
iso-8859-1)r#   Ú_filesystemencodingÚdecode)Úur   r   r   Úunicode_to_wsgi}   s    r3   c                 C   s
   |   d¡S )z@Convert a native string to a WSGI / HTTP compatible byte string.r/   )r#   r   r   r   r   Úwsgi_to_bytes„   s    r4   )r!   )r!   )r!   )r!   )/Ú__doc__Zsixr   r   r   Z	six.movesr   ÚStringIOr   Zconsole_inputr   r   Zsix.moves.urllib.parser	   r
   r   ÚsysZcollections.abcÚabcÚcollections_abcÚImportErrorÚcollectionsÚ__docformat__Úgetfilesystemencodingr0   Úbase64r   Zbase64_decodebytesr   Úbase64_encodebytesZcgir   Zhtml_escaper   r   r   r    r&   r+   r'   r(   r)   ÚhtmlÚb_emptyZb_slashr3   r4   r   r   r   r   Ú<module>   sH   	
	

